View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common 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 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  import java.awt.Image;
22  import java.awt.datatransfer.DataFlavor;
23  import java.awt.datatransfer.Transferable;
24  
25  import javax.swing.Icon;
26  import javax.swing.ImageIcon;
27  import javax.swing.JComponent;
28  import javax.swing.JLabel;
29  import javax.swing.TransferHandler;
30  
31  
32  public class ImageClipboardHelper extends TransferHandler implements Transferable {
33  
34  	private static final long serialVersionUID = 1L;
35  	private static final DataFlavor[] flavors = { DataFlavor.imageFlavor };
36      private Image image;
37  
38      
39      /**
40       * we need to define action copy , because we are copying image to sys. clipboard
41       */
42      public int getSourceActions(JComponent c) {
43          return TransferHandler.COPY;
44      }
45  
46     
47  
48      public Transferable createTransferable(JComponent comp) {
49          image = null;
50  
51          Icon icon = null;
52  
53          if (comp instanceof JLabel) {
54              JLabel label = (JLabel) comp;
55              icon = label.getIcon();
56          } 
57          if (icon instanceof ImageIcon) {
58              image = ((ImageIcon) icon).getImage();
59  
60              return this;
61          }
62  
63          return null;
64      }
65  
66      
67      // Transferable
68      public Object getTransferData(DataFlavor flavor) {
69          if (isDataFlavorSupported(flavor)) {
70              return image;
71          }
72  
73          return null;
74      }
75  
76      public DataFlavor[] getTransferDataFlavors() {
77          return flavors;
78      }
79  
80      public boolean isDataFlavorSupported(DataFlavor flavor) {
81          return flavor.equals(flavors[0]);
82      }
83  }