Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / Clipboard / Clipboard.java
blobc100be7c7c3efdf096f8878437f40615b1a2c405
1 /*************************************************************************
3 * $RCSfile: Clipboard.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:36:21 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
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
17 * are met:
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.lang.XComponent;
42 import com.sun.star.lang.XMultiComponentFactory;
43 import com.sun.star.uno.XComponentContext;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.frame.XComponentLoader;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.beans.XPropertySet;
48 import com.sun.star.datatransfer.DataFlavor;
49 import com.sun.star.datatransfer.UnsupportedFlavorException;
50 import com.sun.star.datatransfer.XTransferable;
51 import com.sun.star.datatransfer.clipboard.XClipboard;
52 import com.sun.star.datatransfer.clipboard.XClipboardNotifier;
53 import com.sun.star.text.XTextDocument;
54 import com.sun.star.uno.AnyConverter;
56 //-------------------------------------------------
57 // Demonstrates the usage of the clipboard service
58 //-------------------------------------------------
60 public class Clipboard
62 public static void main(String[] args)
64 try
66 // get the remote office context. If necessary a new office
67 // process is started
68 XComponentContext xOfficeContext =
69 com.sun.star.comp.helper.Bootstrap.bootstrap();
70 System.out.println("Connected to a running office ...");
71 // get the service manager from the office context
72 XMultiComponentFactory xServiceManager =
73 xOfficeContext.getServiceManager();
75 // create a new test document
76 Object oDesktop = xServiceManager.createInstanceWithContext(
77 "com.sun.star.frame.Desktop", xOfficeContext);
79 XComponentLoader xCompLoader =(XComponentLoader)
80 UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
82 com.sun.star.lang.XComponent xComponent =
83 xCompLoader.loadComponentFromURL("private:factory/swriter",
84 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
86 XTextDocument xDoc =(XTextDocument)
87 UnoRuntime.queryInterface(XTextDocument.class, xComponent);
88 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 = ");
90 // ensure that the document content is optimal visible
91 com.sun.star.frame.XModel xModel =
92 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
93 com.sun.star.frame.XModel.class, xDoc);
94 // get the frame for later usage
95 com.sun.star.frame.XFrame xFrame =
96 xModel.getCurrentController().getFrame();
98 com.sun.star.view.XViewSettingsSupplier xViewSettings =
99 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface(
100 com.sun.star.view.XViewSettingsSupplier.class,
101 xModel.getCurrentController());
102 xViewSettings.getViewSettings().setPropertyValue(
103 "ZoomType", new Short((short)0));
105 // test document will be closed later
107 Object oClipboard = xServiceManager.createInstanceWithContext(
108 "com.sun.star.datatransfer.clipboard.SystemClipboard",
109 xOfficeContext);
111 XClipboard xClipboard = (XClipboard)
112 UnoRuntime.queryInterface(XClipboard.class, oClipboard);
114 //---------------------------------------------------
115 // registering as clipboard listener
116 //---------------------------------------------------
118 XClipboardNotifier xClipNotifier = (XClipboardNotifier)
119 UnoRuntime.queryInterface(XClipboardNotifier.class, oClipboard);
121 ClipboardListener aClipListener= new ClipboardListener();
123 xClipNotifier.addClipboardListener(aClipListener);
125 // Read ClipBoard
126 readClipBoard(xClipboard);
128 //---------------------------------------------------
129 // becoming a clipboard owner
130 //---------------------------------------------------
132 System.out.println("Becoming a clipboard owner...");
133 System.out.println("");
135 ClipboardOwner aClipOwner = new ClipboardOwner();
136 xClipboard.setContents(new TextTransferable("Hello World!"), aClipOwner);
137 int iFirst = 0;
139 while (aClipOwner.isClipboardOwner())
141 if (iFirst != 2) {
142 if (iFirst == 1) {
143 System.out.println("Change clipboard ownership by putting something into the clipboard!\n");
144 System.out.print("Still clipboard owner...");
145 } else {
146 System.out.println("Still clipboard owner...");
148 ++iFirst;
149 } else {
150 System.out.print(".");
152 Thread.sleep(1000);
155 // Read ClipBoard again
156 readClipBoard(xClipboard);
158 //---------------------------------------------------
159 // unregistering as clipboard listener
160 //---------------------------------------------------
161 xClipNotifier.removeClipboardListener(aClipListener);
163 // close test document
164 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
165 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
166 xComponent );
168 if (xCloseable != null ) {
169 xCloseable.close(false);
170 } else
172 xComponent.dispose();
175 System.exit(0);
177 catch( Exception ex )
179 ex.printStackTrace();
183 public static void readClipBoard(XClipboard xClipboard)
184 throws java.lang.Exception
186 //---------------------------------------------------
187 // get a list of formats currently on the clipboard
188 //---------------------------------------------------
190 XTransferable xTransferable = xClipboard.getContents();
192 DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors();
194 // print all available formats
196 System.out.println("Reading the clipboard...");
197 System.out.println("Available clipboard formats:");
199 DataFlavor aUniFlv = null;
201 for (int i=0;i<aDflvArr.length;i++)
203 System.out.println( "MimeType: " +
204 aDflvArr[i].MimeType +
205 " HumanPresentableName: " +
206 aDflvArr[i].HumanPresentableName );
208 // if there is the format unicode text on the clipboard save the
209 // corresponding DataFlavor so that we can later output the string
211 if ( aDflvArr[i].MimeType.equals("text/plain;charset=utf-16") )
213 aUniFlv = aDflvArr[i];
217 System.out.println("");
219 try
221 if (aUniFlv != null)
223 System.out.print("Unicode text on the clipboard ...\nYour selected text \"");
224 Object aData = xTransferable.getTransferData(aUniFlv);
225 System.out.println(AnyConverter.toString(aData)
226 + "\" is now in the clipboard.\n");
229 catch( UnsupportedFlavorException ex )
231 System.err.println( "Requested format is not available on the clipboard!" );
232 ex.printStackTrace();