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)
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
21 package gnu
.classpath
.examples
.datatransfer
;
24 import java
.awt
.event
.*;
25 import java
.awt
.datatransfer
.*;
29 import java
.util
.Arrays
;
30 import java
.util
.Iterator
;
31 import java
.util
.Random
;
34 * An example how datatransfer works for copying and pasting data to
35 * and from other programs.
39 implements ActionListener
, ItemListener
, FlavorListener
41 public static void main(String args
[])
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
;
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
79 addWindowListener(new WindowAdapter ()
81 public void windowClosing (WindowEvent e
)
86 flavors
.addItemListener(this);
87 Toolkit t
= Toolkit
.getDefaultToolkit();
88 Clipboard c
= t
.getSystemClipboard();
89 c
.addFlavorListener(this);
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!",
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
);
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
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
);
147 * The Object Panel holds a simple (Point) object that can be copied
148 * and pasted to another program that supports exchanging serialized
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
);
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
);
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
);
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();
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();
261 c
.setContents(new StringSelection(text
.getText()), null);
268 s
= (String
) c
.getData(DataFlavor
.stringFlavor
);
270 catch (UnsupportedFlavorException dfnse
)
273 catch (IOException ioe
)
276 catch (ClassCastException cce
)
286 c
.setContents(new ImageSelection(image
.getImage()), null);
293 i
= (Image
) c
.getData(DataFlavor
.imageFlavor
);
295 catch (UnsupportedFlavorException dfnse
)
298 catch (IOException ioe
)
301 catch (ClassCastException cce
)
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
)
336 c
.setContents(new FilesSelection(files
.getFiles()), null);
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
)
362 * Simple awt component that shows an settable image.
364 static class ImageComponent
extends Component
368 ImageComponent(Image image
)
379 void setImage(Image image
)
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
);
406 Serializable
getObject()
411 void setObject(Serializable object
)
413 this.object
= object
;
414 setText("Class: " + object
.getClass().getName()
416 + "toString(): " + object
.toString());
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
)
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
]);
446 void setFiles(File
[] files
)
450 for (int i
= 0; i
< files
.length
; i
++)
452 addItem(files
[i
].toString());
457 void setFiles(java
.util
.List list
)
459 File
[] fs
= new File
[list
.size()];
461 Iterator it
= list
.iterator();
463 fs
[i
++] = (File
) it
.next();
470 * Simple awt component that shows a settable list of DataFlavors.
472 static class FlavorsComponent
extends List
474 FlavorsComponent(DataFlavor
[] flavors
)
480 void setFlavors(DataFlavor
[] flavors
)
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
495 static class FlavorDetailsComponent
extends TextArea
497 private DataFlavor df
;
499 FlavorDetailsComponent(DataFlavor df
)
501 super("", 2, 80, TextArea
.SCROLLBARS_NONE
);
507 void setDataFlavor(DataFlavor df
)
510 || (df
!= null && df
.equals(this.df
)))
516 setText("No flavor selected");
520 Throwable exception
= null;
523 Toolkit t
= Toolkit
.getDefaultToolkit();
524 Clipboard c
= t
.getSystemClipboard();
533 setText("Data: " + o
.getClass().getName()
539 setText("Error retrieving: " + df
541 + exception
!= null ? exception
.toString() : "");
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
)
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
);
583 * Helper class to put an Object on a clipboard as Serializable
586 static class ObjectSelection
implements Transferable
588 private final Serializable obj
;
590 ObjectSelection(Serializable 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
);
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
)
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
);