1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.UnoRuntime;
25 
26 /** This class gives you information on the selected objects (text range, text
27  * frame, or graphics) at an OpenOffice.org Server. The Office must be started in
28  * advance and you must have selected something (text, graphics, ...)
29  */
30 public class WriterSelector {
31     /**
32      * @param args No arguments.
33      */
main(String args[])34     public static void main(String args[]) {
35         com.sun.star.uno.XComponentContext xContext = null;
36 
37         try {
38 
39             // bootstrap UNO and get the remote component context. The context can
40             // be used to get the service manager
41             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
42             System.out.println("Connected to a running office ...");
43 
44             // get the remote office service manager
45             com.sun.star.lang.XMultiComponentFactory xMCF =
46                 xContext.getServiceManager();
47 
48             // get a new instance of the desktop
49             com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop)
50                 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
51                     xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
52                                                    xContext ) );
53 
54             com.sun.star.frame.XComponentLoader xCompLoader =
55                 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
56                     com.sun.star.frame.XComponentLoader.class, xDesktop);
57 
58             com.sun.star.lang.XComponent xComponent =
59                 xCompLoader.loadComponentFromURL("private:factory/swriter",
60                     "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
61             {
62             com.sun.star.text.XTextDocument xDoc =(com.sun.star.text.XTextDocument)
63                 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
64                                           xComponent);
65             xDoc.getText().setString("Please select something in this text and press then \"return\" in the shell where you have started the example.\n");
66 
67             // ensure that the document content is optimal visible
68             com.sun.star.frame.XModel xModel =
69                 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
70                     com.sun.star.frame.XModel.class, xDoc);
71 
72             com.sun.star.view.XViewSettingsSupplier xViewSettings =
73                 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface(
74                     com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController());
75             xViewSettings.getViewSettings().setPropertyValue(
76                 "ZoomType", new Short((short)0));
77             }
78             // test document will be closed later
79 
80             System.out.println("\nPlease select something in the test document and press then \"return\" to continues the example ...");
81             char c = 'X';
82             do{
83                 c = (char) System.in.read();
84             }while ((c != 13) && (c != 10));
85 
86             // Getting the current frame from the OpenOffice.org Server.
87             com.sun.star.frame.XFrame xframe = xDesktop.getCurrentFrame();
88 
89             // Getting the controller.
90             com.sun.star.frame.XController xController = xframe.getController();
91 
92             com.sun.star.view.XSelectionSupplier xSelSupplier =
93                 (com.sun.star.view.XSelectionSupplier)UnoRuntime.queryInterface(
94                     com.sun.star.view.XSelectionSupplier.class, xController );
95 
96             Object oSelection = xSelSupplier.getSelection();
97 
98             com.sun.star.lang.XServiceInfo xServInfo =
99                 (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface(
100                     com.sun.star.lang.XServiceInfo.class, oSelection );
101 
102             if ( xServInfo.supportsService("com.sun.star.text.TextRanges") )
103             {
104                 com.sun.star.container.XIndexAccess xIndexAccess =
105                     (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
106                         com.sun.star.container.XIndexAccess.class, oSelection);
107 
108                 int count = xIndexAccess.getCount();
109                 com.sun.star.text.XTextRange xTextRange = null;
110                 for ( int i = 0; i < count; i++ ) {
111                     xTextRange = (com.sun.star.text.XTextRange)
112                         UnoRuntime.queryInterface(
113                             com.sun.star.text.XTextRange.class,
114                             xIndexAccess.getByIndex(i));
115 
116                     System.out.println( "You have selected a text range: \""
117                                         + xTextRange.getString() + "\"." );
118                 }
119             }
120 
121             if ( xServInfo.supportsService("com.sun.star.text.TextGraphicObject") )
122             {
123                 System.out.println( "You have selected a graphics." );
124             }
125 
126             if ( xServInfo.supportsService("com.sun.star.text.TextTableCursor") )
127             {
128                 System.out.println( "You have selected a text table." );
129             }
130 
131 
132             // close test document
133             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
134                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
135                                           xComponent );
136 
137             if (xCloseable != null ) {
138                 xCloseable.close(false);
139             } else
140             {
141                 xComponent.dispose();
142             }
143 
144             System.exit(0);
145         }
146         catch( Exception e ) {
147             e.printStackTrace(System.err);
148             System.exit(1);
149         }
150     }
151 }
152