35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package javax.swing; |
package javax.swing; |
40 |
|
|
41 |
import java.io.Serializable; |
import java.awt.datatransfer.Clipboard; |
42 |
|
import java.awt.datatransfer.DataFlavor; |
43 |
|
import java.awt.datatransfer.Transferable; |
44 |
|
import java.awt.event.ActionEvent; |
45 |
import java.awt.event.InputEvent; |
import java.awt.event.InputEvent; |
46 |
import java.awt.datatransfer.*; |
import java.io.Serializable; |
47 |
|
|
48 |
public class TransferHandler implements Serializable |
public class TransferHandler implements Serializable |
49 |
{ |
{ |
50 |
|
static class TransferAction extends AbstractAction |
51 |
|
{ |
52 |
|
private String command; |
53 |
|
|
54 |
|
public TransferAction(String command) |
55 |
|
{ |
56 |
|
this.command = command; |
57 |
|
} |
58 |
|
|
59 |
|
public void actionPerformed(ActionEvent event) |
60 |
|
{ |
61 |
|
JComponent component = (JComponent) event.getSource(); |
62 |
|
TransferHandler transferHandler = component.getTransferHandler(); |
63 |
|
Clipboard clipboard = getClipboard(component); |
64 |
|
|
65 |
|
if (command.equals(COMMAND_COPY)) |
66 |
|
transferHandler.exportToClipboard(component, clipboard, COPY); |
67 |
|
else if (command.equals(COMMAND_CUT)) |
68 |
|
transferHandler.exportToClipboard(component, clipboard, MOVE); |
69 |
|
else if (command.equals(COMMAND_PASTE)) |
70 |
|
{ |
71 |
|
Transferable transferable = clipboard.getContents(null); |
72 |
|
|
73 |
|
if (transferable != null) |
74 |
|
transferHandler.importData(component, transferable); |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
|
private static Clipboard getClipboard(JComponent component) |
79 |
|
{ |
80 |
|
SecurityManager sm = System.getSecurityManager(); |
81 |
|
|
82 |
|
if (sm != null) |
83 |
|
{ |
84 |
|
try |
85 |
|
{ |
86 |
|
sm.checkSystemClipboardAccess(); |
87 |
|
|
88 |
|
// We may access system clipboard. |
89 |
|
return component.getToolkit().getSystemClipboard(); |
90 |
|
} |
91 |
|
catch (SecurityException e) |
92 |
|
{ |
93 |
|
// We may not access system clipboard. |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
|
// Create VM-local clipboard if non exists yet. |
98 |
|
if (clipboard == null) |
99 |
|
clipboard = new Clipboard("Clipboard"); |
100 |
|
|
101 |
|
return clipboard; |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
private static final long serialVersionUID = -7908749299918704233L; |
private static final long serialVersionUID = -7908749299918704233L; |
106 |
|
|
107 |
|
public static final String COMMAND_COPY = "copy"; |
108 |
|
public static final String COMMAND_CUT = "cut"; |
109 |
|
public static final String COMMAND_PASTE = "paste"; |
110 |
|
|
111 |
public static final int NONE = 0; |
public static final int NONE = 0; |
112 |
public static final int COPY = 1; |
public static final int COPY = 1; |
113 |
public static final int MOVE = 2; |
public static final int MOVE = 2; |
114 |
public static final int COPY_OR_MOVE = 3; |
public static final int COPY_OR_MOVE = 3; |
115 |
|
|
116 |
static Action getCopyAction () |
private static Action copyAction = new TransferAction(COMMAND_COPY); |
117 |
|
private static Action cutAction = new TransferAction(COMMAND_CUT); |
118 |
|
private static Action pasteAction = new TransferAction(COMMAND_PASTE); |
119 |
|
|
120 |
|
/** |
121 |
|
* Clipboard if system clipboard may not be used. |
122 |
|
*/ |
123 |
|
private static Clipboard clipboard; |
124 |
|
|
125 |
|
private int sourceActions; |
126 |
|
private Icon visualRepresentation; |
127 |
|
|
128 |
|
public static Action getCopyAction() |
129 |
{ |
{ |
130 |
return null; |
return copyAction; |
131 |
} |
} |
132 |
|
|
133 |
static Action getCutAction () |
public static Action getCutAction() |
134 |
{ |
{ |
135 |
return null; |
return cutAction; |
136 |
} |
} |
137 |
|
|
138 |
static Action getPasteAction () |
public static Action getPasteAction() |
139 |
{ |
{ |
140 |
return null; |
return pasteAction; |
141 |
} |
} |
142 |
|
|
|
|
|
143 |
protected TransferHandler() |
protected TransferHandler() |
144 |
{ |
{ |
145 |
// Do nothing here. |
this.sourceActions = NONE; |
146 |
} |
} |
147 |
|
|
148 |
public TransferHandler(String property) |
public TransferHandler(String property) |
149 |
{ |
{ |
150 |
|
this.sourceActions = property != null ? COPY : NONE; |
151 |
} |
} |
152 |
|
|
153 |
public boolean canImport (JComponent c, DataFlavor[] flavors) |
public boolean canImport (JComponent c, DataFlavor[] flavors) |
155 |
return false; |
return false; |
156 |
} |
} |
157 |
|
|
158 |
public Transferable createTransferable(JComponent c) |
protected Transferable createTransferable(JComponent c) |
159 |
{ |
{ |
160 |
return null; |
return null; |
161 |
} |
} |
174 |
|
|
175 |
public int getSourceActions (JComponent c) |
public int getSourceActions (JComponent c) |
176 |
{ |
{ |
177 |
return 0; |
return sourceActions; |
178 |
} |
} |
179 |
|
|
180 |
public Icon getVisualRepresentation (Transferable t) |
public Icon getVisualRepresentation (Transferable t) |
181 |
{ |
{ |
182 |
return null; |
return visualRepresentation; |
183 |
} |
} |
184 |
|
|
185 |
public boolean importData (JComponent c, Transferable t) |
public boolean importData (JComponent c, Transferable t) |
186 |
{ |
{ |
187 |
return false; |
return false; |
188 |
} |
} |
|
|
|
189 |
} |
} |