1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 20 * 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. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.uno.UnoRuntime; 36 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, ...) 40 */ 41 public class WriterSelector { 42 /** 43 * @param args No arguments. 44 */ 45 public static void main(String args[]) { 46 com.sun.star.uno.XComponentContext xContext = null; 47 48 try { 49 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 ..."); 54 55 // get the remote office service manager 56 com.sun.star.lang.XMultiComponentFactory xMCF = 57 xContext.getServiceManager(); 58 59 // get a new instance of the desktop 60 com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop) 61 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, 62 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", 63 xContext ) ); 64 65 com.sun.star.frame.XComponentLoader xCompLoader = 66 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface( 67 com.sun.star.frame.XComponentLoader.class, xDesktop); 68 69 com.sun.star.lang.XComponent xComponent = 70 xCompLoader.loadComponentFromURL("private:factory/swriter", 71 "_blank", 0, new com.sun.star.beans.PropertyValue[0]); 72 { 73 com.sun.star.text.XTextDocument xDoc =(com.sun.star.text.XTextDocument) 74 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, 75 xComponent); 76 xDoc.getText().setString("Please select something in this text and press then \"return\" in the shell where you have started the example.\n"); 77 78 // ensure that the document content is optimal visible 79 com.sun.star.frame.XModel xModel = 80 (com.sun.star.frame.XModel)UnoRuntime.queryInterface( 81 com.sun.star.frame.XModel.class, xDoc); 82 83 com.sun.star.view.XViewSettingsSupplier xViewSettings = 84 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface( 85 com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController()); 86 xViewSettings.getViewSettings().setPropertyValue( 87 "ZoomType", new Short((short)0)); 88 } 89 // test document will be closed later 90 91 System.out.println("\nPlease select something in the test document and press then \"return\" to continues the example ..."); 92 char c = 'X'; 93 do{ 94 c = (char) System.in.read(); 95 }while ((c != 13) && (c != 10)); 96 97 // Getting the current frame from the OpenOffice.org Server. 98 com.sun.star.frame.XFrame xframe = xDesktop.getCurrentFrame(); 99 100 // Getting the controller. 101 com.sun.star.frame.XController xController = xframe.getController(); 102 103 com.sun.star.view.XSelectionSupplier xSelSupplier = 104 (com.sun.star.view.XSelectionSupplier)UnoRuntime.queryInterface( 105 com.sun.star.view.XSelectionSupplier.class, xController ); 106 107 Object oSelection = xSelSupplier.getSelection(); 108 109 com.sun.star.lang.XServiceInfo xServInfo = 110 (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface( 111 com.sun.star.lang.XServiceInfo.class, oSelection ); 112 113 if ( xServInfo.supportsService("com.sun.star.text.TextRanges") ) 114 { 115 com.sun.star.container.XIndexAccess xIndexAccess = 116 (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface( 117 com.sun.star.container.XIndexAccess.class, oSelection); 118 119 int count = xIndexAccess.getCount(); 120 com.sun.star.text.XTextRange xTextRange = null; 121 for ( int i = 0; i < count; i++ ) { 122 xTextRange = (com.sun.star.text.XTextRange) 123 UnoRuntime.queryInterface( 124 com.sun.star.text.XTextRange.class, 125 xIndexAccess.getByIndex(i)); 126 127 System.out.println( "You have selected a text range: \"" 128 + xTextRange.getString() + "\"." ); 129 } 130 } 131 132 if ( xServInfo.supportsService("com.sun.star.text.TextGraphicObject") ) 133 { 134 System.out.println( "You have selected a graphics." ); 135 } 136 137 if ( xServInfo.supportsService("com.sun.star.text.TextTableCursor") ) 138 { 139 System.out.println( "You have selected a text table." ); 140 } 141 142 143 // close test document 144 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) 145 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, 146 xComponent ); 147 148 if (xCloseable != null ) { 149 xCloseable.close(false); 150 } else 151 { 152 xComponent.dispose(); 153 } 154 155 System.exit(0); 156 } 157 catch( Exception e ) { 158 e.printStackTrace(System.err); 159 System.exit(1); 160 } 161 } 162 } 163