1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 import com.sun.star.lang.XComponent; 25 import com.sun.star.lang.XMultiComponentFactory; 26 import com.sun.star.uno.XComponentContext; 27 import com.sun.star.uno.UnoRuntime; 28 import com.sun.star.frame.XComponentLoader; 29 import com.sun.star.beans.PropertyValue; 30 import com.sun.star.beans.XPropertySet; 31 import com.sun.star.datatransfer.DataFlavor; 32 import com.sun.star.datatransfer.UnsupportedFlavorException; 33 import com.sun.star.datatransfer.XTransferable; 34 import com.sun.star.datatransfer.clipboard.XClipboard; 35 import com.sun.star.datatransfer.clipboard.XClipboardNotifier; 36 import com.sun.star.text.XTextDocument; 37 import com.sun.star.uno.AnyConverter; 38 39 //------------------------------------------------- 40 // Demonstrates the usage of the clipboard service 41 //------------------------------------------------- 42 43 public class Clipboard 44 { main(String[] args)45 public static void main(String[] args) 46 { 47 try 48 { 49 // get the remote office context. If necessary a new office 50 // process is started 51 XComponentContext xOfficeContext = 52 com.sun.star.comp.helper.Bootstrap.bootstrap(); 53 System.out.println("Connected to a running office ..."); 54 // get the service manager from the office context 55 XMultiComponentFactory xServiceManager = 56 xOfficeContext.getServiceManager(); 57 58 // create a new test document 59 Object oDesktop = xServiceManager.createInstanceWithContext( 60 "com.sun.star.frame.Desktop", xOfficeContext); 61 62 XComponentLoader xCompLoader =(XComponentLoader) 63 UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 64 65 com.sun.star.lang.XComponent xComponent = 66 xCompLoader.loadComponentFromURL("private:factory/swriter", 67 "_blank", 0, new com.sun.star.beans.PropertyValue[0]); 68 { 69 XTextDocument xDoc =(XTextDocument) 70 UnoRuntime.queryInterface(XTextDocument.class, xComponent); 71 xDoc.getText().setString("In the first step, paste the current content of the clipboard in the document!\nThe text \"Hello world!\" shall be insert at the current cursor position below.\n\nIn the second step, please select some words and put it into the clipboard! ...\n\nCurrent clipboard content = "); 72 73 // ensure that the document content is optimal visible 74 com.sun.star.frame.XModel xModel = 75 (com.sun.star.frame.XModel)UnoRuntime.queryInterface( 76 com.sun.star.frame.XModel.class, xDoc); 77 // get the frame for later usage 78 com.sun.star.frame.XFrame xFrame = 79 xModel.getCurrentController().getFrame(); 80 81 com.sun.star.view.XViewSettingsSupplier xViewSettings = 82 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface( 83 com.sun.star.view.XViewSettingsSupplier.class, 84 xModel.getCurrentController()); 85 xViewSettings.getViewSettings().setPropertyValue( 86 "ZoomType", new Short((short)0)); 87 } 88 // test document will be closed later 89 90 Object oClipboard = xServiceManager.createInstanceWithContext( 91 "com.sun.star.datatransfer.clipboard.SystemClipboard", 92 xOfficeContext); 93 94 XClipboard xClipboard = (XClipboard) 95 UnoRuntime.queryInterface(XClipboard.class, oClipboard); 96 97 //--------------------------------------------------- 98 // registering as clipboard listener 99 //--------------------------------------------------- 100 101 XClipboardNotifier xClipNotifier = (XClipboardNotifier) 102 UnoRuntime.queryInterface(XClipboardNotifier.class, oClipboard); 103 104 ClipboardListener aClipListener= new ClipboardListener(); 105 106 xClipNotifier.addClipboardListener(aClipListener); 107 108 // Read ClipBoard 109 readClipBoard(xClipboard); 110 111 //--------------------------------------------------- 112 // becoming a clipboard owner 113 //--------------------------------------------------- 114 115 System.out.println("Becoming a clipboard owner..."); 116 System.out.println(""); 117 118 ClipboardOwner aClipOwner = new ClipboardOwner(); 119 xClipboard.setContents(new TextTransferable("Hello World!"), aClipOwner); 120 int iFirst = 0; 121 122 while (aClipOwner.isClipboardOwner()) 123 { 124 if (iFirst != 2) { 125 if (iFirst == 1) { 126 System.out.println("Change clipboard ownership by putting something into the clipboard!\n"); 127 System.out.print("Still clipboard owner..."); 128 } else { 129 System.out.println("Still clipboard owner..."); 130 } 131 ++iFirst; 132 } else { 133 System.out.print("."); 134 } 135 Thread.sleep(1000); 136 } 137 138 // Read ClipBoard again 139 readClipBoard(xClipboard); 140 141 //--------------------------------------------------- 142 // unregistering as clipboard listener 143 //--------------------------------------------------- 144 xClipNotifier.removeClipboardListener(aClipListener); 145 146 // close test document 147 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) 148 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, 149 xComponent ); 150 151 if (xCloseable != null ) { 152 xCloseable.close(false); 153 } else 154 { 155 xComponent.dispose(); 156 } 157 158 System.exit(0); 159 } 160 catch( Exception ex ) 161 { 162 ex.printStackTrace(); 163 } 164 } 165 readClipBoard(XClipboard xClipboard)166 public static void readClipBoard(XClipboard xClipboard) 167 throws java.lang.Exception 168 { 169 //--------------------------------------------------- 170 // get a list of formats currently on the clipboard 171 //--------------------------------------------------- 172 173 XTransferable xTransferable = xClipboard.getContents(); 174 175 DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors(); 176 177 // print all available formats 178 179 System.out.println("Reading the clipboard..."); 180 System.out.println("Available clipboard formats:"); 181 182 DataFlavor aUniFlv = null; 183 184 for (int i=0;i<aDflvArr.length;i++) 185 { 186 System.out.println( "MimeType: " + 187 aDflvArr[i].MimeType + 188 " HumanPresentableName: " + 189 aDflvArr[i].HumanPresentableName ); 190 191 // if there is the format unicode text on the clipboard save the 192 // corresponding DataFlavor so that we can later output the string 193 194 if ( aDflvArr[i].MimeType.equals("text/plain;charset=utf-16") ) 195 { 196 aUniFlv = aDflvArr[i]; 197 } 198 } 199 200 System.out.println(""); 201 202 try 203 { 204 if (aUniFlv != null) 205 { 206 System.out.print("Unicode text on the clipboard ...\nYour selected text \""); 207 Object aData = xTransferable.getTransferData(aUniFlv); 208 System.out.println(AnyConverter.toString(aData) 209 + "\" is now in the clipboard.\n"); 210 } 211 } 212 catch( UnsupportedFlavorException ex ) 213 { 214 System.err.println( "Requested format is not available on the clipboard!" ); 215 ex.printStackTrace(); 216 } 217 } 218 } 219