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