001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui.plugin.clipboard;
033:
034: import com.vividsolutions.jump.util.StringUtil;
035: import com.vividsolutions.jump.workbench.WorkbenchContext;
036: import com.vividsolutions.jump.workbench.model.Category;
037: import com.vividsolutions.jump.workbench.model.Layerable;
038: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
039: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
040: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
041: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
042: import com.vividsolutions.jump.workbench.ui.GUIUtil;
043:
044: import java.awt.Toolkit;
045: import java.awt.datatransfer.Transferable;
046:
047: import java.util.Collection;
048: import java.util.Iterator;
049:
050: import javax.swing.JComponent;
051:
052: /**
053: *
054: * Lets user paste layers from the clipboard.
055: *
056: */
057:
058: public class PasteLayersPlugIn extends LayerableClipboardPlugIn {
059: //Note: Need to copy the data twice: once when the user hits Copy, so she is
060: //free to modify the original afterwards, and again when the user hits Paste,
061: //so she is free to modify the first copy then hit Paste again. [Jon Aquino]
062: public PasteLayersPlugIn() {
063: }
064:
065: public String getNameWithMnemonic() {
066: return StringUtil.replace(getName(), "P", "&P", false);
067: }
068:
069: public boolean execute(PlugInContext context) throws Exception {
070: Transferable transferable = GUIUtil.getContents(Toolkit
071: .getDefaultToolkit().getSystemClipboard());
072:
073: if (!transferable
074: .isDataFlavorSupported(CollectionOfLayerablesTransferable.COLLECTION_OF_LAYERABLES_FLAVOR)) {
075: return false;
076: }
077:
078: Collection layerables = (Collection) transferable
079: .getTransferData(CollectionOfLayerablesTransferable.COLLECTION_OF_LAYERABLES_FLAVOR);
080: //Cache selected category because selection will change (to layer) after adding first layer
081: //if no other layers exist. [Jon Aquino]
082: Category selectedCategory = ((Category) context
083: .getLayerNamePanel().getSelectedCategories().iterator()
084: .next());
085: for (Iterator i = layerables.iterator(); i.hasNext();) {
086: Layerable layerable = (Layerable) i.next();
087: Layerable clone = cloneLayerable(layerable);
088: clone.setLayerManager(context.getLayerManager());
089: context.getLayerManager().addLayerable(
090: selectedCategory.getName(), clone);
091: clone.setName(context.getLayerManager().uniqueLayerName(
092: clone.getName()));
093: }
094:
095: return true;
096: }
097:
098: public MultiEnableCheck createEnableCheck(
099: WorkbenchContext workbenchContext) {
100: EnableCheckFactory checkFactory = new EnableCheckFactory(
101: workbenchContext);
102:
103: return new MultiEnableCheck()
104: .add(
105: checkFactory
106: .createWindowWithLayerNamePanelMustBeActiveCheck())
107: .add(
108: checkFactory
109: .createExactlyNCategoriesMustBeSelectedCheck(1))
110: .add(new EnableCheck() {
111: public String check(JComponent component) {
112: Transferable transferable = GUIUtil
113: .getContents(Toolkit
114: .getDefaultToolkit()
115: .getSystemClipboard());
116:
117: if (transferable == null) {
118: return "Clipboard must not be empty";
119: }
120:
121: if (!transferable
122: .isDataFlavorSupported(CollectionOfLayerablesTransferable.COLLECTION_OF_LAYERABLES_FLAVOR)) {
123: return "Clipboard contents must be layers";
124: }
125:
126: return null;
127: }
128: });
129: }
130: }
|