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.*;
036: import com.vividsolutions.jts.geom.*;
037: import com.vividsolutions.jump.I18N;
038: import com.vividsolutions.jts.operation.buffer.BufferOp;
039: import com.vividsolutions.jump.feature.*;
040: import com.vividsolutions.jump.task.*;
041: import com.vividsolutions.jump.workbench.model.*;
042: import com.vividsolutions.jump.workbench.plugin.*;
043: import com.vividsolutions.jump.workbench.ui.*;
044:
045: public class BufferPlugIn extends AbstractPlugIn implements
046: ThreadedPlugIn {
047: private String LAYER = I18N
048: .get("ui.plugin.analysis.BufferPlugIn.layer");
049: private String DISTANCE = I18N
050: .get("ui.plugin.analysis.BufferPlugIn.buffer-distance");
051: private String END_CAP_STYLE = I18N
052: .get("ui.plugin.analysis.BufferPlugIn.End-Cap-Style");
053:
054: private String CAP_STYLE_ROUND = I18N
055: .get("ui.plugin.analysis.BufferPlugIn.Round");
056: private String CAP_STYLE_SQUARE = I18N
057: .get("ui.plugin.analysis.BufferPlugIn.Square");
058: private String CAP_STYLE_BUTT = I18N
059: .get("ui.plugin.analysis.BufferPlugIn.Butt");
060:
061: private List endCapStyles = new ArrayList();
062:
063: private MultiInputDialog dialog;
064: private Layer layer;
065: private double bufferDistance = 1.0;
066: private String endCapStyle = CAP_STYLE_ROUND;
067: private boolean exceptionThrown = false;
068:
069: public BufferPlugIn() {
070: endCapStyles.add(CAP_STYLE_ROUND);
071: endCapStyles.add(CAP_STYLE_SQUARE);
072: endCapStyles.add(CAP_STYLE_BUTT);
073: }
074:
075: private String categoryName = StandardCategoryNames.RESULT;
076:
077: public void setCategoryName(String value) {
078: categoryName = value;
079: }
080:
081: public boolean execute(PlugInContext context) throws Exception {
082: //[sstein, 16.07.2006] set again to obtain correct language
083: LAYER = I18N.get("ui.plugin.analysis.BufferPlugIn.layer");
084: DISTANCE = I18N
085: .get("ui.plugin.analysis.BufferPlugIn.buffer-distance");
086: END_CAP_STYLE = I18N
087: .get("ui.plugin.analysis.BufferPlugIn.End-Cap-Style");
088:
089: CAP_STYLE_ROUND = I18N
090: .get("ui.plugin.analysis.BufferPlugIn.Round");
091: CAP_STYLE_SQUARE = I18N
092: .get("ui.plugin.analysis.BufferPlugIn.Square");
093: CAP_STYLE_BUTT = I18N
094: .get("ui.plugin.analysis.BufferPlugIn.Butt");
095:
096: MultiInputDialog dialog = new MultiInputDialog(context
097: .getWorkbenchFrame(), getName(), true);
098: setDialogValues(dialog, context);
099: GUIUtil.centreOnWindow(dialog);
100: dialog.setVisible(true);
101: if (!dialog.wasOKPressed()) {
102: return false;
103: }
104: getDialogValues(dialog);
105: return true;
106: }
107:
108: public void run(TaskMonitor monitor, PlugInContext context)
109: throws Exception {
110: FeatureSchema featureSchema = new FeatureSchema();
111: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
112: FeatureCollection resultFC = new FeatureDataset(featureSchema);
113:
114: Collection resultColl = runBuffer(layer
115: .getFeatureCollectionWrapper());
116: resultFC = FeatureDatasetFactory.createFromGeometry(resultColl);
117: context.getLayerManager().addCategory(categoryName);
118: context
119: .addLayer(
120: categoryName,
121: I18N
122: .get("com.vividsolutions.jump.workbench.ui.plugin.analysis.BufferPlugIn")
123: + "-" + layer.getName(), resultFC);
124: if (exceptionThrown)
125: context
126: .getWorkbenchFrame()
127: .warnUser(
128: I18N
129: .get("ui.plugin.analysis.BufferPlugIn.errors-found-while-executing-buffer"));
130: }
131:
132: private Collection runBuffer(FeatureCollection fcA) {
133: exceptionThrown = false;
134: Collection resultColl = new ArrayList();
135: for (Iterator ia = fcA.iterator(); ia.hasNext();) {
136: Feature fa = (Feature) ia.next();
137: Geometry ga = fa.getGeometry();
138: Geometry result = runBuffer(ga);
139: if (result != null)
140: resultColl.add(result);
141: }
142: return resultColl;
143: }
144:
145: private Geometry runBuffer(Geometry a) {
146: Geometry result = null;
147: try {
148: BufferOp bufOp = new BufferOp(a);
149: bufOp.setEndCapStyle(endCapStyleCode(endCapStyle));
150: result = bufOp.getResultGeometry(bufferDistance);
151: //result = a.buffer(bufferDistance);
152: return result;
153: } catch (RuntimeException ex) {
154: // simply eat exceptions and report them by returning null
155: exceptionThrown = true;
156: }
157: return null;
158: }
159:
160: private void setDialogValues(MultiInputDialog dialog,
161: PlugInContext context) {
162: //dialog.setSideBarImage(new ImageIcon(getClass().getResource("DiffSegments.png")));
163: dialog
164: .setSideBarDescription(I18N
165: .get("ui.plugin.analysis.BufferPlugIn.buffers-all-geometries-in-the-input-layer"));
166: //Initial layer value is null
167: dialog.addLayerComboBox(LAYER, context.getCandidateLayer(0),
168: context.getLayerManager());
169: dialog.addDoubleField(DISTANCE, bufferDistance, 10, null);
170: dialog.addComboBox(END_CAP_STYLE, endCapStyle, endCapStyles,
171: null);
172: }
173:
174: private void getDialogValues(MultiInputDialog dialog) {
175: layer = dialog.getLayer(LAYER);
176: bufferDistance = dialog.getDouble(DISTANCE);
177: endCapStyle = dialog.getText(END_CAP_STYLE);
178: }
179:
180: private static int endCapStyleCode(String capStyle) {
181: String CAP_STYLE_SQUARE = I18N
182: .get("ui.plugin.analysis.BufferPlugIn.Square");
183: String CAP_STYLE_BUTT = I18N
184: .get("ui.plugin.analysis.BufferPlugIn.Butt");
185:
186: if (capStyle == CAP_STYLE_BUTT)
187: return BufferOp.CAP_BUTT;
188: if (capStyle == CAP_STYLE_SQUARE)
189: return BufferOp.CAP_SQUARE;
190: return BufferOp.CAP_ROUND;
191: }
192: }
|