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.ArrayList;
018: import java.util.List;
019:
020: import net.refractions.udig.project.ILayer;
021: import net.refractions.udig.project.command.AbstractCommand;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.tool.edit.internal.Messages;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.support.ClosestEdge;
026: import net.refractions.udig.tools.edit.support.EditBlackboard;
027: import net.refractions.udig.tools.edit.support.EditGeom;
028: import net.refractions.udig.tools.edit.support.Point;
029:
030: import org.eclipse.core.runtime.IProgressMonitor;
031:
032: import com.vividsolutions.jts.geom.MultiPolygon;
033: import com.vividsolutions.jts.geom.Polygon;
034:
035: /**
036: * Add a vertex to the nearest edge to a point.
037: * @author jones
038: * @since 1.1.0
039: */
040: public class AddToNearestEdgeCommand extends AbstractCommand implements
041: UndoableMapCommand {
042:
043: private final EditBlackboard board;
044: private final EditGeom geom;
045: private final Point toAdd;
046: private List<ClosestEdge> edges;
047: private EditToolHandler handler;
048:
049: public AddToNearestEdgeCommand(EditToolHandler handler2,
050: EditBlackboard board, Point toAdd) {
051: this (handler2, board, null, toAdd);
052: }
053:
054: public AddToNearestEdgeCommand(EditToolHandler handler2,
055: EditBlackboard board, EditGeom geom, Point toAdd) {
056: this .board = board;
057: this .geom = geom;
058: this .toAdd = toAdd;
059: this .handler = handler2;
060: }
061:
062: public void rollback(IProgressMonitor monitor) throws Exception {
063: if (edges == null)
064: throw new RuntimeException(
065: "The command has not yet been run!!"); //$NON-NLS-1$
066:
067: if (edges.size() == 0)
068: return;
069:
070: for (ClosestEdge edge : edges) {
071: board.removeCoordinate(edge.getIndexOfPrevious() + 1, edge
072: .getAddedCoord(), edge.getPart());
073: }
074: if (getMap() != null)
075: handler.repaint();
076: }
077:
078: public void run(IProgressMonitor monitor) throws Exception {
079: ILayer editLayer = handler.getEditLayer();
080: Class type = editLayer.getSchema().getDefaultGeometry()
081: .getType();
082: boolean polygonLayer = Polygon.class.isAssignableFrom(type)
083: || MultiPolygon.class.isAssignableFrom(type);
084: if (geom == null) {
085: this .edges = board.addToNearestEdge(toAdd.getX(), toAdd
086: .getY(), polygonLayer);
087: } else {
088: this .edges = new ArrayList<ClosestEdge>();
089: edges.add(board.addToNearestEdge(toAdd.getX(),
090: toAdd.getY(), geom, polygonLayer));
091: }
092: if (getMap() != null)
093: handler.repaint();
094:
095: }
096:
097: public String getName() {
098: return Messages.AddToNearestEdgeCommand_name + toAdd;
099: }
100:
101: }
|