001: package net.sourceforge.squirrel_sql.plugins.graph.graphtofiles;
002:
003: import javax.swing.*;
004: import java.awt.datatransfer.Transferable;
005: import java.awt.datatransfer.DataFlavor;
006: import java.awt.datatransfer.UnsupportedFlavorException;
007: import java.awt.datatransfer.Clipboard;
008: import java.awt.*;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ActionEvent;
011: import java.io.IOException;
012:
013: /**
014: * Unused at the moment.
015: * Might help one day to copy images to clipboard.
016: * See usage example commented at the bottom of this file.
017: */
018: public class ImageSelection extends TransferHandler implements
019: Transferable {
020:
021: private static final DataFlavor flavors[] = {
022: DataFlavor.imageFlavor,
023: new DataFlavor("image/jpeg", "JPEG image"),
024: new DataFlavor("image/gif", "GIF Image"),
025: new DataFlavor("mage/x-pict", "X Pict") };
026:
027: private Image image;
028:
029: public int getSourceActions(JComponent c) {
030: return TransferHandler.COPY;
031: }
032:
033: public boolean canImport(JComponent comp, DataFlavor flavor[]) {
034: if (!(comp instanceof JLabel)
035: || (comp instanceof AbstractButton)) {
036: return false;
037: }
038: for (int i = 0, n = flavor.length; i < n; i++) {
039: if (flavor[i].equals(flavors[0])) {
040: return true;
041: }
042: }
043: return false;
044: }
045:
046: public Transferable createTransferable(JComponent comp) {
047: // Clear
048: image = null;
049: Icon icon = null;
050:
051: if (comp instanceof JLabel) {
052: JLabel label = (JLabel) comp;
053: icon = label.getIcon();
054: } else if (comp instanceof AbstractButton) {
055: AbstractButton button = (AbstractButton) comp;
056: icon = button.getIcon();
057: }
058: if (icon instanceof ImageIcon) {
059: image = ((ImageIcon) icon).getImage();
060: return this ;
061: }
062: return null;
063: }
064:
065: public boolean importData(JComponent comp, Transferable t) {
066: ImageIcon icon = null;
067: try {
068: if (t.isDataFlavorSupported(flavors[0])) {
069: image = (Image) t.getTransferData(flavors[0]);
070: icon = new ImageIcon(image);
071: }
072: if (comp instanceof JLabel) {
073: JLabel label = (JLabel) comp;
074: label.setIcon(icon);
075: return true;
076: } else if (comp instanceof AbstractButton) {
077: AbstractButton button = (AbstractButton) comp;
078: button.setIcon(icon);
079: return true;
080: }
081: } catch (UnsupportedFlavorException ignored) {
082: } catch (IOException ignored) {
083: }
084: return false;
085: }
086:
087: // Transferable
088: public Object getTransferData(DataFlavor flavor) {
089: if (isDataFlavorSupported(flavor)) {
090: return image;
091: }
092: return null;
093: }
094:
095: public DataFlavor[] getTransferDataFlavors() {
096: return flavors;
097: }
098:
099: public boolean isDataFlavorSupported(DataFlavor flavor) {
100: return flavor.equals(flavors[0]);
101: }
102: }
103:
104: //
105: // USAGE EXAMPLE FOR IMAGESELECTION CLASS
106: //
107: //import net.sourceforge.squirrel_sql.plugins.graph.graphtofiles.ImageSelection;
108: //
109: //import java.awt.*;
110: //import java.awt.event.*;
111: //import java.awt.datatransfer.*;
112: //import javax.swing.*;
113: //
114: //public class ImageCopy
115: //{
116: //
117: // public static void main(String args[])
118: // {
119: //
120: // JFrame frame = new JFrame("Copy Image");
121: // frame.setDefaultCloseOperation
122: // (JFrame.EXIT_ON_CLOSE);
123: //
124: // Container contentPane = frame.getContentPane();
125: //
126: // Toolkit kit = Toolkit.getDefaultToolkit();
127: // final Clipboard clipboard =
128: // kit.getSystemClipboard();
129: //
130: // // Your example pic here
131: // Icon icon = new ImageIcon("/tmp/anypic.jpg");
132: // final JLabel label = new JLabel(icon);
133: // label.setTransferHandler(new ImageSelection());
134: //
135: // JScrollPane pane = new JScrollPane(label);
136: // contentPane.add(pane, BorderLayout.CENTER);
137: //
138: // JButton copy = new JButton("Label Copy");
139: // copy.addActionListener(new ActionListener()
140: // {
141: // public void actionPerformed(ActionEvent e)
142: // {
143: // TransferHandler handler =
144: // label.getTransferHandler();
145: // handler.exportToClipboard(label, clipboard,
146: // TransferHandler.COPY);
147: // }
148: // });
149: //
150: // JButton clear = new JButton("Label Clear");
151: // clear.addActionListener(new ActionListener()
152: // {
153: // public void actionPerformed(ActionEvent
154: // actionEvent)
155: // {
156: // label.setIcon(null);
157: // }
158: // });
159: //
160: // JButton paste = new JButton("Label Paste");
161: // paste.addActionListener(new ActionListener()
162: // {
163: // public void actionPerformed(ActionEvent
164: // actionEvent)
165: // {
166: // Transferable clipData =
167: // clipboard.getContents(clipboard);
168: // if (clipData != null)
169: // {
170: // if (clipData.isDataFlavorSupported
171: // (DataFlavor.imageFlavor))
172: // {
173: // TransferHandler handler =
174: // label.getTransferHandler();
175: // handler.importData(label, clipData);
176: // }
177: // }
178: // }
179: // });
180: //
181: // JPanel p = new JPanel();
182: // p.add(copy);
183: // p.add(clear);
184: // p.add(paste);
185: // contentPane.add(p, BorderLayout.NORTH);
186: //
187: // JPanel pasteP = new JPanel();
188: // JButton pasteB = new JButton("Paste");
189: //
190: // pasteB.setTransferHandler(new ImageSelection());
191: //
192: // pasteB.addActionListener
193: // (TransferHandler.getPasteAction());
194: //
195: // pasteP.add(pasteB);
196: // contentPane.add(pasteB, BorderLayout.SOUTH);
197: //
198: // frame.setSize(400, 400);
199: // frame.show();
200: // }
201: //}
|