1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com
.sun
.star
.uno
.UnoRuntime
;
37 /** This class gives you information on the selected objects (text range, text
38 * frame, or graphics) at an OpenOffice.org Server. The Office must be started in
39 * advance and you must have selected something (text, graphics, ...)
41 public class WriterSelector
{
43 * @param args No arguments.
45 public static void main(String args
[]) {
46 com
.sun
.star
.uno
.XComponentContext xContext
= null;
50 // bootstrap UNO and get the remote component context. The context can
51 // be used to get the service manager
52 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
53 System
.out
.println("Connected to a running office ...");
55 // get the remote office service manager
56 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
57 xContext
.getServiceManager();
59 // get a new instance of the desktop
60 com
.sun
.star
.frame
.XDesktop xDesktop
= UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XDesktop
.class,
61 xMCF
.createInstanceWithContext("com.sun.star.frame.Desktop",
64 com
.sun
.star
.frame
.XComponentLoader xCompLoader
=
65 UnoRuntime
.queryInterface(
66 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
68 com
.sun
.star
.lang
.XComponent xComponent
=
69 xCompLoader
.loadComponentFromURL("private:factory/swriter",
70 "_blank", 0, new com
.sun
.star
.beans
.PropertyValue
[0]);
72 com
.sun
.star
.text
.XTextDocument xDoc
=UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
74 xDoc
.getText().setString("Please select something in this text and press then \"return\" in the shell where you have started the example.\n");
76 // ensure that the document content is optimal visible
77 com
.sun
.star
.frame
.XModel xModel
=
78 UnoRuntime
.queryInterface(
79 com
.sun
.star
.frame
.XModel
.class, xDoc
);
81 com
.sun
.star
.view
.XViewSettingsSupplier xViewSettings
=
82 UnoRuntime
.queryInterface(
83 com
.sun
.star
.view
.XViewSettingsSupplier
.class, xModel
.getCurrentController());
84 xViewSettings
.getViewSettings().setPropertyValue(
85 "ZoomType", Short
.valueOf((short)0));
87 // test document will be closed later
89 System
.out
.println("\nPlease select something in the test document and press then \"return\" to continues the example ...");
92 c
= (char) System
.in
.read();
93 }while ((c
!= 13) && (c
!= 10));
95 // Getting the current frame from the OpenOffice.org Server.
96 com
.sun
.star
.frame
.XFrame xframe
= xDesktop
.getCurrentFrame();
98 // Getting the controller.
99 com
.sun
.star
.frame
.XController xController
= xframe
.getController();
101 com
.sun
.star
.view
.XSelectionSupplier xSelSupplier
=
102 UnoRuntime
.queryInterface(
103 com
.sun
.star
.view
.XSelectionSupplier
.class, xController
);
105 Object oSelection
= xSelSupplier
.getSelection();
107 com
.sun
.star
.lang
.XServiceInfo xServInfo
=
108 UnoRuntime
.queryInterface(
109 com
.sun
.star
.lang
.XServiceInfo
.class, oSelection
);
111 if ( xServInfo
.supportsService("com.sun.star.text.TextRanges") )
113 com
.sun
.star
.container
.XIndexAccess xIndexAccess
=
114 UnoRuntime
.queryInterface(
115 com
.sun
.star
.container
.XIndexAccess
.class, oSelection
);
117 int count
= xIndexAccess
.getCount();
118 com
.sun
.star
.text
.XTextRange xTextRange
= null;
119 for ( int i
= 0; i
< count
; i
++ ) {
120 xTextRange
= UnoRuntime
.queryInterface(
121 com
.sun
.star
.text
.XTextRange
.class,
122 xIndexAccess
.getByIndex(i
));
124 System
.out
.println( "You have selected a text range: \""
125 + xTextRange
.getString() + "\"." );
129 if ( xServInfo
.supportsService("com.sun.star.text.TextGraphicObject") )
131 System
.out
.println( "You have selected a graphics." );
134 if ( xServInfo
.supportsService("com.sun.star.text.TextTableCursor") )
136 System
.out
.println( "You have selected a text table." );
140 // close test document
141 com
.sun
.star
.util
.XCloseable xCloseable
= UnoRuntime
.queryInterface(com
.sun
.star
.util
.XCloseable
.class,
144 if (xCloseable
!= null ) {
145 xCloseable
.close(false);
148 xComponent
.dispose();
153 catch( Exception e
) {
154 e
.printStackTrace(System
.err
);