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.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.io.StringReader;
038: import java.util.Iterator;
039:
040: import com.vividsolutions.jts.operation.valid.IsValidOp;
041: import com.vividsolutions.jump.I18N;
042: import com.vividsolutions.jump.feature.Feature;
043: import com.vividsolutions.jump.feature.FeatureCollection;
044: import com.vividsolutions.jump.io.WKTReader;
045: import com.vividsolutions.jump.workbench.WorkbenchException;
046: import com.vividsolutions.jump.workbench.model.Layer;
047: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
048: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
049: import com.vividsolutions.jump.workbench.ui.EditTransaction;
050: import com.vividsolutions.jump.workbench.ui.EnterWKTDialog;
051: import com.vividsolutions.jump.workbench.ui.GUIUtil;
052:
053: /**
054: * Base class for PlugIns that ask the user to enter Well-Known Text.
055: */
056: public abstract class WKTPlugIn extends AbstractPlugIn {
057: protected Layer layer;
058:
059: public WKTPlugIn() {
060: }
061:
062: private void validate(FeatureCollection c, PlugInContext context)
063: throws WorkbenchException {
064: for (Iterator i = c.iterator(); i.hasNext();) {
065: Feature f = (Feature) i.next();
066: IsValidOp op = new IsValidOp(f.getGeometry());
067: if (!op.isValid()) {
068: if (context
069: .getWorkbenchContext()
070: .getWorkbench()
071: .getBlackboard()
072: .get(
073: EditTransaction.ROLLING_BACK_INVALID_EDITS_KEY,
074: false)) {
075: throw new WorkbenchException(op
076: .getValidationError().getMessage());
077: }
078: context.getWorkbenchFrame().warnUser(
079: op.getValidationError().getMessage());
080: }
081: }
082: }
083:
084: protected abstract Layer layer(PlugInContext context);
085:
086: public boolean execute(PlugInContext context) throws Exception {
087: layer = layer(context);
088: EnterWKTDialog d = createDialog(context);
089: d.setVisible(true);
090: return d.wasOKPressed();
091: }
092:
093: protected abstract void apply(FeatureCollection c,
094: PlugInContext context) throws WorkbenchException;
095:
096: protected EnterWKTDialog createDialog(final PlugInContext context) {
097: final EnterWKTDialog d = new EnterWKTDialog(context
098: .getWorkbenchFrame(), I18N
099: .get("ui.plugin.WKTPlugIn.enter-well-known-text"), true);
100: d.setSize(500, 400);
101: d.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: try {
104: if (d.wasOKPressed()) {
105: apply(d.getText(), context);
106: }
107: d.setVisible(false);
108: } catch (Throwable t) {
109: context.getErrorHandler().handleThrowable(t);
110: }
111: }
112: });
113: GUIUtil.centreOnWindow(d);
114: return d;
115: }
116:
117: protected void apply(String wkt, PlugInContext context)
118: throws Exception {
119: StringReader stringReader = new StringReader(wkt);
120: try {
121: WKTReader wktReader = new WKTReader();
122: FeatureCollection c = wktReader.read(stringReader);
123: validate(c, context);
124: apply(c, context);
125: } finally {
126: stringReader.close();
127: }
128: }
129: }
|