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