1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 *************************************************************************/
35 import com
.sun
.star
.lang
.XMultiComponentFactory
;
36 import com
.sun
.star
.uno
.XComponentContext
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.frame
.XComponentLoader
;
39 import com
.sun
.star
.datatransfer
.DataFlavor
;
40 import com
.sun
.star
.datatransfer
.UnsupportedFlavorException
;
41 import com
.sun
.star
.datatransfer
.XTransferable
;
42 import com
.sun
.star
.datatransfer
.clipboard
.XClipboard
;
43 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardNotifier
;
44 import com
.sun
.star
.text
.XTextDocument
;
45 import com
.sun
.star
.uno
.AnyConverter
;
47 //-------------------------------------------------
48 // Demonstrates the usage of the clipboard service
49 //-------------------------------------------------
51 public class Clipboard
53 public static void main(String
[] args
)
57 // get the remote office context. If necessary a new office
59 XComponentContext xOfficeContext
=
60 com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
61 System
.out
.println("Connected to a running office ...");
62 // get the service manager from the office context
63 XMultiComponentFactory xServiceManager
=
64 xOfficeContext
.getServiceManager();
66 // create a new test document
67 Object oDesktop
= xServiceManager
.createInstanceWithContext(
68 "com.sun.star.frame.Desktop", xOfficeContext
);
70 XComponentLoader xCompLoader
=UnoRuntime
.queryInterface(XComponentLoader
.class, oDesktop
);
72 com
.sun
.star
.lang
.XComponent xComponent
=
73 xCompLoader
.loadComponentFromURL("private:factory/swriter",
74 "_blank", 0, new com
.sun
.star
.beans
.PropertyValue
[0]);
76 XTextDocument xDoc
=UnoRuntime
.queryInterface(XTextDocument
.class, xComponent
);
77 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 = ");
79 // ensure that the document content is optimal visible
80 com
.sun
.star
.frame
.XModel xModel
=
81 UnoRuntime
.queryInterface(
82 com
.sun
.star
.frame
.XModel
.class, xDoc
);
83 // get the frame for later usage
84 com
.sun
.star
.frame
.XFrame xFrame
=
85 xModel
.getCurrentController().getFrame();
87 com
.sun
.star
.view
.XViewSettingsSupplier xViewSettings
=
88 UnoRuntime
.queryInterface(
89 com
.sun
.star
.view
.XViewSettingsSupplier
.class,
90 xModel
.getCurrentController());
91 xViewSettings
.getViewSettings().setPropertyValue(
92 "ZoomType", new Short((short)0));
94 // test document will be closed later
96 Object oClipboard
= xServiceManager
.createInstanceWithContext(
97 "com.sun.star.datatransfer.clipboard.SystemClipboard",
100 XClipboard xClipboard
= UnoRuntime
.queryInterface(XClipboard
.class, oClipboard
);
102 //---------------------------------------------------
103 // registering as clipboard listener
104 //---------------------------------------------------
106 XClipboardNotifier xClipNotifier
= UnoRuntime
.queryInterface(XClipboardNotifier
.class, oClipboard
);
108 ClipboardListener aClipListener
= new ClipboardListener();
110 xClipNotifier
.addClipboardListener(aClipListener
);
113 readClipBoard(xClipboard
);
115 //---------------------------------------------------
116 // becoming a clipboard owner
117 //---------------------------------------------------
119 System
.out
.println("Becoming a clipboard owner...");
120 System
.out
.println("");
122 ClipboardOwner aClipOwner
= new ClipboardOwner();
123 xClipboard
.setContents(new TextTransferable("Hello World!"), aClipOwner
);
126 while (aClipOwner
.isClipboardOwner())
130 System
.out
.println("Change clipboard ownership by putting something into the clipboard!\n");
131 System
.out
.print("Still clipboard owner...");
133 System
.out
.println("Still clipboard owner...");
137 System
.out
.print(".");
142 // Read ClipBoard again
143 readClipBoard(xClipboard
);
145 //---------------------------------------------------
146 // unregistering as clipboard listener
147 //---------------------------------------------------
148 xClipNotifier
.removeClipboardListener(aClipListener
);
150 // close test document
151 com
.sun
.star
.util
.XCloseable xCloseable
= UnoRuntime
.queryInterface(com
.sun
.star
.util
.XCloseable
.class,
154 if (xCloseable
!= null ) {
155 xCloseable
.close(false);
158 xComponent
.dispose();
163 catch( Exception ex
)
165 ex
.printStackTrace();
169 public static void readClipBoard(XClipboard xClipboard
)
170 throws java
.lang
.Exception
172 //---------------------------------------------------
173 // get a list of formats currently on the clipboard
174 //---------------------------------------------------
176 XTransferable xTransferable
= xClipboard
.getContents();
178 DataFlavor
[] aDflvArr
= xTransferable
.getTransferDataFlavors();
180 // print all available formats
182 System
.out
.println("Reading the clipboard...");
183 System
.out
.println("Available clipboard formats:");
185 DataFlavor aUniFlv
= null;
187 for (int i
=0;i
<aDflvArr
.length
;i
++)
189 System
.out
.println( "MimeType: " +
190 aDflvArr
[i
].MimeType
+
191 " HumanPresentableName: " +
192 aDflvArr
[i
].HumanPresentableName
);
194 // if there is the format unicode text on the clipboard save the
195 // corresponding DataFlavor so that we can later output the string
197 if ( aDflvArr
[i
].MimeType
.equals("text/plain;charset=utf-16") )
199 aUniFlv
= aDflvArr
[i
];
203 System
.out
.println("");
209 System
.out
.print("Unicode text on the clipboard ...\nYour selected text \"");
210 Object aData
= xTransferable
.getTransferData(aUniFlv
);
211 System
.out
.println(AnyConverter
.toString(aData
)
212 + "\" is now in the clipboard.\n");
215 catch( UnsupportedFlavorException ex
)
217 System
.err
.println( "Requested format is not available on the clipboard!" );
218 ex
.printStackTrace();