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: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) 2004 Integrated Systems Analysts, Inc.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: *
026: * Integrated Systems Analysts, Inc.
027: * 630C Anchors St., Suite 101
028: * Fort Walton Beach, Florida
029: * USA
030: *
031: * (850)862-7321
032: */
033:
034: package org.openjump.core.ui.plugin.edit;
035:
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.Iterator;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041: import com.vividsolutions.jts.geom.GeometryCollection;
042: import com.vividsolutions.jts.geom.LineString;
043: import com.vividsolutions.jts.geom.LinearRing;
044: import com.vividsolutions.jts.geom.MultiLineString;
045: import com.vividsolutions.jts.geom.MultiPoint;
046: import com.vividsolutions.jts.geom.MultiPolygon;
047: import com.vividsolutions.jts.geom.Point;
048: import com.vividsolutions.jts.geom.Polygon;
049: import com.vividsolutions.jump.I18N;
050: import com.vividsolutions.jump.feature.Feature;
051: import com.vividsolutions.jump.feature.FeatureCollection;
052: import com.vividsolutions.jump.workbench.WorkbenchContext;
053: import com.vividsolutions.jump.workbench.model.Layer;
054: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
055: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
056: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
057: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
058: import com.vividsolutions.jump.workbench.ui.AbstractSelection;
059: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
060: import com.vividsolutions.jump.workbench.ui.MenuNames;
061: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
062:
063: public class SelectByTypePlugIn extends AbstractPlugIn {
064: private WorkbenchContext workbenchContext;
065: private boolean selectEmpty = false;
066: private boolean selectPoint = false;
067: private boolean selectMultiPoint = false;
068: private boolean selectLineString = false;
069: private boolean selectLinearRing = false;
070: private boolean selectMultiLineString = false;
071: private boolean selectPolygon = false;
072: private boolean selectMultiPolygon = false;
073: private boolean selectGeometryCollection = false;
074: private boolean selectedLayersOnly = true;
075: protected AbstractSelection selection;
076:
077: final static String sSelectByGeometryType = I18N
078: .get("org.openjump.core.ui.plugin.edit.SelectByTypePlugIn.Select-by-Geometry-Type");
079: final static String sSelectOnlyTheseTypes = I18N
080: .get("org.openjump.core.ui.plugin.edit.SelectByTypePlugIn.Select-only-these-types");
081: final static String sEmptyGeometries = I18N
082: .get("org.openjump.core.ui.plugin.edit.SelectByTypePlugIn.Empty-Geometries");
083: final static String sOnSelectedLayersOnly = I18N
084: .get("org.openjump.core.ui.plugin.edit.SelectByTypePlugIn.On-selected-layers-only");
085:
086: public void initialize(PlugInContext context) throws Exception {
087: workbenchContext = context.getWorkbenchContext();
088: context.getFeatureInstaller().addMainMenuItemWithJava14Fix(
089: this , new String[] { MenuNames.EDIT },
090: sSelectByGeometryType + "..." + "{pos:8}", false, null,
091: this .createEnableCheck(workbenchContext));
092: }
093:
094: public boolean execute(final PlugInContext context)
095: throws Exception {
096: reportNothingToUndoYet(context);
097: MultiInputDialog dialog = new MultiInputDialog(context
098: .getWorkbenchFrame(), getName(), true);
099: setDialogValues(dialog, context);
100: dialog.setVisible(true);
101:
102: if (!dialog.wasOKPressed()) {
103: return false;
104: }
105:
106: getDialogValues(dialog);
107: LayerViewPanel layerViewPanel = context.getWorkbenchContext()
108: .getLayerViewPanel();
109: ArrayList selectedFeatures = new ArrayList();
110:
111: layerViewPanel.getSelectionManager().clear();
112: Collection layers;
113:
114: if (selectedLayersOnly)
115: layers = (Collection) context.getWorkbenchContext()
116: .getLayerNamePanel().selectedNodes(Layer.class);
117: else
118: layers = (Collection) context.getWorkbenchContext()
119: .getLayerNamePanel().getLayerManager().getLayers();
120:
121: for (Iterator j = layers.iterator(); j.hasNext();) {
122: Layer layer = (Layer) j.next();
123: selectedFeatures.clear();
124:
125: if (layer.isVisible()) {
126: FeatureCollection featureCollection = layer
127: .getFeatureCollectionWrapper();
128: for (Iterator i = featureCollection.iterator(); i
129: .hasNext();) {
130: Feature feature = (Feature) i.next();
131: if (selectFeature(feature)) {
132: selectedFeatures.add(feature);
133: }
134: }
135: }
136: if (selectedFeatures.size() > 0)
137: layerViewPanel.getSelectionManager()
138: .getFeatureSelection().selectItems(layer,
139: selectedFeatures);
140: }
141: return true;
142: }
143:
144: private boolean selectFeature(Feature feature) {
145: Geometry geo = feature.getGeometry();
146:
147: if (selectPoint && (geo instanceof Point))
148: return true;
149: else if (selectMultiPoint && (geo instanceof MultiPoint))
150: return true;
151: else if (selectLineString && (geo instanceof LineString))
152: return true;
153: else if (selectLinearRing && (geo instanceof LinearRing))
154: return true;
155: else if (selectMultiLineString
156: && (geo instanceof MultiLineString))
157: return true;
158: else if (selectPolygon && (geo instanceof Polygon))
159: return true;
160: else if (selectMultiPolygon && (geo instanceof MultiPolygon))
161: return true;
162: else if (selectGeometryCollection
163: && (geo instanceof GeometryCollection))
164: return true;
165: else if (selectEmpty && geo.isEmpty())
166: return true;
167: return false;
168: }
169:
170: private void setDialogValues(MultiInputDialog dialog,
171: PlugInContext context) {
172: dialog.addLabel(sSelectOnlyTheseTypes);
173: dialog.addCheckBox(sEmptyGeometries, selectEmpty);
174: dialog.addCheckBox("Point", selectPoint);
175: dialog.addCheckBox("MultiPoint", selectMultiPoint);
176: dialog.addCheckBox("LineString", selectLineString);
177: dialog.addCheckBox("LinearRing", selectLinearRing);
178: dialog.addCheckBox("MultiLineString", selectMultiLineString);
179: dialog.addCheckBox("Polygon", selectPolygon);
180: dialog.addCheckBox("MultiPolygon", selectMultiPolygon);
181: dialog.addCheckBox("GeometryCollection",
182: selectGeometryCollection);
183: dialog.addCheckBox(sOnSelectedLayersOnly, selectedLayersOnly);
184: }
185:
186: private void getDialogValues(MultiInputDialog dialog) {
187: selectEmpty = dialog.getCheckBox(sEmptyGeometries).isSelected();
188: selectPoint = dialog.getCheckBox("Point").isSelected();
189: selectMultiPoint = dialog.getCheckBox("MultiPoint")
190: .isSelected();
191: selectLineString = dialog.getCheckBox("LineString")
192: .isSelected();
193: selectLinearRing = dialog.getCheckBox("LinearRing")
194: .isSelected();
195: selectMultiLineString = dialog.getCheckBox("MultiLineString")
196: .isSelected();
197: selectPolygon = dialog.getCheckBox("Polygon").isSelected();
198: selectMultiPolygon = dialog.getCheckBox("MultiPolygon")
199: .isSelected();
200: selectGeometryCollection = dialog.getCheckBox(
201: "GeometryCollection").isSelected();
202: selectedLayersOnly = dialog.getCheckBox(sOnSelectedLayersOnly)
203: .isSelected();
204: }
205:
206: public MultiEnableCheck createEnableCheck(
207: final WorkbenchContext workbenchContext) {
208: EnableCheckFactory checkFactory = new EnableCheckFactory(
209: workbenchContext);
210: return new MultiEnableCheck()
211: .add(
212: checkFactory
213: .createWindowWithLayerViewPanelMustBeActiveCheck())
214: .add(checkFactory.createAtLeastNLayersMustExistCheck(1));
215: }
216: }
|