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:
033: package com.vividsolutions.jump.workbench.ui.plugin;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import javax.swing.JComponent;
040:
041: import com.vividsolutions.jump.I18N;
042: import com.vividsolutions.jump.feature.Feature;
043: import com.vividsolutions.jump.util.StringUtil;
044: import com.vividsolutions.jump.warp.Triangulator;
045: import com.vividsolutions.jump.workbench.WorkbenchContext;
046: import com.vividsolutions.jump.workbench.model.Layer;
047: import com.vividsolutions.jump.workbench.model.UndoableCommand;
048: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
049: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
050: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
051: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
052: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
053: import com.vividsolutions.jump.workbench.ui.LayerViewPanelContext;
054: import com.vividsolutions.jump.workbench.ui.plugin.clipboard.PasteItemsPlugIn;
055: import com.vividsolutions.jump.workbench.ui.warp.WarpingVectorLayerFinder;
056:
057: public class CopySelectedLayersToWarpingVectorsPlugIn extends
058: AbstractPlugIn {
059:
060: public EnableCheck createEnableCheck(
061: final WorkbenchContext workbenchContext) {
062: EnableCheckFactory checkFactory = new EnableCheckFactory(
063: workbenchContext);
064: return new MultiEnableCheck()
065: .add(checkFactory.createTaskWindowMustBeActiveCheck())
066: .add(
067: checkFactory
068: .createAtLeastNLayersMustBeSelectedCheck(1))
069: .add(new EnableCheck() {
070: public String check(JComponent component) {
071: return workbenchContext.getLayerNamePanel()
072: .getSelectedLayers().length == 1
073: && workbenchContext.getLayerNamePanel()
074: .getSelectedLayers()[0] == new WarpingVectorLayerFinder(
075: workbenchContext).getLayer() ? I18N
076: .get("ui.plugin.CopySelectedLayersToWarpingVectorsPlugIn.a-layer-other-than")
077: + "'"
078: + new WarpingVectorLayerFinder(
079: workbenchContext)
080: .getLayerName()
081: + "' "
082: + I18N
083: .get("ui.plugin.CopySelectedLayersToWarpingVectorsPlugIn.must-be-selected")
084: : null;
085: }
086: });
087: }
088:
089: public static Collection removeNonVectorFeaturesAndWarn(
090: Collection features, LayerViewPanelContext context) {
091: ArrayList newFeatures = new ArrayList(features);
092: Collection nonVectorFeatures = nonVectorFeatures(newFeatures);
093: if (!nonVectorFeatures.isEmpty()) {
094: context
095: .warnUser(I18N
096: .get("ui.plugin.CopySelectedLayersToWarpingVectorsPlugIn.skipped")
097: + " "
098: + nonVectorFeatures.size()
099: + " non-two-point-linestring"
100: + StringUtil.s(nonVectorFeatures.size())
101: + " e.g. "
102: + ((Feature) nonVectorFeatures.iterator()
103: .next()).getGeometry().toText());
104: newFeatures.removeAll(nonVectorFeatures);
105: }
106: return newFeatures;
107: }
108:
109: public boolean execute(PlugInContext context) throws Exception {
110: reportNothingToUndoYet(context);
111: Collection newWarpingVectors = new ArrayList();
112: Layer[] selectedLayers = context.getSelectedLayers();
113: for (int i = 0; i < selectedLayers.length; i++) {
114: if (selectedLayers[i] == new WarpingVectorLayerFinder(
115: context).getLayer()) {
116: continue;
117: }
118: newWarpingVectors.addAll(selectedLayers[i]
119: .getFeatureCollectionWrapper().getFeatures());
120: }
121: newWarpingVectors = removeNonVectorFeaturesAndWarn(
122: newWarpingVectors, context.getWorkbenchFrame());
123: final Collection finalNewWarpingVectors = newWarpingVectors;
124: final WarpingVectorLayerFinder finder = new WarpingVectorLayerFinder(
125: context);
126: execute(Layer.addUndo(finder.getLayerName(), context,
127: new UndoableCommand(getName()) {
128: public void execute() {
129: if (finder.getLayer() == null) {
130: finder.createLayer();
131: }
132: finder
133: .getLayer()
134: .getFeatureCollectionWrapper()
135: .addAll(
136: PasteItemsPlugIn
137: .conform(
138: finalNewWarpingVectors,
139: finder
140: .getLayer()
141: .getFeatureCollectionWrapper()
142: .getFeatureSchema()));
143: }
144:
145: public void unexecute() {
146: }
147: }), context);
148: return true;
149: }
150:
151: private static Collection nonVectorFeatures(Collection candidates) {
152: ArrayList nonVectorFeatures = new ArrayList();
153: for (Iterator i = candidates.iterator(); i.hasNext();) {
154: Feature candidate = (Feature) i.next();
155: if (!Triangulator.vector(candidate.getGeometry())) {
156: nonVectorFeatures.add(candidate);
157: }
158: }
159: return nonVectorFeatures;
160: }
161:
162: }
|