/plugins/ImageViewer/tags/1.2/src/imageviewer/ImageSelection.java

# · Java · 42 lines · 28 code · 7 blank · 7 comment · 2 complexity · 556b487d7a4ed506cd5e97aa8979c770 MD5 · raw file

  1. package imageviewer;
  2. import java.awt.*;
  3. import java.awt.datatransfer.*;
  4. /**
  5. * A Transferable for copying an image to the system clipboard.
  6. */
  7. public class ImageSelection implements Transferable {
  8. private Image image;
  9. /**
  10. * Copy the given image to the system clipboard.
  11. * @param image The image to copy.
  12. */
  13. public static void copyImageToClipboard( Image image ) {
  14. ImageSelection imageSelection = new ImageSelection( image );
  15. Toolkit toolkit = Toolkit.getDefaultToolkit();
  16. toolkit.getSystemClipboard().setContents( imageSelection, null );
  17. }
  18. public ImageSelection( Image image ) {
  19. this.image = image;
  20. }
  21. public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException {
  22. if ( flavor.equals( DataFlavor.imageFlavor ) == false ) {
  23. throw new UnsupportedFlavorException( flavor );
  24. }
  25. return image;
  26. }
  27. public boolean isDataFlavorSupported( DataFlavor flavor ) {
  28. return flavor.equals( DataFlavor.imageFlavor );
  29. }
  30. public DataFlavor[] getTransferDataFlavors() {
  31. return new DataFlavor[] {
  32. DataFlavor.imageFlavor
  33. };
  34. }
  35. }