tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / Clipboard / java / Clipboard.java
blob742fed8ed4baf54fad4eca6aecb2f55f2f4e2523
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.frame.Desktop;
39 import com.sun.star.frame.XDesktop2;
40 import com.sun.star.datatransfer.DataFlavor;
41 import com.sun.star.datatransfer.UnsupportedFlavorException;
42 import com.sun.star.datatransfer.XTransferable;
43 import com.sun.star.datatransfer.clipboard.XClipboard;
44 import com.sun.star.datatransfer.clipboard.SystemClipboard;
45 import com.sun.star.datatransfer.clipboard.XSystemClipboard;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.AnyConverter;
50 // Demonstrates the usage of the clipboard service
53 public class Clipboard
55 public static void main(String[] args)
57 try
59 // get the remote office context. If necessary a new office
60 // process is started
61 XComponentContext xOfficeContext =
62 com.sun.star.comp.helper.Bootstrap.bootstrap();
63 System.out.println("Connected to a running office ...");
65 // create a new test document
66 XDesktop2 xDesktop = Desktop.create(xOfficeContext);
67 com.sun.star.lang.XComponent xComponent =
68 xDesktop.loadComponentFromURL("private:factory/swriter",
69 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
71 XTextDocument xDoc =UnoRuntime.queryInterface(XTextDocument.class, xComponent);
72 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 = ");
74 // ensure that the document content is optimal visible
75 com.sun.star.frame.XModel xModel =
76 UnoRuntime.queryInterface(
77 com.sun.star.frame.XModel.class, xDoc);
78 // get the frame for later usage
79 com.sun.star.frame.XFrame xFrame =
80 xModel.getCurrentController().getFrame();
82 com.sun.star.view.XViewSettingsSupplier xViewSettings =
83 UnoRuntime.queryInterface(
84 com.sun.star.view.XViewSettingsSupplier.class,
85 xModel.getCurrentController());
86 xViewSettings.getViewSettings().setPropertyValue(
87 "ZoomType", Short.valueOf((short)0));
89 // test document will be closed later
91 XSystemClipboard xClipboard = SystemClipboard.create(xOfficeContext);
94 // registering as clipboard listener
97 ClipboardListener aClipListener= new ClipboardListener();
99 xClipboard.addClipboardListener(aClipListener);
101 // Read ClipBoard
102 readClipBoard(xClipboard);
105 // becoming a clipboard owner
108 System.out.println("Becoming a clipboard owner...");
109 System.out.println("");
111 ClipboardOwner aClipOwner = new ClipboardOwner();
112 xClipboard.setContents(new TextTransferable("Hello World!"), aClipOwner);
113 int iFirst = 0;
115 while (aClipOwner.isClipboardOwner())
117 if (iFirst != 2) {
118 if (iFirst == 1) {
119 System.out.println("Change clipboard ownership by putting something into the clipboard!\n");
120 System.out.print("Still clipboard owner...");
121 } else {
122 System.out.println("Still clipboard owner...");
124 ++iFirst;
125 } else {
126 System.out.print(".");
128 Thread.sleep(1000);
131 // Read ClipBoard again
132 readClipBoard(xClipboard);
135 // unregistering as clipboard listener
137 xClipboard.removeClipboardListener(aClipListener);
139 // close test document
140 com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
141 xComponent );
143 if (xCloseable != null ) {
144 xCloseable.close(false);
145 } else
147 xComponent.dispose();
150 System.exit(0);
152 catch( Exception ex )
154 ex.printStackTrace();
158 public static void readClipBoard(XClipboard xClipboard)
159 throws java.lang.Exception
162 // get a list of formats currently on the clipboard
165 XTransferable xTransferable = xClipboard.getContents();
167 DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors();
169 // print all available formats
171 System.out.println("Reading the clipboard...");
172 System.out.println("Available clipboard formats:");
174 DataFlavor aUniFlv = null;
176 for (int i=0;i<aDflvArr.length;i++)
178 System.out.println( "MimeType: " +
179 aDflvArr[i].MimeType +
180 " HumanPresentableName: " +
181 aDflvArr[i].HumanPresentableName );
183 // if there is the format unicode text on the clipboard save the
184 // corresponding DataFlavor so that we can later output the string
186 if ( aDflvArr[i].MimeType.equals("text/plain;charset=utf-16") )
188 aUniFlv = aDflvArr[i];
192 System.out.println("");
196 if (aUniFlv != null)
198 System.out.print("Unicode text on the clipboard ...\nYour selected text \"");
199 Object aData = xTransferable.getTransferData(aUniFlv);
200 System.out.println(AnyConverter.toString(aData)
201 + "\" is now in the clipboard.\n");
204 catch( UnsupportedFlavorException ex )
206 System.err.println( "Requested format is not available on the clipboard!" );
207 ex.printStackTrace();
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */