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 net.refractions.udig.project.command.AbstractCommand;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.tools.edit.EditToolHandler;
020: import net.refractions.udig.tools.edit.support.EditBlackboard;
021: import net.refractions.udig.tools.edit.support.EditGeom;
022: import net.refractions.udig.tools.edit.support.Point;
023: import net.refractions.udig.tools.edit.support.PrimitiveShape;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026:
027: /**
028: * Removes all vertices from EditGeom's shell.
029: *
030: * @author Jesse
031: * @since 1.1.0
032: */
033: public class DeleteEditGeomCommand extends AbstractCommand implements
034: UndoableMapCommand {
035:
036: private EditToolHandler handler;
037: private EditGeom oldGeom;
038: /**
039: * if -1 then the old shape is the shell of the oldGeom otherwise it is the index of the hole.
040: */
041: private int oldCurrentShape = -5;
042:
043: public DeleteEditGeomCommand(EditToolHandler handler) {
044: this .handler = handler;
045: }
046:
047: public void run(IProgressMonitor monitor) throws Exception {
048: EditGeom geom = handler.getCurrentGeom();
049: PrimitiveShape shape = handler.getCurrentShape();
050: if (shape == geom.getShell())
051: oldCurrentShape = -1;
052: else {
053: for (int i = 0; i < geom.getHoles().size(); i++) {
054: if (shape == geom.getHoles().get(i)) {
055:
056: }
057: }
058: }
059:
060: if (oldCurrentShape == -5)
061: throw new RuntimeException(
062: "For some reason the current shape is not part of the current geometry"); //$NON-NLS-1$
063:
064: oldGeom = new EditGeom(geom);
065: for (Point point : geom.getShell()) {
066: geom.getEditBlackboard().removeCoords(point.getX(),
067: point.getY(), geom.getShell());
068: }
069: handler.setCurrentShape(null);
070: }
071:
072: public String getName() {
073: return "DeleteEditGeomCommand"; //$NON-NLS-1$
074: }
075:
076: public void rollback(IProgressMonitor monitor) throws Exception {
077: EditBlackboard bb = oldGeom.getEditBlackboard();
078: EditGeom geom = bb.newGeom(oldGeom.getFeatureIDRef().get(),
079: oldGeom.getShapeType());
080: for (Point p : oldGeom.getShell()) {
081: bb.addPoint(p.getX(), p.getY(), geom.getShell());
082: }
083:
084: for (PrimitiveShape shape : oldGeom.getHoles()) {
085: PrimitiveShape hole = geom.newHole();
086: for (Point p : shape) {
087: bb.addPoint(p.getX(), p.getY(), hole);
088: }
089: }
090:
091: PrimitiveShape currentShape;
092: if (oldCurrentShape == -1)
093: currentShape = geom.getShell();
094: else {
095: currentShape = geom.getHoles().get(oldCurrentShape);
096: }
097: handler.setCurrentShape(currentShape);
098: }
099:
100: }
|