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.cursortool.editing;
034:
035: import java.awt.geom.NoninvertibleTransformException;
036:
037: import javax.swing.Icon;
038:
039: import com.vividsolutions.jts.geom.GeometryFactory;
040: import com.vividsolutions.jts.geom.LineString;
041: import com.vividsolutions.jts.operation.valid.IsValidOp;
042: import com.vividsolutions.jump.I18N;
043: import com.vividsolutions.jump.workbench.ui.EditTransaction;
044: import com.vividsolutions.jump.workbench.ui.LayerNamePanelProxy;
045: import com.vividsolutions.jump.workbench.ui.cursortool.*;
046: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
047:
048: public class DrawLineStringTool extends MultiClickTool {
049: private FeatureDrawingUtil featureDrawingUtil;
050:
051: private DrawLineStringTool(FeatureDrawingUtil featureDrawingUtil) {
052: this .featureDrawingUtil = featureDrawingUtil;
053: setMetricsDisplay(new CoordinateListMetrics());
054: }
055:
056: public static CursorTool create(
057: LayerNamePanelProxy layerNamePanelProxy) {
058: FeatureDrawingUtil featureDrawingUtil = new FeatureDrawingUtil(
059: layerNamePanelProxy);
060:
061: return featureDrawingUtil.prepare(new DrawLineStringTool(
062: featureDrawingUtil), true);
063: }
064:
065: public String getName() {
066: //Specify name explicitly, otherwise it will be "Draw Line String" [Jon Aquino]
067: return I18N
068: .get("ui.cursortool.editing.DrawLineString.draw-linestring");
069: }
070:
071: public Icon getIcon() {
072: return IconLoader.icon("DrawLineString.gif");
073: }
074:
075: protected void gestureFinished() throws Exception {
076: reportNothingToUndoYet();
077:
078: if (!checkLineString()) {
079: return;
080: }
081:
082: execute(featureDrawingUtil.createAddCommand(getLineString(),
083: isRollingBackInvalidEdits(), getPanel(), this ));
084: }
085:
086: protected LineString getLineString()
087: throws NoninvertibleTransformException {
088: return new GeometryFactory()
089: .createLineString(toArray(getCoordinates()));
090: }
091:
092: protected boolean checkLineString()
093: throws NoninvertibleTransformException {
094: if (getCoordinates().size() < 2) {
095: getPanel()
096: .getContext()
097: .warnUser(
098: I18N
099: .get("ui.cursortool.editing.DrawLineString.the-linestring-must-have-at-least-2-points"));
100:
101: return false;
102: }
103:
104: IsValidOp isValidOp = new IsValidOp(getLineString());
105:
106: if (!isValidOp.isValid()) {
107: getPanel().getContext().warnUser(
108: isValidOp.getValidationError().getMessage());
109:
110: if (getWorkbench().getBlackboard().get(
111: EditTransaction.ROLLING_BACK_INVALID_EDITS_KEY,
112: false)) {
113: return false;
114: }
115: }
116:
117: return true;
118: }
119: }
|