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.List;
018:
019: import net.refractions.udig.project.command.AbstractCommand;
020: import net.refractions.udig.project.command.UndoableMapCommand;
021: import net.refractions.udig.project.internal.Layer;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.EditState;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.support.EditBlackboard;
026: import net.refractions.udig.tools.edit.support.EditGeom;
027: import net.refractions.udig.tools.edit.support.EditUtils;
028: import net.refractions.udig.tools.edit.support.PrimitiveShape;
029:
030: import org.eclipse.core.runtime.IProgressMonitor;
031: import org.geotools.filter.Filter;
032:
033: /**
034: * Clears the edit Blackboards and the current edit shape and state.
035: *
036: * @author Jesse
037: * @since 1.1.0
038: */
039: public class DefaultCancelEditingCommand extends AbstractCommand
040: implements UndoableMapCommand {
041:
042: private EditToolHandler handler;
043: private PrimitiveShape currentShape;
044: private EditState currentState;
045: private List<EditGeom> geoms;
046: private Filter oldFilter;
047:
048: public DefaultCancelEditingCommand(EditToolHandler handler) {
049: this .handler = handler;
050: }
051:
052: public void run(IProgressMonitor monitor) throws Exception {
053: Layer editLayer = (Layer) handler.getEditLayer();
054: if (currentShape != null) {
055: this .currentShape = handler.getCurrentShape();
056: this .currentState = handler.getCurrentState();
057: this .geoms = handler.getEditBlackboard(editLayer)
058: .getGeoms();
059: }
060: handler.setCurrentShape(null);
061: handler.setCurrentState(EditState.NONE);
062:
063: oldFilter = editLayer.getFilter();
064: editLayer.setFilter(Filter.ALL);
065:
066: EditUtils.instance.cancelHideSelection(editLayer);
067:
068: EditBlackboard editBlackboard = handler
069: .getEditBlackboard(editLayer);
070: editBlackboard.startBatchingEvents();
071: editBlackboard.clear();
072: editBlackboard.fireBatchedEvents();
073:
074: handler.repaint();
075: }
076:
077: public String getName() {
078: return Messages.DefaultCancelEditingCommand_name;
079: }
080:
081: public void rollback(IProgressMonitor monitor) throws Exception {
082: Layer editLayer = (Layer) handler.getEditLayer();
083: editLayer.setFilter(oldFilter);
084:
085: EditBlackboard editBlackboard = handler
086: .getEditBlackboard(editLayer);
087: editBlackboard.startBatchingEvents();
088: for (EditGeom geom : geoms) {
089: copyFeature(editBlackboard, geom);
090: }
091: handler.setCurrentState(this .currentState);
092: editBlackboard.fireBatchedEvents();
093: handler.repaint();
094: }
095:
096: /**
097: * Copies the geometry back onto the editblackboard.
098: *
099: * @return
100: */
101: private void copyFeature(EditBlackboard editBlackboard,
102: EditGeom geom) {
103: EditGeom newGeom = editBlackboard.newGeom(geom
104: .getFeatureIDRef().get(), geom.getShapeType());
105: for (PrimitiveShape shape : geom) {
106: PrimitiveShape newShape;
107: if (shape == geom.getShell()) {
108: newShape = newGeom.getShell();
109: } else {
110: newShape = newGeom.newHole();
111: }
112: if (shape == currentShape)
113: handler.setCurrentShape(newShape);
114: for (int i = 0; i < shape.getNumCoords(); i++) {
115: editBlackboard.addCoordinate(shape.getCoord(i),
116: newShape);
117: }
118: newGeom.setChanged(geom.isChanged());
119: }
120: }
121:
122: }
|