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
.uno
.XComponentContext
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
37 import com
.sun
.star
.frame
.Desktop
;
38 import com
.sun
.star
.frame
.XDesktop2
;
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
.SystemClipboard
;
44 import com
.sun
.star
.datatransfer
.clipboard
.XSystemClipboard
;
45 import com
.sun
.star
.text
.XTextDocument
;
46 import com
.sun
.star
.uno
.AnyConverter
;
49 // Demonstrates the usage of the clipboard service
52 public class Clipboard
54 public static void main(String
[] args
)
58 // get the remote office context. If necessary a new office
60 XComponentContext xOfficeContext
=
61 com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
62 System
.out
.println("Connected to a running office ...");
64 // create a new test document
65 XDesktop2 xDesktop
= Desktop
.create(xOfficeContext
);
66 com
.sun
.star
.lang
.XComponent xComponent
=
67 xDesktop
.loadComponentFromURL("private:factory/swriter",
68 "_blank", 0, new com
.sun
.star
.beans
.PropertyValue
[0]);
70 XTextDocument xDoc
=UnoRuntime
.queryInterface(XTextDocument
.class, xComponent
);
71 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 = ");
73 // ensure that the document content is optimal visible
74 com
.sun
.star
.frame
.XModel xModel
=
75 UnoRuntime
.queryInterface(
76 com
.sun
.star
.frame
.XModel
.class, xDoc
);
77 // get the frame for later usage
78 com
.sun
.star
.frame
.XFrame xFrame
=
79 xModel
.getCurrentController().getFrame();
81 com
.sun
.star
.view
.XViewSettingsSupplier xViewSettings
=
82 UnoRuntime
.queryInterface(
83 com
.sun
.star
.view
.XViewSettingsSupplier
.class,
84 xModel
.getCurrentController());
85 xViewSettings
.getViewSettings().setPropertyValue(
86 "ZoomType", Short
.valueOf((short)0));
88 // test document will be closed later
90 XSystemClipboard xClipboard
= SystemClipboard
.create(xOfficeContext
);
93 // registering as clipboard listener
96 ClipboardListener aClipListener
= new ClipboardListener();
98 xClipboard
.addClipboardListener(aClipListener
);
101 readClipBoard(xClipboard
);
104 // becoming a clipboard owner
107 System
.out
.println("Becoming a clipboard owner...");
108 System
.out
.println("");
110 ClipboardOwner aClipOwner
= new ClipboardOwner();
111 xClipboard
.setContents(new TextTransferable("Hello World!"), aClipOwner
);
114 while (aClipOwner
.isClipboardOwner())
118 System
.out
.println("Change clipboard ownership by putting something into the clipboard!\n");
119 System
.out
.print("Still clipboard owner...");
121 System
.out
.println("Still clipboard owner...");
125 System
.out
.print(".");
130 // Read ClipBoard again
131 readClipBoard(xClipboard
);
134 // unregistering as clipboard listener
136 xClipboard
.removeClipboardListener(aClipListener
);
138 // close test document
139 com
.sun
.star
.util
.XCloseable xCloseable
= UnoRuntime
.queryInterface(com
.sun
.star
.util
.XCloseable
.class,
142 if (xCloseable
!= null ) {
143 xCloseable
.close(false);
146 xComponent
.dispose();
151 catch( Exception ex
)
153 ex
.printStackTrace();
157 public static void readClipBoard(XClipboard xClipboard
)
158 throws java
.lang
.Exception
161 // get a list of formats currently on the clipboard
164 XTransferable xTransferable
= xClipboard
.getContents();
166 DataFlavor
[] aDflvArr
= xTransferable
.getTransferDataFlavors();
168 // print all available formats
170 System
.out
.println("Reading the clipboard...");
171 System
.out
.println("Available clipboard formats:");
173 DataFlavor aUniFlv
= null;
175 for (int i
=0;i
<aDflvArr
.length
;i
++)
177 System
.out
.println( "MimeType: " +
178 aDflvArr
[i
].MimeType
+
179 " HumanPresentableName: " +
180 aDflvArr
[i
].HumanPresentableName
);
182 // if there is the format unicode text on the clipboard save the
183 // corresponding DataFlavor so that we can later output the string
185 if ( aDflvArr
[i
].MimeType
.equals("text/plain;charset=utf-16") )
187 aUniFlv
= aDflvArr
[i
];
191 System
.out
.println("");
197 System
.out
.print("Unicode text on the clipboard ...\nYour selected text \"");
198 Object aData
= xTransferable
.getTransferData(aUniFlv
);
199 System
.out
.println(AnyConverter
.toString(aData
)
200 + "\" is now in the clipboard.\n");
203 catch( UnsupportedFlavorException ex
)
205 System
.err
.println( "Requested format is not available on the clipboard!" );
206 ex
.printStackTrace();