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.lang.XComponent;
36 import com.sun.star.lang.XMultiComponentFactory;
37 import com.sun.star.uno.XComponentContext;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.frame.XComponentLoader;
40 import com.sun.star.beans.PropertyValue;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.datatransfer.DataFlavor;
43 import com.sun.star.datatransfer.UnsupportedFlavorException;
44 import com.sun.star.datatransfer.XTransferable;
45 import com.sun.star.datatransfer.clipboard.XClipboard;
46 import com.sun.star.datatransfer.clipboard.XClipboardNotifier;
47 import com.sun.star.text.XTextDocument;
48 import com.sun.star.uno.AnyConverter;
49 
50 //-------------------------------------------------
51 // Demonstrates the usage of the clipboard service
52 //-------------------------------------------------
53 
54 public class Clipboard
55 {
56 	public static void main(String[] args)
57 	{
58 		try
59 		{
60             // get the remote office context. If necessary a new office
61             // process is started
62             XComponentContext xOfficeContext =
63                 com.sun.star.comp.helper.Bootstrap.bootstrap();
64             System.out.println("Connected to a running office ...");
65 			// get the service manager from the office context
66             XMultiComponentFactory xServiceManager =
67                 xOfficeContext.getServiceManager();
68 
69             // create a new test document
70             Object oDesktop = xServiceManager.createInstanceWithContext(
71                 "com.sun.star.frame.Desktop", xOfficeContext);
72 
73             XComponentLoader xCompLoader =(XComponentLoader)
74                 UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
75 
76             com.sun.star.lang.XComponent xComponent =
77                 xCompLoader.loadComponentFromURL("private:factory/swriter",
78                     "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
79             {
80             XTextDocument xDoc =(XTextDocument)
81                 UnoRuntime.queryInterface(XTextDocument.class, xComponent);
82             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 
84             // ensure that the document content is optimal visible
85             com.sun.star.frame.XModel xModel =
86                 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
87                     com.sun.star.frame.XModel.class, xDoc);
88             // get the frame for later usage
89             com.sun.star.frame.XFrame xFrame =
90                 xModel.getCurrentController().getFrame();
91 
92             com.sun.star.view.XViewSettingsSupplier xViewSettings =
93                 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface(
94                     com.sun.star.view.XViewSettingsSupplier.class,
95                     xModel.getCurrentController());
96             xViewSettings.getViewSettings().setPropertyValue(
97                 "ZoomType", new Short((short)0));
98             }
99             // test document will be closed later
100 
101 			Object oClipboard = xServiceManager.createInstanceWithContext(
102                 "com.sun.star.datatransfer.clipboard.SystemClipboard",
103                 xOfficeContext);
104 
105 			XClipboard xClipboard = (XClipboard)
106 				UnoRuntime.queryInterface(XClipboard.class, oClipboard);
107 
108 			//---------------------------------------------------
109 			// registering as clipboard listener
110 			//---------------------------------------------------
111 
112 			XClipboardNotifier xClipNotifier = (XClipboardNotifier)
113 				UnoRuntime.queryInterface(XClipboardNotifier.class, oClipboard);
114 
115 			ClipboardListener aClipListener= new ClipboardListener();
116 
117 			xClipNotifier.addClipboardListener(aClipListener);
118 
119             // Read ClipBoard
120             readClipBoard(xClipboard);
121 
122 			//---------------------------------------------------
123 			// becoming a clipboard owner
124 			//---------------------------------------------------
125 
126 			System.out.println("Becoming a clipboard owner...");
127 			System.out.println("");
128 
129 			ClipboardOwner aClipOwner = new ClipboardOwner();
130 			xClipboard.setContents(new TextTransferable("Hello World!"), aClipOwner);
131 			int iFirst = 0;
132 
133 			while (aClipOwner.isClipboardOwner())
134 			{
135 				if (iFirst != 2) {
136 					if (iFirst == 1) {
137 						System.out.println("Change clipboard ownership by putting something into the clipboard!\n");
138 						System.out.print("Still clipboard owner...");
139 					} else {
140 						System.out.println("Still clipboard owner...");
141 					}
142 					++iFirst;
143 				} else {
144 					System.out.print(".");
145 				}
146 				Thread.sleep(1000);
147 			}
148 
149             // Read ClipBoard again
150             readClipBoard(xClipboard);
151 
152 			//---------------------------------------------------
153 			// unregistering as clipboard listener
154 			//---------------------------------------------------
155 			xClipNotifier.removeClipboardListener(aClipListener);
156 
157             // close test document
158             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
159                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
160                                           xComponent );
161 
162             if (xCloseable != null ) {
163                 xCloseable.close(false);
164             } else
165             {
166                 xComponent.dispose();
167             }
168 
169 			System.exit(0);
170 		}
171 		catch( Exception ex )
172 		{
173             ex.printStackTrace();
174 		}
175 	}
176 
177     public static void readClipBoard(XClipboard xClipboard)
178         throws java.lang.Exception
179     {
180         //---------------------------------------------------
181         // get a list of formats currently on the clipboard
182         //---------------------------------------------------
183 
184         XTransferable xTransferable = xClipboard.getContents();
185 
186         DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors();
187 
188         // print all available formats
189 
190         System.out.println("Reading the clipboard...");
191         System.out.println("Available clipboard formats:");
192 
193         DataFlavor aUniFlv = null;
194 
195         for (int i=0;i<aDflvArr.length;i++)
196         {
197             System.out.println( "MimeType: " +
198                                 aDflvArr[i].MimeType +
199                                 " HumanPresentableName: " +
200                                 aDflvArr[i].HumanPresentableName );
201 
202             // if there is the format unicode text on the clipboard save the
203             // corresponding DataFlavor so that we can later output the string
204 
205             if ( aDflvArr[i].MimeType.equals("text/plain;charset=utf-16") )
206             {
207                 aUniFlv = aDflvArr[i];
208             }
209         }
210 
211         System.out.println("");
212 
213         try
214         {
215             if (aUniFlv != null)
216             {
217                 System.out.print("Unicode text on the clipboard ...\nYour selected text \"");
218                 Object aData = xTransferable.getTransferData(aUniFlv);
219                 System.out.println(AnyConverter.toString(aData)
220                                    + "\" is now in the clipboard.\n");
221             }
222         }
223         catch( UnsupportedFlavorException ex )
224         {
225             System.err.println( "Requested format is not available on the clipboard!" );
226             ex.printStackTrace();
227         }
228     }
229 }
230