Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / examples / gnu / classpath / examples / datatransfer / Demo.java
blob4f789c9416b34bc015512574358dd300c73ef4a1
1 /* Demo.java -- And example of copy/paste datatransfer
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
21 package gnu.classpath.examples.datatransfer;
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.awt.datatransfer.*;
27 import java.io.*;
28 import java.net.URL;
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.Random;
33 /**
34 * An example how datatransfer works for copying and pasting data to
35 * and from other programs.
37 class Demo
38 extends Frame
39 implements ActionListener, ItemListener, FlavorListener
41 public static void main(String args[])
43 new Demo();
46 private TextArea text;
47 private Button copyText;
48 private Button pasteText;
50 private ImageComponent image;
51 private Button copyImage;
52 private Button pasteImage;
54 private ObjectComponent object;
55 private Button copyObject;
56 private Button pasteObject;
58 private FilesComponent files;
59 private Button copyFiles;
60 private Button pasteFiles;
62 private FlavorsComponent flavors;
63 private FlavorDetailsComponent details;
65 private Demo()
67 super("GNU Classpath datatransfer");
69 /* Add all the different panel to the main window in one row. */
70 setLayout(new GridLayout(5, 1, 10, 10));
71 add(createTextPanel());
72 add(createImagePanel());
73 add(createObjectPanel());
74 add(createFilesPanel());
75 add(createFlavorsPanel());
77 /* Add listeners for the various buttons and events we are
78 interested in. */
79 addWindowListener(new WindowAdapter ()
81 public void windowClosing (WindowEvent e)
83 dispose();
85 });
86 flavors.addItemListener(this);
87 Toolkit t = Toolkit.getDefaultToolkit();
88 Clipboard c = t.getSystemClipboard();
89 c.addFlavorListener(this);
91 /* Show time! */
92 pack();
93 show();
96 /**
97 * The Text Panel will show simple text that can be copied and pasted.
99 private Panel createTextPanel()
101 Panel textPanel = new Panel();
102 textPanel.setLayout(new BorderLayout());
103 text = new TextArea("GNU Everywhere!",
104 2, 80,
105 TextArea.SCROLLBARS_VERTICAL_ONLY);
106 text.setEditable(false);
107 text.setEnabled(true);
108 Panel textButtons = new Panel();
109 textButtons.setLayout(new FlowLayout());
110 copyText = new Button("Copy text");
111 copyText.addActionListener(this);
112 pasteText = new Button("Paste text");
113 pasteText.addActionListener(this);
114 textButtons.add(copyText);
115 textButtons.add(pasteText);
116 textPanel.add(text, BorderLayout.CENTER);
117 textPanel.add(textButtons, BorderLayout.SOUTH);
118 return textPanel;
122 * The Image Panel shows an image that can be copied to another
123 * program or be replaced by pasting in an image from another
124 * application.
126 private Panel createImagePanel()
128 Panel imagePanel = new Panel();
129 imagePanel.setLayout(new BorderLayout());
130 URL imageurl = this.getClass()
131 .getResource("/gnu/classpath/examples/icons/big-fullscreen.png");
132 Image img = Toolkit.getDefaultToolkit().createImage(imageurl);
133 image = new ImageComponent(img);
134 Panel imageButtons = new Panel();
135 copyImage = new Button("Copy image");
136 copyImage.addActionListener(this);
137 pasteImage = new Button("Paste image");
138 pasteImage.addActionListener(this);
139 imageButtons.add(copyImage);
140 imageButtons.add(pasteImage);
141 imagePanel.add(image, BorderLayout.CENTER);
142 imagePanel.add(imageButtons, BorderLayout.SOUTH);
143 return imagePanel;
147 * The Object Panel holds a simple (Point) object that can be copied
148 * and pasted to another program that supports exchanging serialized
149 * objects.
151 private Panel createObjectPanel()
153 Panel objectPanel = new Panel();
154 objectPanel.setLayout(new BorderLayout());
155 Random random = new Random();
156 int x = (byte) random.nextInt();
157 int y = (byte) random.nextInt();
158 object = new ObjectComponent(new Point(x, y));
159 Panel objectButtons = new Panel();
160 copyObject = new Button("Copy object");
161 copyObject.addActionListener(this);
162 pasteObject = new Button("Paste object");
163 pasteObject.addActionListener(this);
164 objectButtons.add(copyObject);
165 objectButtons.add(pasteObject);
166 objectPanel.add(object, BorderLayout.CENTER);
167 objectPanel.add(objectButtons, BorderLayout.SOUTH);
168 return objectPanel;
172 * The Files Panel shows the files from the current working
173 * directory. They can be copied and pasted between other
174 * applications that support the exchange of file lists.
176 private Panel createFilesPanel()
178 Panel filesPanel = new Panel();
179 filesPanel.setLayout(new BorderLayout());
180 files = new FilesComponent(new File(".").listFiles());
181 Panel filesButtons = new Panel();
182 copyFiles = new Button("Copy files");
183 copyFiles.addActionListener(this);
184 pasteFiles = new Button("Paste files");
185 pasteFiles.addActionListener(this);
186 filesButtons.add(copyFiles);
187 filesButtons.add(pasteFiles);
188 filesPanel.add(files, BorderLayout.CENTER);
189 filesPanel.add(filesButtons, BorderLayout.SOUTH);
190 return filesPanel;
194 * The Flavors Panel shows the different formats (mime-types) that
195 * data on the clipboard is available in. By clicking on a flavor
196 * details about the representation class and object is given.
198 private Panel createFlavorsPanel()
200 Panel flavorsPanel = new Panel();
201 flavorsPanel.setLayout(new BorderLayout());
202 Label flavorsHeader = new Label("Flavors on clipboard:");
203 Toolkit t = Toolkit.getDefaultToolkit();
204 Clipboard c = t.getSystemClipboard();
205 DataFlavor[] dataflavors = c.getAvailableDataFlavors();
206 flavors = new FlavorsComponent(dataflavors);
207 details = new FlavorDetailsComponent(null);
208 flavorsPanel.add(flavorsHeader, BorderLayout.NORTH);
209 flavorsPanel.add(flavors, BorderLayout.CENTER);
210 flavorsPanel.add(details, BorderLayout.SOUTH);
211 return flavorsPanel;
215 * FlavorListener implementation that updates the Flavors Panel
216 * whenever a change in the mime-types available has been detected.
218 public void flavorsChanged(FlavorEvent event)
220 Toolkit t = Toolkit.getDefaultToolkit();
221 Clipboard c = t.getSystemClipboard();
222 DataFlavor[] dataflavors = c.getAvailableDataFlavors();
223 flavors.setFlavors(dataflavors);
224 details.setDataFlavor(null);
228 * ItemChangeListener implementation that updates the flavor details
229 * whenever the user selects a different representation of the data
230 * available on the clipboard.
232 public void itemStateChanged(ItemEvent evt)
234 DataFlavor df = null;
235 String s = flavors.getSelectedItem();
236 if (s != null)
240 df = new DataFlavor(s);
242 catch (ClassNotFoundException cnfe)
244 cnfe.printStackTrace();
247 details.setDataFlavor(df);
251 * ActionListener implementations that will copy or past data
252 * to/from the clipboard when the user requests that for the text,
253 * image, object of file component.
255 public void actionPerformed (ActionEvent evt)
257 Button b = (Button) evt.getSource();
258 Toolkit t = Toolkit.getDefaultToolkit();
259 Clipboard c = t.getSystemClipboard();
260 if (b == copyText)
261 c.setContents(new StringSelection(text.getText()), null);
263 if (b == pasteText)
265 String s = null;
268 s = (String) c.getData(DataFlavor.stringFlavor);
270 catch (UnsupportedFlavorException dfnse)
273 catch (IOException ioe)
276 catch (ClassCastException cce)
279 if (s == null)
280 t.beep();
281 else
282 text.setText(s);
285 if (b == copyImage)
286 c.setContents(new ImageSelection(image.getImage()), null);
288 if (b == pasteImage)
290 Image i = null;
293 i = (Image) c.getData(DataFlavor.imageFlavor);
295 catch (UnsupportedFlavorException dfnse)
298 catch (IOException ioe)
301 catch (ClassCastException cce)
304 if (i == null)
305 t.beep();
306 else
307 image.setImage(i);
310 if (b == copyObject)
311 c.setContents(new ObjectSelection(object.getObject()), null);
313 if (b == pasteObject)
315 Serializable o = null;
318 o = (Serializable) c.getData(ObjectSelection.objFlavor);
320 catch (UnsupportedFlavorException dfnse)
323 catch (IOException ioe)
326 catch (ClassCastException cce)
329 if (o == null)
330 t.beep();
331 else
332 object.setObject(o);
335 if (b == copyFiles)
336 c.setContents(new FilesSelection(files.getFiles()), null);
338 if (b == pasteFiles)
340 java.util.List fs = null;
343 fs = (java.util.List) c.getData(DataFlavor.javaFileListFlavor);
345 catch (UnsupportedFlavorException dfnse)
348 catch (IOException ioe)
351 catch (ClassCastException cce)
354 if (fs == null)
355 t.beep();
356 else
357 files.setFiles(fs);
362 * Simple awt component that shows an settable image.
364 static class ImageComponent extends Component
366 private Image image;
368 ImageComponent(Image image)
370 setSize(20, 20);
371 setImage(image);
374 Image getImage()
376 return image;
379 void setImage(Image image)
381 this.image = image;
382 repaint();
385 public void paint(Graphics g)
387 g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
392 * Simple awt component that shows a settable Serializable object.
394 static class ObjectComponent extends TextArea
396 private Serializable object;
398 ObjectComponent(Serializable object)
400 super("", 2, 80, TextArea.SCROLLBARS_NONE);
401 setEditable(false);
402 setEnabled(false);
403 setObject(object);
406 Serializable getObject()
408 return object;
411 void setObject(Serializable object)
413 this.object = object;
414 setText("Class: " + object.getClass().getName()
415 + "\n"
416 + "toString(): " + object.toString());
417 repaint();
422 * Simple awt component that shows a settable list of Files.
424 static class FilesComponent extends List
426 private File[] files;
428 FilesComponent(File[] files)
430 super(4, true);
431 setFiles(files);
434 File[] getFiles()
436 String[] strings = getSelectedItems();
437 if (strings == null || strings.length == 0)
438 return (File[]) files.clone();
440 File[] fs = new File[strings.length];
441 for (int i = 0; i < strings.length; i++)
442 fs[i] = new File(strings[i]);
443 return fs;
446 void setFiles(File[] files)
448 this.files = files;
449 removeAll();
450 for (int i = 0; i < files.length; i++)
452 addItem(files[i].toString());
453 select(i);
457 void setFiles(java.util.List list)
459 File[] fs = new File[list.size()];
460 int i = 0;
461 Iterator it = list.iterator();
462 while (it.hasNext())
463 fs[i++] = (File) it.next();
465 setFiles(fs);
470 * Simple awt component that shows a settable list of DataFlavors.
472 static class FlavorsComponent extends List
474 FlavorsComponent(DataFlavor[] flavors)
476 super(4);
477 setFlavors(flavors);
480 void setFlavors(DataFlavor[] flavors)
482 removeAll();
483 for (int i = 0; i < flavors.length; i++)
485 addItem(flavors[i].getMimeType());
491 * Simple awt component that shows the details for and an object as
492 * found on the system clipboard as represented by a given
493 * DataFlavor.
495 static class FlavorDetailsComponent extends TextArea
497 private DataFlavor df;
499 FlavorDetailsComponent(DataFlavor df)
501 super("", 2, 80, TextArea.SCROLLBARS_NONE);
502 setEditable(false);
503 setEnabled(false);
504 setDataFlavor(df);
507 void setDataFlavor(DataFlavor df)
509 if (df == this.df
510 || (df != null && df.equals(this.df)))
511 return;
513 this.df = df;
515 if (df == null)
516 setText("No flavor selected");
517 else
519 Object o = null;
520 Throwable exception = null;
523 Toolkit t = Toolkit.getDefaultToolkit();
524 Clipboard c = t.getSystemClipboard();
525 o = c.getData(df);
527 catch (Throwable t)
529 exception = t;
531 if (o != null)
533 setText("Data: " + o.getClass().getName()
534 + "\n"
535 + o);
537 else
539 setText("Error retrieving: " + df
540 + "\n"
541 + exception != null ? exception.toString() : "");
544 repaint();
549 * Helper class to put an Image on a clipboard as
550 * DataFlavor.imageFlavor.
552 static class ImageSelection implements Transferable
554 private final Image img;
556 ImageSelection(Image img)
558 this.img = img;
561 static DataFlavor[] flavors = new DataFlavor[] { DataFlavor.imageFlavor };
562 public DataFlavor[] getTransferDataFlavors()
564 return (DataFlavor[]) flavors.clone();
567 public boolean isDataFlavorSupported(DataFlavor flavor)
569 return flavor.equals(DataFlavor.imageFlavor);
572 public Object getTransferData(DataFlavor flavor)
573 throws UnsupportedFlavorException
575 if (!isDataFlavorSupported(flavor))
576 throw new UnsupportedFlavorException(flavor);
578 return img;
583 * Helper class to put an Object on a clipboard as Serializable
584 * object.
586 static class ObjectSelection implements Transferable
588 private final Serializable obj;
590 ObjectSelection(Serializable obj)
592 this.obj = obj;
595 static DataFlavor objFlavor = new DataFlavor(Serializable.class,
596 "Serialized Object");
597 static DataFlavor[] flavors = new DataFlavor[] { objFlavor };
598 public DataFlavor[] getTransferDataFlavors()
600 return (DataFlavor[]) flavors.clone();
603 public boolean isDataFlavorSupported(DataFlavor flavor)
605 return flavor.equals(objFlavor);
608 public Object getTransferData(DataFlavor flavor)
609 throws UnsupportedFlavorException
611 if (!isDataFlavorSupported(flavor))
612 throw new UnsupportedFlavorException(flavor);
614 return obj;
619 * Helper class to put a List of Files on the clipboard as
620 * DataFlavor.javaFileListFlavor.
622 static class FilesSelection implements Transferable
624 private final File[] files;
626 FilesSelection(File[] files)
628 this.files = files;
631 static DataFlavor[] flavors = new DataFlavor[]
632 { DataFlavor.javaFileListFlavor };
633 public DataFlavor[] getTransferDataFlavors()
635 return (DataFlavor[]) flavors.clone();
638 public boolean isDataFlavorSupported(DataFlavor flavor)
640 return flavor.equals(DataFlavor.javaFileListFlavor);
643 public Object getTransferData(DataFlavor flavor)
644 throws UnsupportedFlavorException
646 if (!isDataFlavorSupported(flavor))
647 throw new UnsupportedFlavorException(flavor);
649 return Arrays.asList(files);