1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 36*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 37*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 39*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 40*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 41*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 42*cdf0e10cSrcweir import com.sun.star.datatransfer.DataFlavor; 43*cdf0e10cSrcweir import com.sun.star.datatransfer.UnsupportedFlavorException; 44*cdf0e10cSrcweir import com.sun.star.datatransfer.XTransferable; 45*cdf0e10cSrcweir import com.sun.star.datatransfer.clipboard.XClipboard; 46*cdf0e10cSrcweir import com.sun.star.datatransfer.clipboard.XClipboardNotifier; 47*cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 48*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir //------------------------------------------------- 51*cdf0e10cSrcweir // Demonstrates the usage of the clipboard service 52*cdf0e10cSrcweir //------------------------------------------------- 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir public class Clipboard 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir public static void main(String[] args) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir try 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir // get the remote office context. If necessary a new office 61*cdf0e10cSrcweir // process is started 62*cdf0e10cSrcweir XComponentContext xOfficeContext = 63*cdf0e10cSrcweir com.sun.star.comp.helper.Bootstrap.bootstrap(); 64*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 65*cdf0e10cSrcweir // get the service manager from the office context 66*cdf0e10cSrcweir XMultiComponentFactory xServiceManager = 67*cdf0e10cSrcweir xOfficeContext.getServiceManager(); 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // create a new test document 70*cdf0e10cSrcweir Object oDesktop = xServiceManager.createInstanceWithContext( 71*cdf0e10cSrcweir "com.sun.star.frame.Desktop", xOfficeContext); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir XComponentLoader xCompLoader =(XComponentLoader) 74*cdf0e10cSrcweir UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir com.sun.star.lang.XComponent xComponent = 77*cdf0e10cSrcweir xCompLoader.loadComponentFromURL("private:factory/swriter", 78*cdf0e10cSrcweir "_blank", 0, new com.sun.star.beans.PropertyValue[0]); 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir XTextDocument xDoc =(XTextDocument) 81*cdf0e10cSrcweir UnoRuntime.queryInterface(XTextDocument.class, xComponent); 82*cdf0e10cSrcweir 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 = "); 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir // ensure that the document content is optimal visible 85*cdf0e10cSrcweir com.sun.star.frame.XModel xModel = 86*cdf0e10cSrcweir (com.sun.star.frame.XModel)UnoRuntime.queryInterface( 87*cdf0e10cSrcweir com.sun.star.frame.XModel.class, xDoc); 88*cdf0e10cSrcweir // get the frame for later usage 89*cdf0e10cSrcweir com.sun.star.frame.XFrame xFrame = 90*cdf0e10cSrcweir xModel.getCurrentController().getFrame(); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir com.sun.star.view.XViewSettingsSupplier xViewSettings = 93*cdf0e10cSrcweir (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface( 94*cdf0e10cSrcweir com.sun.star.view.XViewSettingsSupplier.class, 95*cdf0e10cSrcweir xModel.getCurrentController()); 96*cdf0e10cSrcweir xViewSettings.getViewSettings().setPropertyValue( 97*cdf0e10cSrcweir "ZoomType", new Short((short)0)); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir // test document will be closed later 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir Object oClipboard = xServiceManager.createInstanceWithContext( 102*cdf0e10cSrcweir "com.sun.star.datatransfer.clipboard.SystemClipboard", 103*cdf0e10cSrcweir xOfficeContext); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir XClipboard xClipboard = (XClipboard) 106*cdf0e10cSrcweir UnoRuntime.queryInterface(XClipboard.class, oClipboard); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir //--------------------------------------------------- 109*cdf0e10cSrcweir // registering as clipboard listener 110*cdf0e10cSrcweir //--------------------------------------------------- 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir XClipboardNotifier xClipNotifier = (XClipboardNotifier) 113*cdf0e10cSrcweir UnoRuntime.queryInterface(XClipboardNotifier.class, oClipboard); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir ClipboardListener aClipListener= new ClipboardListener(); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir xClipNotifier.addClipboardListener(aClipListener); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir // Read ClipBoard 120*cdf0e10cSrcweir readClipBoard(xClipboard); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir //--------------------------------------------------- 123*cdf0e10cSrcweir // becoming a clipboard owner 124*cdf0e10cSrcweir //--------------------------------------------------- 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir System.out.println("Becoming a clipboard owner..."); 127*cdf0e10cSrcweir System.out.println(""); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir ClipboardOwner aClipOwner = new ClipboardOwner(); 130*cdf0e10cSrcweir xClipboard.setContents(new TextTransferable("Hello World!"), aClipOwner); 131*cdf0e10cSrcweir int iFirst = 0; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir while (aClipOwner.isClipboardOwner()) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir if (iFirst != 2) { 136*cdf0e10cSrcweir if (iFirst == 1) { 137*cdf0e10cSrcweir System.out.println("Change clipboard ownership by putting something into the clipboard!\n"); 138*cdf0e10cSrcweir System.out.print("Still clipboard owner..."); 139*cdf0e10cSrcweir } else { 140*cdf0e10cSrcweir System.out.println("Still clipboard owner..."); 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir ++iFirst; 143*cdf0e10cSrcweir } else { 144*cdf0e10cSrcweir System.out.print("."); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir Thread.sleep(1000); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // Read ClipBoard again 150*cdf0e10cSrcweir readClipBoard(xClipboard); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir //--------------------------------------------------- 153*cdf0e10cSrcweir // unregistering as clipboard listener 154*cdf0e10cSrcweir //--------------------------------------------------- 155*cdf0e10cSrcweir xClipNotifier.removeClipboardListener(aClipListener); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir // close test document 158*cdf0e10cSrcweir com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) 159*cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, 160*cdf0e10cSrcweir xComponent ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir if (xCloseable != null ) { 163*cdf0e10cSrcweir xCloseable.close(false); 164*cdf0e10cSrcweir } else 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir xComponent.dispose(); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir System.exit(0); 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir catch( Exception ex ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir ex.printStackTrace(); 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir public static void readClipBoard(XClipboard xClipboard) 178*cdf0e10cSrcweir throws java.lang.Exception 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir //--------------------------------------------------- 181*cdf0e10cSrcweir // get a list of formats currently on the clipboard 182*cdf0e10cSrcweir //--------------------------------------------------- 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir XTransferable xTransferable = xClipboard.getContents(); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors(); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir // print all available formats 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir System.out.println("Reading the clipboard..."); 191*cdf0e10cSrcweir System.out.println("Available clipboard formats:"); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir DataFlavor aUniFlv = null; 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir for (int i=0;i<aDflvArr.length;i++) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir System.out.println( "MimeType: " + 198*cdf0e10cSrcweir aDflvArr[i].MimeType + 199*cdf0e10cSrcweir " HumanPresentableName: " + 200*cdf0e10cSrcweir aDflvArr[i].HumanPresentableName ); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir // if there is the format unicode text on the clipboard save the 203*cdf0e10cSrcweir // corresponding DataFlavor so that we can later output the string 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir if ( aDflvArr[i].MimeType.equals("text/plain;charset=utf-16") ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir aUniFlv = aDflvArr[i]; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir System.out.println(""); 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir try 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir if (aUniFlv != null) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir System.out.print("Unicode text on the clipboard ...\nYour selected text \""); 218*cdf0e10cSrcweir Object aData = xTransferable.getTransferData(aUniFlv); 219*cdf0e10cSrcweir System.out.println(AnyConverter.toString(aData) 220*cdf0e10cSrcweir + "\" is now in the clipboard.\n"); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir catch( UnsupportedFlavorException ex ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir System.err.println( "Requested format is not available on the clipboard!" ); 226*cdf0e10cSrcweir ex.printStackTrace(); 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230