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: package com.vividsolutions.jump.workbench.ui.cursortool.editing;
033:
034: import java.awt.Color;
035: import java.awt.Cursor;
036: import java.awt.geom.NoninvertibleTransformException;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.Iterator;
040: import javax.swing.Icon;
041: import com.vividsolutions.jts.geom.Geometry;
042: import com.vividsolutions.jts.util.Assert;
043: import com.vividsolutions.jump.I18N;
044: import com.vividsolutions.jump.geom.EnvelopeUtil;
045: import com.vividsolutions.jump.workbench.model.Layer;
046: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
047: import com.vividsolutions.jump.workbench.ui.EditTransaction;
048: import com.vividsolutions.jump.workbench.ui.GeometryEditor;
049: import com.vividsolutions.jump.workbench.ui.cursortool.Animations;
050: import com.vividsolutions.jump.workbench.ui.cursortool.SpecifyFeaturesTool;
051: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
052: import com.vividsolutions.jump.workbench.ui.plugin.VerticesInFencePlugIn;
053:
054: //Override SpecifyFeaturesTool rather than just DragTool so that clicks are
055: //handled. [Jon Aquino]
056: public class DeleteVertexTool extends SpecifyFeaturesTool {
057: private EnableCheckFactory checkFactory;
058: private GeometryEditor geometryEditor = new GeometryEditor();
059:
060: public DeleteVertexTool(EnableCheckFactory checkFactory) {
061: this .checkFactory = checkFactory;
062: setViewClickBuffer(5);
063: }
064:
065: protected void gestureFinished() throws java.lang.Exception {
066: reportNothingToUndoYet();
067: if (!check(checkFactory
068: .createAtLeastNItemsMustBeSelectedCheck(1))) {
069: return;
070: }
071: if (!check(checkFactory
072: .createSelectedItemsLayersMustBeEditableCheck())) {
073: return;
074: }
075: final ArrayList verticesDeleted = new ArrayList();
076: ArrayList transactions = new ArrayList();
077: for (Iterator i = getPanel().getSelectionManager()
078: .getLayersWithSelectedItems().iterator(); i.hasNext();) {
079: Layer layer = (Layer) i.next();
080: transactions.add(createTransaction(layer, verticesDeleted));
081: }
082: int emptyGeometryCount = EditTransaction
083: .emptyGeometryCount(transactions);
084: if (emptyGeometryCount > 0) {
085: getPanel()
086: .getContext()
087: .warnUser(
088: I18N
089: .get("ui.cursortool.editing.DeleteVertexTool.cancelled-deletion-would-result-in-empty-geometry"));
090: return;
091: }
092: if (verticesDeleted.isEmpty()) {
093: getPanel()
094: .getContext()
095: .warnUser(
096: I18N
097: .get("ui.cursortool.editing.DeleteVertexTool.no-selection-handles-here"));
098: return;
099: }
100: EditTransaction.commit(transactions,
101: new EditTransaction.SuccessAction() {
102: public void run() {
103: try {
104: Animations.drawExpandingRings(getPanel()
105: .getViewport().toViewPoints(
106: verticesDeleted), true,
107: Color.red, getPanel(), new float[] {
108: 15, 15 });
109: } catch (Throwable t) {
110: getPanel().getContext().warnUser(
111: t.toString());
112: }
113: }
114: });
115: }
116:
117: protected EditTransaction createTransaction(Layer layer,
118: final ArrayList verticesDeleted)
119: throws NoninvertibleTransformException {
120: final Geometry box = EnvelopeUtil
121: .toGeometry(getBoxInModelCoordinates());
122: EditTransaction transaction = EditTransaction
123: .createTransactionOnSelection(
124: new EditTransaction.SelectionEditor() {
125: public Geometry edit(
126: Geometry geometryWithSelectedItems,
127: Collection selectedItems) {
128: if (wasClick()
129: && !verticesDeleted.isEmpty()) {
130: Assert.isTrue(verticesDeleted
131: .size() == 1);
132: return geometryWithSelectedItems;
133: }
134: if (!box
135: .getEnvelopeInternal()
136: .intersects(
137: geometryWithSelectedItems
138: .getEnvelopeInternal())) {
139: return geometryWithSelectedItems;
140: }
141: Collection verticesInBox = VerticesInFencePlugIn
142: .verticesInFence(selectedItems,
143: box, true);
144: if (wasClick()
145: && !verticesInBox.isEmpty()) {
146: verticesDeleted.add(verticesInBox
147: .iterator().next());
148: } else {
149: verticesDeleted
150: .addAll(verticesInBox);
151: }
152: return geometryEditor.deleteVertices(
153: geometryWithSelectedItems,
154: verticesInBox);
155: }
156: }, getPanel(), getPanel().getContext(),
157: getName(), layer, isRollingBackInvalidEdits(),
158: false);
159: return transaction;
160: }
161:
162: public Cursor getCursor() {
163: return createCursor(IconLoader.icon("DeleteCursor.gif")
164: .getImage());
165: }
166:
167: public Icon getIcon() {
168: return IconLoader.icon("DeleteVertex.gif");
169: }
170: }
|