forked from Shornone/MultiSVG-D2S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GClip.java
119 lines (107 loc) · 3.08 KB
/
GClip.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.*;
import java.awt.datatransfer.*;
public class GClip implements ClipboardOwner {
/**
* Static reference to enforce singleton pattern
*/
private static GClip gclip = null;
/**
* Class attribute to reference the programs clipboard
*/
private Clipboard clipboard = null;
/**
* Copy a string to the clipboard
* <a href="/two/profile/param">@param</a> chars
*/
public static boolean copy(String chars) {
if (gclip == null)
gclip = new GClip();
return gclip.copyString(chars);
}
/**
* Get a string from the clipboard
* <a href="/two/profile/return">@return</a> the string on the clipboard
*/
public static String paste() {
if (gclip == null)
gclip = new GClip();
return gclip.pasteString();
}
/**
* Ctor is private so clipboard is only created when a copy or paste is
* attempted and one does not exist already.
*/
private GClip() {
if (clipboard == null) {
makeClipboardObject();
}
}
/**
* If security permits use the system clipboard otherwise create
* our own application clipboard.
*/
private void makeClipboardObject() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkSystemClipboardAccess();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}
catch (SecurityException e) {
clipboard = new Clipboard("Application Clipboard");
}
} else {
try {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}
catch (Exception e) {
}
}
}
/**
* Copy a string to the clipboard. If the Clipboard has not been created
* then create it.
* <a href="/two/profile/return">@return</a> true for a successful copy to clipboard
*/
private boolean copyString(String chars) {
if (clipboard == null)
makeClipboardObject();
if (clipboard != null) {
StringSelection fieldContent = new StringSelection (chars);
clipboard.setContents (fieldContent, this);
return true;
}
return false;
}
/**
* Gets a string from the clipboard. If there is no Clipboard
* then create it.
* <a href="/two/profile/return">@return</a> if possible the string on the clipboard else an empty string
*/
private String pasteString() {
// If there is no clipboard then there is nothing to paste
if (clipboard == null) {
makeClipboardObject();
return "";
}
// We have a clipboard so get the string if we can
Transferable clipboardContent = clipboard.getContents(this);
if ((clipboardContent != null) &&
(clipboardContent.isDataFlavorSupported(DataFlavor.stringFlavor))) {
try {
String tempString;
tempString = (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
return tempString;
}
catch (Exception e) {
e.printStackTrace ();
}
}
return "";
}
/**
* Reqd by ClipboardOwner interface
*/
public void lostOwnership(Clipboard clipboard, Transferable contents) {
}
}