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.analysis;
034:
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: import javax.swing.JComboBox;
040:
041: import com.vividsolutions.jts.geom.*;
042: import com.vividsolutions.jump.I18N;
043: import com.vividsolutions.jump.feature.Feature;
044: import com.vividsolutions.jump.feature.FeatureCollection;
045: import com.vividsolutions.jump.feature.FeatureDatasetFactory;
046: import com.vividsolutions.jump.task.TaskMonitor;
047: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
048: import com.vividsolutions.jump.workbench.plugin.*;
049: import com.vividsolutions.jump.workbench.ui.GUIUtil;
050: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
051:
052: public class ConvexHullPlugIn extends AbstractPlugIn implements
053: ThreadedPlugIn {
054: private String LAYER = I18N
055: .get("ui.plugin.analysis.ConvexHullPlugIn.Source-Layer");
056: private MultiInputDialog dialog;
057:
058: public ConvexHullPlugIn() {
059: }
060:
061: private String categoryName = StandardCategoryNames.RESULT;
062:
063: public void setCategoryName(String value) {
064: categoryName = value;
065: }
066:
067: public static EnableCheck getEnableCheck(
068: EnableCheckFactory checkFactory) {
069: return new MultiEnableCheck()
070: .add(
071: checkFactory
072: .createWindowWithLayerNamePanelMustBeActiveCheck())
073: .add(checkFactory.createAtLeastNLayersMustExistCheck(1));
074: }
075:
076: public boolean execute(PlugInContext context) throws Exception {
077: //[sstein, 16.07.2006] put here again for language settings
078: LAYER = I18N
079: .get("ui.plugin.analysis.ConvexHullPlugIn.Source-Layer");
080: //Unlike ValidatePlugIn, here we always call #initDialog because we want
081: //to update the layer comboboxes. [Jon Aquino]
082: initDialog(context);
083: dialog.setVisible(true);
084:
085: if (!dialog.wasOKPressed()) {
086: return false;
087: }
088:
089: return true;
090: }
091:
092: public String getName() {
093: return I18N
094: .get("ui.plugin.analysis.ConvexHullPlugIn.Convex-Hull-on-Layer");
095: }
096:
097: private void initDialog(PlugInContext context) {
098: dialog = new MultiInputDialog(
099: context.getWorkbenchFrame(),
100: I18N
101: .get("ui.plugin.analysis.ConvexHullPlugIn.Convex-Hull-on-Layer"),
102: true);
103:
104: //dialog.setSideBarImage(IconLoader.icon("Overlay.gif"));
105: dialog
106: .setSideBarDescription(I18N
107: .get("ui.plugin.analysis.ConvexHullPlugIn.Creates-a-new-layer-containing-the-convex-hull-of-all-the-features-in-the-source-layer"));
108: String fieldName = LAYER;
109: JComboBox addLayerComboBox = dialog.addLayerComboBox(fieldName,
110: context.getCandidateLayer(0), null, context
111: .getLayerManager());
112: GUIUtil.centreOnWindow(dialog);
113: }
114:
115: public void run(TaskMonitor monitor, PlugInContext context)
116: throws Exception {
117: FeatureCollection a = dialog.getLayer(LAYER)
118: .getFeatureCollectionWrapper();
119: FeatureCollection hullFC = convexHhull(monitor, a);
120:
121: if (hullFC == null)
122: return;
123:
124: context.getLayerManager().addCategory(categoryName);
125: context
126: .addLayer(
127: categoryName,
128: I18N
129: .get("ui.plugin.analysis.ConvexHullPlugIn.Convex-Hull"),
130: hullFC);
131: }
132:
133: private FeatureCollection convexHhull(TaskMonitor monitor,
134: FeatureCollection fc) {
135: monitor.allowCancellationRequests();
136: monitor
137: .report(I18N
138: .get("ui.plugin.analysis.ConvexHullPlugIn.Computing-Convex-Hull")
139: + "...");
140:
141: int size = fc.size();
142: GeometryFactory geomFact = null;
143:
144: if (size == 0)
145: return null;
146: int count = 0;
147: Geometry[] geoms = new Geometry[size];
148:
149: for (Iterator i = fc.iterator(); i.hasNext();) {
150: Feature f = (Feature) i.next();
151: Geometry geom = f.getGeometry();
152: if (geom == null)
153: continue;
154: if (geomFact == null)
155: geomFact = geom.getFactory();
156:
157: geoms[count++] = geom;
158: }
159: GeometryCollection gc = geomFact
160: .createGeometryCollection(geoms);
161: Geometry hull = gc.convexHull();
162: List hullList = new ArrayList();
163: hullList.add(hull);
164:
165: return FeatureDatasetFactory.createFromGeometry(hullList);
166: }
167: }
|