001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.commands;
016:
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Set;
021:
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.command.AbstractCommand;
024: import net.refractions.udig.project.command.UndoableMapCommand;
025: import net.refractions.udig.tool.edit.internal.Messages;
026: import net.refractions.udig.tools.edit.EditState;
027: import net.refractions.udig.tools.edit.EditToolHandler;
028: import net.refractions.udig.tools.edit.support.EditBlackboard;
029: import net.refractions.udig.tools.edit.support.EditGeom;
030: import net.refractions.udig.tools.edit.support.EditUtils;
031: import net.refractions.udig.tools.edit.support.PrimitiveShape;
032:
033: import org.eclipse.core.runtime.IProgressMonitor;
034: import org.eclipse.core.runtime.SubProgressMonitor;
035:
036: import com.vividsolutions.jts.geom.Coordinate;
037: import com.vividsolutions.jts.geom.Envelope;
038:
039: /**
040: * Removes an EditGeom from the edit blackboard and updates the handler's current geom
041: * if it has been removed.
042: *
043: * @author jones
044: * @since 1.1.0
045: */
046: public class RemoveEditGeomCommand extends AbstractCommand implements
047: UndoableMapCommand {
048:
049: private EditToolHandler handler;
050: private List<EditGeom> geoms;
051: private List<EditGeom> removed;
052: private EditGeom currentGeom;
053: private PrimitiveShape currentShape;
054: private EditState currentState;
055: private boolean removedAll;
056: private UndoableMapCommand nullEditFeatureCommand;
057: private ILayer layer;
058:
059: public RemoveEditGeomCommand(EditToolHandler handler,
060: List<EditGeom> geoms) {
061: this .handler = handler;
062: this .geoms = geoms;
063: this .layer = handler.getEditLayer();
064: }
065:
066: public void rollback(IProgressMonitor monitor) throws Exception {
067: monitor.beginTask(
068: Messages.RemoveEditGeomCommand_rollbackTaskMessage, 20);
069: if (currentState != null)
070: handler.setCurrentState(currentState);
071: if (nullEditFeatureCommand != null)
072: nullEditFeatureCommand.rollback(new SubProgressMonitor(
073: monitor, 10));
074: EditBlackboard bb = handler.getEditBlackboard(layer);
075: EditGeom newCurrentGeom = null;
076: List<EditGeom> empty = bb.getGeoms();
077: for (EditGeom original : removed) {
078: EditGeom inBlackboard = bb.newGeom(original
079: .getFeatureIDRef().get(), original.getShapeType());
080: inBlackboard.setChanged(original.isChanged());
081: if (original == currentGeom)
082: newCurrentGeom = inBlackboard;
083:
084: PrimitiveShape destination = inBlackboard.getShell();
085: newCurrentGeom = setCurrentGeom(newCurrentGeom,
086: destination, original.getShell());
087:
088: for (Iterator<Coordinate> iter = original.getShell()
089: .coordIterator(); iter.hasNext();) {
090: bb.addCoordinate(iter.next(), destination);
091: }
092:
093: for (PrimitiveShape shape : original.getHoles()) {
094: destination = inBlackboard.newHole();
095: newCurrentGeom = setCurrentGeom(newCurrentGeom,
096: destination, shape);
097: for (Iterator<Coordinate> iter = shape.coordIterator(); iter
098: .hasNext();) {
099: bb.addCoordinate(iter.next(), destination);
100: }
101: }
102: }
103: if (removedAll)
104: bb.removeGeometries(empty);
105: }
106:
107: private EditGeom setCurrentGeom(EditGeom newCurrentGeom,
108: PrimitiveShape destination, PrimitiveShape shape) {
109: if (currentGeom != null && newCurrentGeom != null
110: && shape == currentShape) {
111: handler.setCurrentShape(destination);
112: return null;
113: }
114: return newCurrentGeom;
115: }
116:
117: public void run(IProgressMonitor monitor) throws Exception {
118: monitor.beginTask(
119: Messages.RemoveEditGeomCommand_runTaskMessage, 20);
120: if (geoms.containsAll(handler.getEditBlackboard(layer)
121: .getGeoms())) {
122: removedAll = true;
123: this .removed = handler.getEditBlackboard(layer).getGeoms();
124: handler.getEditBlackboard(layer).clear();
125: } else {
126: this .removed = handler.getEditBlackboard(layer)
127: .removeGeometries(geoms);
128: }
129: if (removed.contains(handler.getCurrentGeom())) {
130: this .currentGeom = handler.getCurrentGeom();
131: this .currentShape = handler.getCurrentShape();
132: this .currentState = handler.getCurrentState();
133: handler.setCurrentShape(null);
134: handler.setCurrentState(EditState.NONE);
135: nullEditFeatureCommand = handler.getContext()
136: .getEditFactory().createSetEditFeatureCommand(null,
137: layer);
138: nullEditFeatureCommand.setMap(getMap());
139: nullEditFeatureCommand.run(new SubProgressMonitor(monitor,
140: 10));
141: }
142: Envelope env = new Envelope();
143:
144: Set<String> fids = new HashSet<String>();
145: for (EditGeom geom : removed) {
146: if (env.isNull()) {
147: env.init(geom.getShell().getEnvelope());
148: } else {
149: env.expandToInclude(geom.getShell().getEnvelope());
150: }
151: fids.add(geom.getFeatureIDRef().get());
152: }
153: EditUtils.instance.refreshLayer(layer, fids, env, true, false);
154: monitor.done();
155: }
156:
157: public String getName() {
158: return Messages.RemoveEditGeomCommand_commandName;
159: }
160:
161: }
|