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: import java.util.List;
039:
040: import javax.swing.JComponent;
041:
042: import com.vividsolutions.jts.geom.GeometryCollection;
043: import com.vividsolutions.jump.feature.Feature;
044: import com.vividsolutions.jump.util.StringUtil;
045: import com.vividsolutions.jump.workbench.WorkbenchContext;
046: import com.vividsolutions.jump.workbench.model.Layer;
047: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
048: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
049: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
050: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
051: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
052: import com.vividsolutions.jump.workbench.ui.EditTransaction;
053:
054: public class ExplodeSelectedFeaturesPlugIn extends AbstractPlugIn {
055: public boolean execute(final PlugInContext context)
056: throws Exception {
057: final ArrayList transactions = new ArrayList();
058: for (Iterator i = context.getLayerViewPanel()
059: .getSelectionManager().getLayersWithSelectedItems()
060: .iterator(); i.hasNext();) {
061: Layer layerWithSelectedItems = (Layer) i.next();
062: transactions.add(createTransaction(layerWithSelectedItems,
063: context));
064: }
065: return EditTransaction.commit(transactions,
066: new EditTransaction.SuccessAction() {
067: public void run() {
068: //SuccessActions don't run after redos, which is the behaviour we want here. [Jon Aquino]
069: for (Iterator i = transactions.iterator(); i
070: .hasNext();) {
071: EditTransaction transaction = (EditTransaction) i
072: .next();
073: context.getLayerViewPanel()
074: .getSelectionManager()
075: .getFeatureSelection().selectItems(
076: transaction.getLayer(),
077: newFeatures(transaction));
078: }
079: }
080:
081: });
082: }
083:
084: private Collection newFeatures(EditTransaction transaction) {
085: ArrayList newFeatures = new ArrayList();
086: for (int i = 0; i < transaction.size(); i++) {
087: if (!transaction.getGeometry(i).isEmpty()) {
088: newFeatures.add(transaction.getFeature(i));
089: }
090: }
091: return newFeatures;
092: }
093:
094: private EditTransaction createTransaction(Layer layer,
095: PlugInContext context) {
096: Collection intactFeatures = context.getLayerViewPanel()
097: .getSelectionManager().getFeaturesWithSelectedItems(
098: layer);
099: EditTransaction transaction = new EditTransaction(
100: new ArrayList(), getName(), layer,
101: isRollingBackInvalidEdits(context), true, context
102: .getLayerViewPanel());
103: for (Iterator i = intactFeatures.iterator(); i.hasNext();) {
104: Feature intactFeature = (Feature) i.next();
105: transaction.deleteFeature(intactFeature);
106: }
107: for (Iterator i = explode(intactFeatures).iterator(); i
108: .hasNext();) {
109: Feature explodedFeature = (Feature) i.next();
110: transaction.createFeature(explodedFeature);
111: }
112: return transaction;
113: }
114:
115: private List explode(Collection features) {
116: ArrayList explodedFeatures = new ArrayList();
117: for (Iterator i = features.iterator(); i.hasNext();) {
118: Feature feature = (Feature) i.next();
119: GeometryCollection collection = (GeometryCollection) feature
120: .getGeometry();
121: for (int j = 0; j < collection.getNumGeometries(); j++) {
122: Feature explodedFeature = (Feature) feature.clone();
123: explodedFeature.setGeometry(collection.getGeometryN(j));
124: explodedFeatures.add(explodedFeature);
125: }
126: }
127: return explodedFeatures;
128: }
129:
130: public MultiEnableCheck createEnableCheck(
131: final WorkbenchContext workbenchContext) {
132: EnableCheckFactory checkFactory = new EnableCheckFactory(
133: workbenchContext);
134: return new MultiEnableCheck()
135: .add(
136: checkFactory
137: .createWindowWithLayerViewPanelMustBeActiveCheck())
138: .add(
139: checkFactory
140: .createAtLeastNFeaturesMustHaveSelectedItemsCheck(1))
141: .add(
142: checkFactory
143: .createSelectedItemsLayersMustBeEditableCheck())
144: .add(new EnableCheck() {
145: public String check(JComponent component) {
146: Collection featuresWithSelectedItems = workbenchContext
147: .getLayerViewPanel()
148: .getSelectionManager()
149: .getFeaturesWithSelectedItems();
150: for (Iterator i = featuresWithSelectedItems
151: .iterator(); i.hasNext();) {
152: Feature feature = (Feature) i.next();
153: if (!(feature.getGeometry() instanceof GeometryCollection)) {
154: return "Selected feature"
155: + StringUtil
156: .s(featuresWithSelectedItems
157: .size())
158: + " must be geometry collection"
159: + StringUtil
160: .s(featuresWithSelectedItems
161: .size());
162: }
163: }
164: return null;
165: }
166: });
167: }
168: }
|