1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46: import ;
47: import ;
48:
49: public class GtkClipboard extends Clipboard
50: {
51:
54: final static GtkClipboard clipboard = new GtkClipboard("System Clipboard");
55:
56:
59: final static GtkClipboard selection = new GtkClipboard("System Selection");
60:
61:
62:
63: static final String stringMimeType
64: = DataFlavor.stringFlavor.getMimeType();
65: static final String imageMimeType
66: = DataFlavor.imageFlavor.getMimeType();
67: static final String filesMimeType
68: = DataFlavor.javaFileListFlavor.getMimeType();
69:
70:
71:
72:
73: static final boolean canCache = initNativeState(clipboard, selection,
74: stringMimeType,
75: imageMimeType,
76: filesMimeType);
77:
78:
82: private GtkClipboard(String name)
83: {
84: super(name);
85: setContents(new GtkSelection(this), null);
86: }
87:
88:
92: static GtkClipboard getClipboardInstance()
93: {
94: return clipboard;
95: }
96:
97:
101: static GtkClipboard getSelectionInstance()
102: {
103: return selection;
104: }
105:
106:
114: private synchronized void setSystemContents(boolean cleared)
115: {
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126: boolean owner = ! (contents instanceof GtkSelection);
127: boolean needNotification = (cleared && owner) || (! cleared && ! owner);
128: if (needNotification)
129: GtkClipboardNotifier.announce(this);
130: }
131:
132:
136: public synchronized void setContents(Transferable contents,
137: ClipboardOwner owner)
138: {
139: super.setContents(contents, owner);
140:
141: if (contents == null)
142: {
143: advertiseContent(null, false, false, false);
144: return;
145: }
146:
147:
148: if (contents instanceof GtkSelection)
149: return;
150:
151: boolean text = false;
152: boolean images = false;
153: boolean files = false;
154:
155: if (contents instanceof StringSelection
156: || contents.isDataFlavorSupported(DataFlavor.stringFlavor)
157: || contents.isDataFlavorSupported(DataFlavor.plainTextFlavor)
158: || contents.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor()))
159: text = true;
160:
161: DataFlavor[] flavors = contents.getTransferDataFlavors();
162: String[] mimeTargets = new String[flavors.length];
163: for (int i = 0; i < flavors.length; i++)
164: {
165: DataFlavor flavor = flavors[i];
166: String mimeType = flavor.getMimeType();
167: mimeTargets[i] = mimeType;
168:
169: if (! text)
170: if ("text".equals(flavor.getPrimaryType())
171: || flavor.isRepresentationClassReader())
172: text = true;
173:
174: if (! images && flavors[i].equals(DataFlavor.imageFlavor))
175: {
176: try
177: {
178: Object o = contents.getTransferData(DataFlavor.imageFlavor);
179: if (o instanceof Image)
180: images = true;
181: }
182: catch (UnsupportedFlavorException ufe)
183: {
184: }
185: catch (IOException ioe)
186: {
187: }
188: catch (ClassCastException cce)
189: {
190: }
191: }
192:
193: if (flavors[i].equals(DataFlavor.javaFileListFlavor))
194: files = true;
195: }
196:
197: advertiseContent(mimeTargets, text, images, files);
198: }
199:
200:
208: private native void advertiseContent(String[] targets,
209: boolean text,
210: boolean images,
211: boolean files);
212:
213:
218: private String provideText()
219: {
220: Transferable contents = this.contents;
221: if (contents == null || contents instanceof GtkSelection)
222: return null;
223:
224:
225: if (contents instanceof StringSelection)
226: {
227: try
228: {
229: return (String) contents.getTransferData(DataFlavor.stringFlavor);
230: }
231: catch (UnsupportedFlavorException ufe)
232: {
233: }
234: catch (IOException ioe)
235: {
236: }
237: catch (ClassCastException cce)
238: {
239: }
240: }
241:
242:
243:
244: try
245: {
246: DataFlavor plainText = DataFlavor.getTextPlainUnicodeFlavor();
247: Reader r = plainText.getReaderForText(contents);
248: if (r != null)
249: {
250: StringBuffer sb = new StringBuffer();
251: char[] cs = new char[1024];
252: int l = r.read(cs);
253: while (l != -1)
254: {
255: sb.append(cs, 0, l);
256: l = r.read(cs);
257: }
258: return sb.toString();
259: }
260: }
261: catch (IllegalArgumentException iae)
262: {
263: }
264: catch (UnsupportedEncodingException iee)
265: {
266: }
267: catch (UnsupportedFlavorException ufe)
268: {
269: }
270: catch (IOException ioe)
271: {
272: }
273:
274: return null;
275: }
276:
277:
282: private GtkImage provideImage()
283: {
284: Transferable contents = this.contents;
285: if (contents == null || contents instanceof GtkSelection)
286: return null;
287:
288: try
289: {
290: Object o = contents.getTransferData(DataFlavor.imageFlavor);
291: if( o instanceof GtkImage )
292: return (GtkImage) o;
293: else
294: return new GtkImage(((Image)o).getSource());
295: }
296: catch (UnsupportedFlavorException ufe)
297: {
298: }
299: catch (IOException ioe)
300: {
301: }
302: catch (ClassCastException cce)
303: {
304: }
305:
306: return null;
307: }
308:
309:
315: private String[] provideURIs()
316: {
317: Transferable contents = this.contents;
318: if (contents == null || contents instanceof GtkSelection)
319: return null;
320:
321: try
322: {
323: List list = (List) contents.getTransferData(DataFlavor.javaFileListFlavor);
324: String[] uris = new String[list.size()];
325: int u = 0;
326: Iterator it = list.iterator();
327: while (it.hasNext())
328: uris[u++] = ((File) it.next()).toURI().toString();
329: return uris;
330: }
331: catch (UnsupportedFlavorException ufe)
332: {
333: }
334: catch (IOException ioe)
335: {
336: }
337: catch (ClassCastException cce)
338: {
339: }
340:
341: return null;
342: }
343:
344:
351: private byte[] provideContent(String target)
352: {
353:
354:
355: Transferable contents = this.contents;
356: if (contents == null || contents instanceof GtkSelection)
357: return null;
358:
359:
360:
361:
362:
363:
364: try
365: {
366: DataFlavor flavor = new DataFlavor(target);
367: Object o = contents.getTransferData(flavor);
368:
369: if (o instanceof byte[])
370: return (byte[]) o;
371:
372: if (o instanceof InputStream)
373: {
374: InputStream is = (InputStream) o;
375: ByteArrayOutputStream baos = new ByteArrayOutputStream();
376: byte[] bs = new byte[1024];
377: int l = is.read(bs);
378: while (l != -1)
379: {
380: baos.write(bs, 0, l);
381: l = is.read(bs);
382: }
383: return baos.toByteArray();
384: }
385:
386: if (o instanceof Serializable)
387: {
388: ByteArrayOutputStream baos = new ByteArrayOutputStream();
389: ObjectOutputStream oos = new ObjectOutputStream(baos);
390: oos.writeObject(o);
391: oos.close();
392: return baos.toByteArray();
393: }
394: }
395: catch (ClassNotFoundException cnfe)
396: {
397: }
398: catch (UnsupportedFlavorException ufe)
399: {
400: }
401: catch (IOException ioe)
402: {
403: }
404: catch (ClassCastException cce)
405: {
406: }
407:
408: return null;
409: }
410:
411:
416: private static native boolean initNativeState(GtkClipboard clipboard,
417: GtkClipboard selection,
418: String stringTarget,
419: String imageTarget,
420: String filesTarget);
421: }