Skip to content

Commit a3d8420

Browse files
authored
Merge pull request #177 from BobHanson/hanson1
JSUtilI.getClipboardText()
2 parents f63071c + 186fde1 commit a3d8420

File tree

10 files changed

+72
-7
lines changed

10 files changed

+72
-7
lines changed
-80 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200807063741
1+
20200813222737
-80 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200807063741
1+
20200813222737
-80 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.awt.Dimension;
66
import java.awt.JSComponent;
77
import java.awt.Toolkit;
8-
import java.awt.image.BufferedImage;
8+
import java.awt.datatransfer.DataFlavor;
9+
import java.awt.datatransfer.Transferable;
910
import java.io.BufferedInputStream;
1011
import java.io.BufferedReader;
1112
import java.io.ByteArrayInputStream;
@@ -21,19 +22,17 @@
2122
import java.util.Map;
2223
import java.util.Map.Entry;
2324
import java.util.Properties;
25+
import java.util.function.Consumer;
2426
import java.util.function.Function;
2527
import java.util.zip.ZipEntry;
2628
import java.util.zip.ZipInputStream;
2729

2830
import javax.sound.sampled.AudioFormat;
29-
import javax.sound.sampled.UnsupportedAudioFileException;
3031
import javax.swing.JComponent;
3132
import javax.swing.plaf.ComponentUI;
3233

33-
import swingjs.api.js.JSFunction;
3434
import javajs.util.AU;
3535
import javajs.util.AjaxURLConnection;
36-
import javajs.util.PT;
3736
import javajs.util.Rdr;
3837
import javajs.util.SB;
3938
import javajs.util.ZipTools;
@@ -44,6 +43,7 @@
4443
import swingjs.api.js.HTML5Applet;
4544
import swingjs.api.js.J2SInterface;
4645
import swingjs.api.js.JQuery;
46+
import swingjs.api.js.JSFunction;
4747
import swingjs.json.JSON;
4848
import swingjs.plaf.JSComponentUI;
4949
import swingjs.plaf.JSFrameUI;
@@ -1102,6 +1102,35 @@ public static void setClipboardContents(String data) {
11021102
}
11031103
}
11041104

1105+
@Override
1106+
public void getClipboardText(Consumer<String> whenDone) {
1107+
getClipboardContentsStatic(whenDone);
1108+
}
1109+
1110+
public static String getClipboardContentsStatic(Consumer<String> whenDone) {
1111+
1112+
String s = null;
1113+
1114+
try {
1115+
/**
1116+
* @j2sNative
1117+
*
1118+
* navigator.clipboard.readText().then(function(text) {whenDone.accept$O(text)});
1119+
* return null;
1120+
*
1121+
*/ {
1122+
Transferable data = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
1123+
s = (data.isDataFlavorSupported(DataFlavor.stringFlavor) ? (String) data.getTransferData(DataFlavor.stringFlavor) : "");
1124+
}
1125+
1126+
} catch (Throwable t) {
1127+
// browser does not support this function
1128+
}
1129+
if (whenDone != null)
1130+
whenDone.accept(s);
1131+
return s;
1132+
}
1133+
11051134
@Override
11061135
public byte[] getCachedBytes(String path) {
11071136
return (byte[]) getCachedFileData(path, true);

sources/net.sf.j2s.java.core/src/swingjs/api/JSUtilI.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.HashMap;
1010
import java.util.Map;
1111
import java.util.Properties;
12+
import java.util.function.Consumer;
1213
import java.util.function.Function;
1314
import java.util.zip.ZipEntry;
1415
import java.util.zip.ZipInputStream;
@@ -358,4 +359,11 @@ public interface JSUtilI {
358359
*/
359360
void setAppClass(Object app);
360361

362+
/** As of v. 73, not available in Firefox -- will alert the user and return null
363+
*
364+
* @param whenDone method to capture return asynchronously, returning null only if the
365+
* browser does not allow reading the clipboard.
366+
*/
367+
void getClipboardText(Consumer<String> whenDone);
368+
361369
}

sources/net.sf.j2s.java.core/src/test/TApp2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void keyTyped(KeyEvent e) {
153153

154154
@Override
155155
public void keyPressed(KeyEvent e) {
156-
System.out.println("keypressed");
156+
System.out.println("keypressed " + e);
157157
}
158158

159159
@Override

sources/net.sf.j2s.java.core/src/test/Test_Class.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package test;
22

3+
34
import static java.awt.Color.getColor;
45
import static java.awt.Toolkit.getDefaultToolkit;
56

@@ -25,6 +26,15 @@
2526
;
2627
class Test_Class extends Test_Class2<Integer> {
2728

29+
static class TestStatic {
30+
31+
static String sayHello() { return "Hello there"; }
32+
}
33+
34+
35+
36+
37+
2838
public int test_int = 3; // shadows Test_
2939

3040

@@ -324,6 +334,10 @@ public static String localtest() {
324334

325335
public static void main(String[] args) {
326336

337+
TestStatic ts = null;
338+
339+
System.out.println(ts.sayHello());
340+
327341
// System.out.println(new Date() + " " + Date.parse("3/4/2020"));
328342

329343
System.out.println(Number.class.isAssignableFrom(Double.class));

sources/net.sf.j2s.java.core/src/test/Test_Clipboard.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.EventQueue;
66
import java.awt.Font;
77
import java.awt.GridLayout;
8+
import java.awt.Toolkit;
89
import java.awt.event.ActionEvent;
910
import java.awt.event.ActionListener;
1011
import java.util.Date;
@@ -25,6 +26,7 @@
2526
import javax.swing.TransferHandler;
2627
import javax.swing.table.DefaultTableModel;
2728

29+
import swingjs.JSUtil;
2830
import swingjs.plaf.JSTableUI;
2931

3032
public class Test_Clipboard extends JFrame {
@@ -157,6 +159,18 @@ public void actionPerformed(ActionEvent e) {
157159

158160
});
159161

162+
m.add(btn, null);
163+
btn = new JButton("paste");
164+
btn.addActionListener(new ActionListener() {
165+
166+
@Override
167+
public void actionPerformed(ActionEvent e) {
168+
new JSUtil().getClipboardText((text) -> {
169+
System.out.println(text);
170+
});
171+
}
172+
173+
});
160174
m.add(btn, null);
161175
add(m, BorderLayout.SOUTH);
162176

0 commit comments

Comments
 (0)