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
159: .getTextPlainUnicodeFlavor()))
160: text = true;
161:
162: DataFlavor[] flavors = contents.getTransferDataFlavors();
163: String[] mimeTargets = new String[flavors.length];
164: for (int i = 0; i < flavors.length; i++)
165: {
166: DataFlavor flavor = flavors[i];
167: String mimeType = flavor.getMimeType();
168: mimeTargets[i] = mimeType;
169:
170: if (! text)
171: if ("text".equals(flavor.getPrimaryType())
172: || flavor.isRepresentationClassReader())
173: text = true;
174:
175: if (! images && flavors[i].equals(DataFlavor.imageFlavor))
176: {
177: try
178: {
179: Object o = contents.getTransferData(DataFlavor.imageFlavor);
180: if (o instanceof Image)
181: images = true;
182: }
183: catch (UnsupportedFlavorException ufe)
184: {
185: }
186: catch (IOException ioe)
187: {
188: }
189: catch (ClassCastException cce)
190: {
191: }
192: }
193:
194: if (flavors[i].equals(DataFlavor.javaFileListFlavor))
195: files = true;
196: }
197:
198: advertiseContent(mimeTargets, text, images, files);
199: }
200:
201:
209: private native void advertiseContent(String[] targets,
210: boolean text,
211: boolean images,
212: boolean files);
213:
214:
219: private String provideText()
220: {
221: Transferable contents = this.contents;
222: if (contents == null || contents instanceof GtkSelection)
223: return null;
224:
225:
226: if (contents instanceof StringSelection)
227: {
228: try
229: {
230: return (String) contents.getTransferData(DataFlavor.stringFlavor);
231: }
232: catch (UnsupportedFlavorException ufe)
233: {
234: }
235: catch (IOException ioe)
236: {
237: }
238: catch (ClassCastException cce)
239: {
240: }
241: }
242:
243:
244:
245: try
246: {
247: DataFlavor plainText = DataFlavor.getTextPlainUnicodeFlavor();
248: Reader r = plainText.getReaderForText(contents);
249: if (r != null)
250: {
251: StringBuffer sb = new StringBuffer();
252: char[] cs = new char[1024];
253: int l = r.read(cs);
254: while (l != -1)
255: {
256: sb.append(cs, 0, l);
257: l = r.read(cs);
258: }
259: return sb.toString();
260: }
261: }
262: catch (IllegalArgumentException iae)
263: {
264: }
265: catch (UnsupportedEncodingException iee)
266: {
267: }
268: catch (UnsupportedFlavorException ufe)
269: {
270: }
271: catch (IOException ioe)
272: {
273: }
274:
275: return null;
276: }
277:
278:
283: private GtkImage provideImage()
284: {
285: Transferable contents = this.contents;
286: if (contents == null || contents instanceof GtkSelection)
287: return null;
288:
289: try
290: {
291: Object o = contents.getTransferData(DataFlavor.imageFlavor);
292: if( o instanceof GtkImage )
293: return (GtkImage) o;
294: else
295: return new GtkImage(((Image)o).getSource());
296: }
297: catch (UnsupportedFlavorException ufe)
298: {
299: }
300: catch (IOException ioe)
301: {
302: }
303: catch (ClassCastException cce)
304: {
305: }
306:
307: return null;
308: }
309:
310:
316: private String[] provideURIs()
317: {
318: Transferable contents = this.contents;
319: if (contents == null || contents instanceof GtkSelection)
320: return null;
321:
322: try
323: {
324: List list = (List) contents.getTransferData
325: (DataFlavor.javaFileListFlavor);
326: String[] uris = new String[list.size()];
327: int u = 0;
328: Iterator it = list.iterator();
329: while (it.hasNext())
330: uris[u++] = ((File) it.next()).toURI().toString();
331: return uris;
332: }
333: catch (UnsupportedFlavorException ufe)
334: {
335: }
336: catch (IOException ioe)
337: {
338: }
339: catch (ClassCastException cce)
340: {
341: }
342:
343: return null;
344: }
345:
346:
353: private byte[] provideContent(String target)
354: {
355:
356:
357: Transferable contents = this.contents;
358: if (contents == null || contents instanceof GtkSelection)
359: return null;
360:
361:
362:
363:
364:
365:
366: try
367: {
368: DataFlavor flavor = new DataFlavor(target);
369: Object o = contents.getTransferData(flavor);
370:
371: if (o instanceof byte[])
372: return (byte[]) o;
373:
374: if (o instanceof InputStream)
375: {
376: InputStream is = (InputStream) o;
377: ByteArrayOutputStream baos = new ByteArrayOutputStream();
378: byte[] bs = new byte[1024];
379: int l = is.read(bs);
380: while (l != -1)
381: {
382: baos.write(bs, 0, l);
383: l = is.read(bs);
384: }
385: return baos.toByteArray();
386: }
387:
388: if (o instanceof Serializable)
389: {
390: ByteArrayOutputStream baos = new ByteArrayOutputStream();
391: ObjectOutputStream oos = new ObjectOutputStream(baos);
392: oos.writeObject(o);
393: oos.close();
394: return baos.toByteArray();
395: }
396: }
397: catch (ClassNotFoundException cnfe)
398: {
399: }
400: catch (UnsupportedFlavorException ufe)
401: {
402: }
403: catch (IOException ioe)
404: {
405: }
406: catch (ClassCastException cce)
407: {
408: }
409:
410: return null;
411: }
412:
413:
418: private static native boolean initNativeState(GtkClipboard clipboard,
419: GtkClipboard selection,
420: String stringTarget,
421: String imageTarget,
422: String filesTarget);
423: }