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.core.IBlockingProvider;
018: import net.refractions.udig.project.command.AbstractCommand;
019: import net.refractions.udig.project.command.UndoableMapCommand;
020: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
021: import net.refractions.udig.project.ui.AnimationUpdater;
022: import net.refractions.udig.project.ui.IAnimation;
023: import net.refractions.udig.tool.edit.internal.Messages;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.animation.AddVertexAnimation;
026: import net.refractions.udig.tools.edit.animation.DeleteVertexAnimation;
027: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
028: import net.refractions.udig.tools.edit.support.EditBlackboard;
029: import net.refractions.udig.tools.edit.support.EditUtils;
030: import net.refractions.udig.tools.edit.support.Point;
031: import net.refractions.udig.tools.edit.support.PrimitiveShape;
032: import net.refractions.udig.tools.edit.support.Selection;
033:
034: import org.eclipse.core.runtime.IProgressMonitor;
035: import org.eclipse.core.runtime.SubProgressMonitor;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038:
039: /**
040: * Command for adding a vertext to a {@link net.refractions.udig.tools.edit.support.EditGeom}
041: *
042: * @author jones
043: * @since 1.1.0
044: */
045: public class InsertVertexCommand extends AbstractCommand implements
046: UndoableMapCommand {
047:
048: private final Coordinate toAdd;
049: private final IBlockingProvider<PrimitiveShape> shape;
050: private final EditBlackboard board;
051: private int index;
052: private final IMapDisplay mapDisplay;
053: private EditToolHandler handler;
054: private Selection oldSelection;
055: private Point point;
056:
057: public InsertVertexCommand(EditToolHandler handler,
058: EditBlackboard board, IMapDisplay display,
059: IBlockingProvider<PrimitiveShape> shape, Point toAdd,
060: int index, boolean useSnapping) {
061: this .board = board;
062: this .shape = shape;
063: this .index = index;
064: this .mapDisplay = display;
065: this .handler = handler;
066: this .point = toAdd;
067: this .toAdd = performSnapCalculation(toAdd, useSnapping);
068: }
069:
070: private Coordinate performSnapCalculation(Point point,
071: boolean useSnapping) {
072: Coordinate toCoord = board.toCoord(point);
073: if (useSnapping) {
074: Coordinate newCoord = EditUtils.instance
075: .getClosestSnapPoint(handler, board, point, false,
076: PreferenceUtil.instance()
077: .getSnapBehaviour(), handler
078: .getCurrentState());
079: if (newCoord != null) {
080: this .point = board.toPoint(newCoord);
081: return newCoord;
082: }
083: return toCoord;
084: }
085:
086: return toCoord;
087: }
088:
089: public void rollback(IProgressMonitor monitor) throws Exception {
090: board.startBatchingEvents();
091:
092: if (handler.getContext().getMapDisplay() != null) {
093: IAnimation animation = new DeleteVertexAnimation(point);
094: AnimationUpdater.runTimer(handler.getContext()
095: .getMapDisplay(), animation);
096: }
097:
098: board.removeCoordinate(index, toAdd, shape
099: .get(new SubProgressMonitor(monitor, 1)));
100: board.selectionClear();
101: board.selectionAddAll(oldSelection);
102: board.fireBatchedEvents();
103:
104: if (getMap() != null)
105: handler.repaint();
106:
107: }
108:
109: public void run(IProgressMonitor monitor) throws Exception {
110: board.startBatchingEvents();
111: PrimitiveShape primitiveShape = shape
112: .get(new SubProgressMonitor(monitor, 1));
113: board.insertCoordinate(toAdd, index, primitiveShape);
114: oldSelection = new Selection(board.getSelection());
115: oldSelection.disconnect();
116: board.selectionClear();
117: board.selectionAdd(point);
118: board.fireBatchedEvents();
119: if (getMap() != null)
120: handler.repaint();
121:
122: if (mapDisplay != null) {
123: IAnimation animation = new AddVertexAnimation(point.getX(),
124: point.getY());
125: AnimationUpdater.runTimer(mapDisplay, animation);
126: handler.repaint();
127: }
128: }
129:
130: public String getName() {
131: return Messages.InsertVertexCommand_name1 + toAdd
132: + Messages.InsertVertexCommand_name2 + index;
133: }
134:
135: }
|