| 1 |
/* ClipboardContent.java |
| 2 |
Copyright (C) 2012 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
|
| 38 |
import java.awt.Toolkit; |
| 39 |
import java.awt.datatransfer.Clipboard; |
| 40 |
import java.awt.datatransfer.DataFlavor; |
| 41 |
import java.awt.datatransfer.StringSelection; |
| 42 |
import java.awt.datatransfer.Transferable; |
| 43 |
import java.awt.datatransfer.UnsupportedFlavorException; |
| 44 |
import java.io.IOException; |
| 45 |
import java.lang.reflect.InvocationTargetException; |
| 46 |
import java.util.concurrent.TimeUnit; |
| 47 |
import javax.swing.JFrame; |
| 48 |
import javax.swing.JPanel; |
| 49 |
import javax.swing.JTextField; |
| 50 |
import javax.swing.SwingUtilities; |
| 51 |
import javax.swing.text.JTextComponent; |
| 52 |
|
| 53 |
public class ClipboardContent extends JPanel { |
| 54 |
|
| 55 |
private static final String contentC = "COPY#$REPRODUCER"; |
| 56 |
private static final String contentP = "PASTE#$REPRODUCER"; |
| 57 |
|
| 58 |
private static class LocalFrame extends JFrame { |
| 59 |
|
| 60 |
JTextField t; |
| 61 |
|
| 62 |
public LocalFrame(String str) { |
| 63 |
super(); |
| 64 |
t = new JTextField(str); |
| 65 |
this.add(t); |
| 66 |
this.setSize(100, 100); |
| 67 |
this.pack(); |
| 68 |
t.selectAll(); |
| 69 |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 70 |
} |
| 71 |
|
| 72 |
public void run() throws InterruptedException { |
| 73 |
SwingUtilities.invokeLater(new Runnable() { |
| 74 |
|
| 75 |
@Override |
| 76 |
public void run() { |
| 77 |
setVisible(true); |
| 78 |
|
| 79 |
} |
| 80 |
}); |
| 81 |
while (!this.isVisible()) { |
| 82 |
Thread.sleep(100); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public JTextField getT() { |
| 87 |
return t; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void putToClipboard1(String str) { |
| 92 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 93 |
Clipboard clipboard = toolkit.getSystemClipboard(); |
| 94 |
StringSelection strSel = new StringSelection(str); |
| 95 |
clipboard.setContents(strSel, null); |
| 96 |
printFlavors(); |
| 97 |
} |
| 98 |
|
| 99 |
public void putToClipboard2(final String str) throws InterruptedException, NoSuchMethodException, IllegalAccessException, UnsupportedFlavorException, IllegalArgumentException, InvocationTargetException, IOException { |
| 100 |
final LocalFrame lf = new LocalFrame(str); |
| 101 |
lf.run(); |
| 102 |
((JTextComponent) (lf.getT())).copy(); |
| 103 |
printFlavors(); |
| 104 |
lf.dispose(); |
| 105 |
} |
| 106 |
|
| 107 |
public String pasteFromClipboard2() throws InterruptedException, NoSuchMethodException, IllegalAccessException, UnsupportedFlavorException, IllegalArgumentException, InvocationTargetException, IOException { |
| 108 |
final LocalFrame lf = new LocalFrame("xxx"); |
| 109 |
lf.run(); |
| 110 |
((JTextComponent) (lf.getT())).paste(); |
| 111 |
printFlavors(); |
| 112 |
String s = lf.getT().getText(); |
| 113 |
lf.dispose(); |
| 114 |
return s; |
| 115 |
} |
| 116 |
|
| 117 |
private void printFlavors() { |
| 118 |
//just for debugging |
| 119 |
// Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 120 |
// Clipboard clipboard = toolkit.getSystemClipboard(); |
| 121 |
// Transferable clipData = clipboard.getContents(clipboard); |
| 122 |
// DataFlavor[] cd = clipData.getTransferDataFlavors(); |
| 123 |
// for (DataFlavor dataFlavor : cd) { |
| 124 |
// System.out.println(dataFlavor.getMimeType()); |
| 125 |
// } |
| 126 |
} |
| 127 |
|
| 128 |
public String pasteFromClipboard1() throws UnsupportedFlavorException, IOException { |
| 129 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 130 |
Clipboard clipboard = toolkit.getSystemClipboard(); |
| 131 |
Transferable clipData = clipboard.getContents(clipboard); |
| 132 |
printFlavors(); |
| 133 |
String s = (String) (clipData.getTransferData( |
| 134 |
DataFlavor.stringFlavor)); |
| 135 |
return s; |
| 136 |
} |
| 137 |
|
| 138 |
public static void main(String[] args) throws Exception { |
| 139 |
ClipboardContent cl = new ClipboardContent(); |
| 140 |
if (args.length == 0) { |
| 141 |
throw new IllegalArgumentException("at least copy1|2 or paste1|2 must be as argument (+mandatory number giving use timeout in seconds before termination)"); |
| 142 |
} else if (args.length == 1) { |
| 143 |
cl.proceed(args[0]); |
| 144 |
} else { |
| 145 |
cl.proceed(args[0], args[1]); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
public void proceed(String arg) throws Exception { |
| 151 |
proceed(arg, 0); |
| 152 |
} |
| 153 |
|
| 154 |
public void proceed(String arg, String keepAliveFor) throws Exception { |
| 155 |
proceed(arg, Long.valueOf(keepAliveFor)); |
| 156 |
} |
| 157 |
|
| 158 |
public void proceed(String arg, long timeOut) throws Exception { |
| 159 |
if (arg.equals("copy1")) { |
| 160 |
System.out.println(this.getClass().getName() + " copying1 to clipboard " + contentC); |
| 161 |
putToClipboard1(contentC); |
| 162 |
System.out.println(this.getClass().getName() + " copied1 to clipboard " + pasteFromClipboard1()); |
| 163 |
} else if (arg.equals("paste1")) { |
| 164 |
System.out.println(this.getClass().getName() + " pasting1 from clipboard "); |
| 165 |
String nwContent = pasteFromClipboard1(); |
| 166 |
System.out.println(this.getClass().getName() + " pasted1 from clipboard " + nwContent); |
| 167 |
} else if (arg.equals("copy2")) { |
| 168 |
System.out.println(this.getClass().getName() + " copying2 to clipboard " + contentC); |
| 169 |
putToClipboard2(contentC); |
| 170 |
System.out.println(this.getClass().getName() + " copied2 to clipboard " + pasteFromClipboard2()); |
| 171 |
} else if (arg.equals("paste2")) { |
| 172 |
System.out.println(this.getClass().getName() + " pasting2 from clipboard "); |
| 173 |
String nwContent = pasteFromClipboard2(); |
| 174 |
System.out.println(this.getClass().getName() + " pasted2 from clipboard " + nwContent); |
| 175 |
} else { |
| 176 |
throw new IllegalArgumentException("supported copy1|2 paste1|2"); |
| 177 |
} |
| 178 |
long start = System.nanoTime(); |
| 179 |
while (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < timeOut) { |
| 180 |
Thread.sleep(500); |
| 181 |
} |
| 182 |
} |
| 183 |
} |