01: package com.vividsolutions.jump.workbench.ui.plugin.clipboard;
02:
03: import java.awt.Toolkit;
04: import java.awt.datatransfer.DataFlavor;
05: import java.awt.datatransfer.Transferable;
06: import java.awt.datatransfer.UnsupportedFlavorException;
07: import java.awt.image.BufferedImage;
08: import java.io.IOException;
09:
10: import javax.swing.JComponent;
11:
12: import com.vividsolutions.jts.util.Assert;
13: import com.vividsolutions.jump.I18N;
14: import com.vividsolutions.jump.workbench.WorkbenchContext;
15: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
16: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
17: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
18: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
19: import com.vividsolutions.jump.workbench.ui.plugin.ExportImagePlugIn;
20:
21: public class CopyImagePlugIn extends ExportImagePlugIn {
22: public boolean execute(PlugInContext context) throws Exception {
23: Transferable transferable = createTransferable(context);
24: if (transferable == null) {
25: context
26: .getWorkbenchFrame()
27: .warnUser(
28: I18N
29: .get("ui.plugin.clipboard.CopyImagePlugIn.could-not-copy-the-image-for-some-reason"));
30: return false;
31: }
32: Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
33: transferable, new DummyClipboardOwner());
34: return true;
35: }
36:
37: private Transferable createTransferable(final PlugInContext context) {
38: return new AbstractTransferable(
39: new DataFlavor[] { DataFlavor.imageFlavor }) {
40: public Object getTransferData(DataFlavor flavor)
41: throws UnsupportedFlavorException, IOException {
42: Assert.isTrue(flavor == DataFlavor.imageFlavor);
43: return image(context.getLayerViewPanel());
44: }
45: };
46: }
47:
48: public static MultiEnableCheck createEnableCheck(
49: WorkbenchContext workbenchContext) {
50: EnableCheckFactory checkFactory = new EnableCheckFactory(
51: workbenchContext);
52: return new MultiEnableCheck()
53: .add(
54: checkFactory
55: .createWindowWithLayerViewPanelMustBeActiveCheck())
56: .add(new EnableCheck() {
57: public String check(JComponent component) {
58: //Need Java 1.4's ability to auto-convert DataFlavor.imageFlavor to
59: //the native image format for the platform
60: //(see http://access1.sun.com/tutorials/Swing_Tutorial/Dnd-Merlin-Tutorial/3.html).
61: //[Jon Aquino 11/6/2003]
62: return !java14OrNewer() ? I18N
63: .get("ui.plugin.clipboard.CopyImagePlugIn.this-feature-requires-Java-1.4-or-newer")
64: : null;
65: }
66: });
67: }
68: }
|