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