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:
52:
53:
54: static final String stringMimeType;
55: static final String imageMimeType;
56: static final String filesMimeType;
57:
58:
59:
60:
61: static final boolean canCache;
62:
63: static
64: {
65: stringMimeType = DataFlavor.stringFlavor.getMimeType();
66: imageMimeType = DataFlavor.imageFlavor.getMimeType();
67: filesMimeType = DataFlavor.javaFileListFlavor.getMimeType();
68:
69: canCache = initNativeState(stringMimeType, imageMimeType, filesMimeType);
70: }
71:
72:
75: private static GtkClipboard instance = new GtkClipboard();
76:
77:
81: private GtkClipboard()
82: {
83: super("System Clipboard");
84: setContents(new GtkSelection(), null);
85: }
86:
87:
90:
91: static GtkClipboard getInstance()
92: {
93: return instance;
94: }
95:
96:
101: private static void setSystemContents()
102: {
103: GtkClipboardNotifier.announce();
104: }
105:
106:
110: public synchronized void setContents(Transferable contents,
111: ClipboardOwner owner)
112: {
113: super.setContents(contents, owner);
114:
115: if (contents == null)
116: {
117: advertiseContent(null, false, false, false);
118: return;
119: }
120:
121:
122: if (contents instanceof GtkSelection)
123: return;
124:
125: boolean text = false;
126: boolean images = false;
127: boolean files = false;
128:
129: if (contents instanceof StringSelection
130: || contents.isDataFlavorSupported(DataFlavor.stringFlavor)
131: || contents.isDataFlavorSupported(DataFlavor.plainTextFlavor)
132: || contents.isDataFlavorSupported(DataFlavor
133: .getTextPlainUnicodeFlavor()))
134: text = true;
135:
136: DataFlavor[] flavors = contents.getTransferDataFlavors();
137: String[] mimeTargets = new String[flavors.length];
138: for (int i = 0; i < flavors.length; i++)
139: {
140: DataFlavor flavor = flavors[i];
141: String mimeType = flavor.getMimeType();
142: mimeTargets[i] = mimeType;
143:
144: if (! text)
145: if ("text".equals(flavor.getPrimaryType())
146: || flavor.isRepresentationClassReader())
147: text = true;
148:
149:
150:
151:
152: if (! images && flavors[i].equals(DataFlavor.imageFlavor))
153: {
154: try
155: {
156: Object o = contents.getTransferData(DataFlavor.imageFlavor);
157: if (o instanceof GtkImage)
158: images = true;
159: }
160: catch (UnsupportedFlavorException ufe)
161: {
162: }
163: catch (IOException ioe)
164: {
165: }
166: catch (ClassCastException cce)
167: {
168: }
169: }
170:
171: if (flavors[i].equals(DataFlavor.javaFileListFlavor))
172: files = true;
173: }
174:
175: advertiseContent(mimeTargets, text, images, files);
176: }
177:
178:
186: private native void advertiseContent(String[] targets,
187: boolean text,
188: boolean images,
189: boolean files);
190:
191:
196: private String provideText()
197: {
198: Transferable contents = this.contents;
199: if (contents == null || contents instanceof GtkSelection)
200: return null;
201:
202:
203: if (contents instanceof StringSelection)
204: {
205: try
206: {
207: return (String) contents.getTransferData(DataFlavor.stringFlavor);
208: }
209: catch (UnsupportedFlavorException ufe)
210: {
211: }
212: catch (IOException ioe)
213: {
214: }
215: catch (ClassCastException cce)
216: {
217: }
218: }
219:
220:
221:
222: try
223: {
224: DataFlavor plainText = DataFlavor.getTextPlainUnicodeFlavor();
225: Reader r = plainText.getReaderForText(contents);
226: if (r != null)
227: {
228: StringBuffer sb = new StringBuffer();
229: char[] cs = new char[1024];
230: int l = r.read(cs);
231: while (l != -1)
232: {
233: sb.append(cs, 0, l);
234: l = r.read(cs);
235: }
236: return sb.toString();
237: }
238: }
239: catch (IllegalArgumentException iae)
240: {
241: }
242: catch (UnsupportedEncodingException iee)
243: {
244: }
245: catch (UnsupportedFlavorException ufe)
246: {
247: }
248: catch (IOException ioe)
249: {
250: }
251:
252: return null;
253: }
254:
255:
260: private GtkImage provideImage()
261: {
262: Transferable contents = this.contents;
263: if (contents == null || contents instanceof GtkSelection)
264: return null;
265:
266: try
267: {
268: return (GtkImage) contents.getTransferData(DataFlavor.imageFlavor);
269: }
270: catch (UnsupportedFlavorException ufe)
271: {
272: }
273: catch (IOException ioe)
274: {
275: }
276: catch (ClassCastException cce)
277: {
278: }
279:
280: return null;
281: }
282:
283:
289: private String[] provideURIs()
290: {
291: Transferable contents = this.contents;
292: if (contents == null || contents instanceof GtkSelection)
293: return null;
294:
295: try
296: {
297: List list = (List) contents.getTransferData
298: (DataFlavor.javaFileListFlavor);
299: String[] uris = new String[list.size()];
300: int u = 0;
301: Iterator it = list.iterator();
302: while (it.hasNext())
303: uris[u++] = ((File) it.next()).toURI().toString();
304: return uris;
305: }
306: catch (UnsupportedFlavorException ufe)
307: {
308: }
309: catch (IOException ioe)
310: {
311: }
312: catch (ClassCastException cce)
313: {
314: }
315:
316: return null;
317: }
318:
319:
326: private byte[] provideContent(String target)
327: {
328:
329:
330: Transferable contents = this.contents;
331: if (contents == null || contents instanceof GtkSelection)
332: return null;
333:
334:
335:
336:
337:
338:
339: try
340: {
341: DataFlavor flavor = new DataFlavor(target);
342: Object o = contents.getTransferData(flavor);
343:
344: if (o instanceof byte[])
345: return (byte[]) o;
346:
347: if (o instanceof InputStream)
348: {
349: InputStream is = (InputStream) o;
350: ByteArrayOutputStream baos = new ByteArrayOutputStream();
351: byte[] bs = new byte[1024];
352: int l = is.read(bs);
353: while (l != -1)
354: {
355: baos.write(bs, 0, l);
356: l = is.read(bs);
357: }
358: return baos.toByteArray();
359: }
360:
361: if (o instanceof Serializable)
362: {
363: ByteArrayOutputStream baos = new ByteArrayOutputStream();
364: ObjectOutputStream oos = new ObjectOutputStream(baos);
365: oos.writeObject(o);
366: oos.close();
367: return baos.toByteArray();
368: }
369: }
370: catch (ClassNotFoundException cnfe)
371: {
372: }
373: catch (UnsupportedFlavorException ufe)
374: {
375: }
376: catch (IOException ioe)
377: {
378: }
379: catch (ClassCastException cce)
380: {
381: }
382:
383: return null;
384: }
385:
386:
391: private static native boolean initNativeState(String stringTarget,
392: String imageTarget,
393: String filesTarget);
394: }