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