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.tools;
035:
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import org.openjump.core.geomutils.GeoUtils;
040:
041: import com.vividsolutions.jts.geom.CoordinateList;
042: import com.vividsolutions.jts.geom.Geometry;
043: import com.vividsolutions.jts.geom.GeometryFactory;
044: import com.vividsolutions.jts.geom.LineString;
045: import com.vividsolutions.jts.util.UniqueCoordinateArrayFilter;
046: import com.vividsolutions.jump.I18N;
047: import com.vividsolutions.jump.feature.AttributeType;
048: import com.vividsolutions.jump.feature.BasicFeature;
049: import com.vividsolutions.jump.feature.Feature;
050: import com.vividsolutions.jump.feature.FeatureDataset;
051: import com.vividsolutions.jump.feature.FeatureSchema;
052: import com.vividsolutions.jump.workbench.WorkbenchContext;
053: import com.vividsolutions.jump.workbench.model.LayerManager;
054: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
055: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
056: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
057: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
058: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
059: import com.vividsolutions.jump.workbench.ui.MenuNames;
060: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
061:
062: public class ConvexHullPlugIn extends AbstractPlugIn {
063: private WorkbenchContext workbenchContext;
064: private String TOLERANCE = I18N
065: .get("org.openjump.core.ui.plugin.tools.ConvexHullPlugIn.Tolerance");
066: private MultiInputDialog dialog;
067: private double blendTolerance = 0.1;
068: private boolean exceptionThrown = false;
069: private String sConvexHull = I18N
070: .get("org.openjump.core.ui.plugin.tools.ConvexHullPlugIn.Convex-Hull");
071:
072: public void initialize(PlugInContext context) throws Exception {
073: workbenchContext = context.getWorkbenchContext();
074: context.getFeatureInstaller().addMainMenuItemWithJava14Fix(
075: this ,
076: new String[] { MenuNames.TOOLS,
077: MenuNames.TOOLS_GENERATE },
078: getName() + "{pos:2}", false, null,
079: this .createEnableCheck(workbenchContext));
080: }
081:
082: public String getName() {
083: return sConvexHull;
084: }
085:
086: public boolean execute(final PlugInContext context)
087: throws Exception {
088: TOLERANCE = I18N
089: .get("org.openjump.core.ui.plugin.tools.ConvexHullPlugIn.Tolerance");
090: sConvexHull = I18N
091: .get("org.openjump.core.ui.plugin.tools.ConvexHullPlugIn.Convex-Hull");
092:
093: reportNothingToUndoYet(context);
094: Collection selectedFeatures = context.getLayerViewPanel()
095: .getSelectionManager().getFeaturesWithSelectedItems();
096: Collection selectedCategories = context.getLayerNamePanel()
097: .getSelectedCategories();
098: LayerManager layerManager = context.getLayerManager();
099: FeatureSchema featureSchema = new FeatureSchema();
100: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
101: CoordinateList coords = new CoordinateList();
102:
103: for (Iterator j = selectedFeatures.iterator(); j.hasNext();) {
104: Feature f = (Feature) j.next();
105: Geometry geo = f.getGeometry();
106: UniqueCoordinateArrayFilter filter = new UniqueCoordinateArrayFilter();
107: geo.apply(filter);
108: coords.add(filter.getCoordinates(), false);
109: }
110:
111: CoordinateList convexHullCoords = GeoUtils
112: .ConvexHullWrap(coords);
113: LineString convexHull = new GeometryFactory()
114: .createLineString(convexHullCoords.toCoordinateArray());
115:
116: Feature newFeature = new BasicFeature(featureSchema);
117: newFeature.setGeometry(convexHull);
118: FeatureDataset newFeatures = new FeatureDataset(featureSchema);
119: newFeatures.add(newFeature);
120:
121: layerManager
122: .addLayer(
123: selectedCategories.isEmpty() ? StandardCategoryNames.WORKING
124: : selectedCategories.iterator().next()
125: .toString(), layerManager
126: .uniqueLayerName(sConvexHull),
127: newFeatures);
128:
129: layerManager.getLayer(0).setFeatureCollectionModified(true);
130: layerManager.getLayer(0).setEditable(true);
131:
132: return true;
133: }
134:
135: public MultiEnableCheck createEnableCheck(
136: final WorkbenchContext workbenchContext) {
137: EnableCheckFactory checkFactory = new EnableCheckFactory(
138: workbenchContext);
139: return new MultiEnableCheck()
140: .add(
141: checkFactory
142: .createWindowWithLayerViewPanelMustBeActiveCheck())
143: .add(
144: checkFactory
145: .createAtLeastNFeaturesMustHaveSelectedItemsCheck(1));
146: }
147: }
|