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