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.ui.AnimationUpdater;
021: import net.refractions.udig.project.ui.IAnimation;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.EditToolHandler;
024: import net.refractions.udig.tools.edit.animation.AddVertexAnimation;
025: import net.refractions.udig.tools.edit.animation.DeleteVertexAnimation;
026: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
027: import net.refractions.udig.tools.edit.support.EditBlackboard;
028: import net.refractions.udig.tools.edit.support.EditUtils;
029: import net.refractions.udig.tools.edit.support.Point;
030: import net.refractions.udig.tools.edit.support.PrimitiveShape;
031:
032: import org.eclipse.core.runtime.IProgressMonitor;
033:
034: import com.vividsolutions.jts.geom.Coordinate;
035:
036: /**
037: * Command for adding a vertext to a {@link net.refractions.udig.tools.edit.support.EditGeom}
038: *
039: * @author jones
040: * @since 1.1.0
041: */
042: public class AddVertexCommand extends AbstractCommand implements
043: UndoableMapCommand {
044:
045: private final Coordinate toAdd;
046: private Point point;
047: private Coordinate addedCoord;
048: private final EditBlackboard board;
049: private IBlockingProvider<PrimitiveShape> shapeProvider;
050: private EditToolHandler handler;
051: private int index;
052: private boolean showAnimation = true;
053:
054: public AddVertexCommand(EditToolHandler handler2,
055: EditBlackboard editBlackboard, Point point) {
056: this (handler2, editBlackboard,
057: new EditUtils.EditToolHandlerShapeProvider(handler2),
058: point, true);
059: }
060:
061: /**
062: * New instance is created. The animation is not shown.
063: *
064: * @param useSnapping TODO
065: */
066: public AddVertexCommand(EditToolHandler handler2,
067: EditBlackboard bb,
068: IBlockingProvider<PrimitiveShape> provider, Point point,
069: boolean useSnapping) {
070: this .handler = handler2;
071: board = bb;
072: shapeProvider = provider;
073: this .point = point;
074: toAdd = performSnapCalculation(this .point, useSnapping);
075: }
076:
077: private Coordinate performSnapCalculation(Point point,
078: boolean useSnapping) {
079: Coordinate toCoord = board.toCoord(point);
080: if (useSnapping) {
081: Coordinate newCoord = EditUtils.instance
082: .getClosestSnapPoint(handler, board, point, false,
083: PreferenceUtil.instance()
084: .getSnapBehaviour(), handler
085: .getCurrentState());
086: if (newCoord != null) {
087: this .point = board.toPoint(newCoord);
088: return newCoord;
089: }
090: return toCoord;
091: }
092:
093: return toCoord;
094: }
095:
096: public void rollback(IProgressMonitor monitor) throws Exception {
097: if (addedCoord == null)
098: return;
099:
100: if (handler.getContext().getMapDisplay() != null
101: && showAnimation) {
102: IAnimation animation = new DeleteVertexAnimation(point);
103: AnimationUpdater.runTimer(handler.getContext()
104: .getMapDisplay(), animation);
105: }
106: board.removeCoordinate(index, addedCoord, shapeProvider
107: .get(monitor));
108: }
109:
110: public void run(IProgressMonitor monitor) throws Exception {
111: PrimitiveShape shape = shapeProvider.get(monitor);
112: boolean collapseVertices = board.isCollapseVertices();
113: try {
114: board.setCollapseVertices(false);
115: board.addCoordinate(toAdd, shape);
116: addedCoord = toAdd;
117: index = shape.getNumPoints() - 1;
118: } finally {
119: board.setCollapseVertices(collapseVertices);
120: }
121: if (handler.getContext().getMapDisplay() != null
122: && showAnimation) {
123: IAnimation animation = new AddVertexAnimation(point.getX(),
124: point.getY());
125: AnimationUpdater.runTimer(handler.getContext()
126: .getMapDisplay(), animation);
127: }
128: }
129:
130: public String getName() {
131: return Messages.AddVertexCommand_name + toAdd;
132: }
133:
134: /**
135: * @return Returns the toAdd.
136: */
137: public Point getPointToAdd() {
138: return this .point;
139: }
140:
141: public boolean isShowAnimation() {
142: return showAnimation;
143: }
144:
145: public void setShowAnimation(boolean showAnimation) {
146: this.showAnimation = showAnimation;
147: }
148:
149: }
|