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.Collection;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import net.refractions.udig.project.ILayer;
022: import net.refractions.udig.project.command.AbstractCommand;
023: import net.refractions.udig.project.command.Command;
024: import net.refractions.udig.project.command.UndoableMapCommand;
025: import net.refractions.udig.project.ui.tool.IToolContext;
026: import net.refractions.udig.tool.edit.internal.Messages;
027: import net.refractions.udig.tools.edit.EditState;
028: import net.refractions.udig.tools.edit.EditToolHandler;
029: import net.refractions.udig.tools.edit.support.EditBlackboard;
030: import net.refractions.udig.tools.edit.support.EditGeom;
031: import net.refractions.udig.tools.edit.support.EditUtils;
032: import net.refractions.udig.tools.edit.support.Point;
033: import net.refractions.udig.tools.edit.support.PrimitiveShape;
034:
035: import org.eclipse.core.runtime.IProgressMonitor;
036: import org.eclipse.core.runtime.SubProgressMonitor;
037: import org.geotools.feature.Feature;
038:
039: import com.vividsolutions.jts.geom.Coordinate;
040: import com.vividsolutions.jts.geom.Envelope;
041: import com.vividsolutions.jts.geom.MultiPolygon;
042: import com.vividsolutions.jts.geom.Polygon;
043:
044: /**
045: * Sets the selected feature to be the edit feature.
046: * <ul>
047: * <li>Sets the editblack board so it contains the default geometry of the feature.</li>
048: * <li>Sets the edit managers editFeature to be the feature.</li>
049: * <li>Sets the EditToolHandler's currentGeom to be the newly added geom.</li>
050: * @author jones
051: * @since 1.1.0
052: */
053: public class SetGeomCommand extends AbstractCommand implements Command,
054: UndoableMapCommand {
055:
056: private ILayer selectedLayer;
057: private Feature feature;
058: private EditToolHandler handler;
059: private UndoableMapCommand unselectcommand;
060: private UndoableMapCommand command;
061: private List<EditGeom> removed;
062: private EditGeom currentGeom;
063: private PrimitiveShape currentShape;
064: private EditState currentState;
065: private Point mouse;
066: private Envelope refreshBounds;
067:
068: public SetGeomCommand(EditToolHandler handler, Feature feature,
069: ILayer selectedLayer, Point mouse) {
070: if (mouse == null)
071: throw new NullPointerException("mouse is null"); //$NON-NLS-1$
072: this .handler = handler;
073: this .feature = feature;
074: this .selectedLayer = selectedLayer;
075: this .mouse = mouse;
076: }
077:
078: public void run(IProgressMonitor monitor) throws Exception {
079: monitor.beginTask(Messages.SetGeomCommand_undoTask, 30);
080: EditUtils.instance.cancelHideSelection(selectedLayer);
081: IToolContext context = handler.getContext();
082:
083: this .refreshBounds = feature.getBounds();
084: EditUtils.instance.refreshLayer(selectedLayer, feature,
085: refreshBounds, false, true);
086:
087: unselectcommand = context.getSelectionFactory()
088: .createNoSelectCommand();
089: command = context.getEditFactory().createSetEditFeatureCommand(
090: feature, selectedLayer);
091:
092: unselectcommand.setMap(getMap());
093: command.setMap(getMap());
094:
095: unselectcommand.run(new SubProgressMonitor(monitor, 10));
096: command.run(new SubProgressMonitor(monitor, 10));
097:
098: this .removed = handler.getEditBlackboard(selectedLayer)
099: .getGeoms();
100: this .currentGeom = handler.getCurrentGeom();
101: this .currentShape = handler.getCurrentShape();
102: this .currentState = handler.getCurrentState();
103:
104: handler.setCurrentState(EditState.MODIFYING);
105:
106: Collection<EditGeom> geoms = handler.getEditBlackboard(
107: selectedLayer).setGeometries(
108: feature.getDefaultGeometry(), feature.getID()).values();
109: Class type = selectedLayer.getSchema().getDefaultGeometry()
110: .getType();
111: boolean polygonLayer = Polygon.class.isAssignableFrom(type)
112: || MultiPolygon.class.isAssignableFrom(type);
113: EditGeom over = EditUtils.instance.getGeomWithMouseOver(geoms,
114: mouse, polygonLayer);
115: handler.setCurrentShape(over.getShell());
116: monitor.done();
117: }
118:
119: public String getName() {
120: return Messages.SetGeomCommand_name;
121: }
122:
123: public void rollback(IProgressMonitor monitor) throws Exception {
124: monitor.beginTask(Messages.SetGeomCommand_runTask, 30);
125: handler.setCurrentShape(currentShape);
126: EditBlackboard bb = handler.getEditBlackboard(selectedLayer);
127: handler.setCurrentState(currentState);
128: EditGeom newCurrentGeom = null;
129: List<EditGeom> empty = bb.getGeoms();
130:
131: for (EditGeom original : removed) {
132: EditGeom inBlackboard = bb.newGeom(original
133: .getFeatureIDRef().get(), original.getShapeType());
134: inBlackboard.setChanged(original.isChanged());
135: if (original == currentGeom)
136: newCurrentGeom = inBlackboard;
137:
138: PrimitiveShape destination = inBlackboard.getShell();
139: newCurrentGeom = setCurrentGeom(newCurrentGeom,
140: destination, original.getShell());
141:
142: for (Iterator<Coordinate> iter = original.getShell()
143: .coordIterator(); iter.hasNext();) {
144: bb.addCoordinate(iter.next(), destination);
145: }
146:
147: for (PrimitiveShape shape : original.getHoles()) {
148: destination = inBlackboard.newHole();
149: newCurrentGeom = setCurrentGeom(newCurrentGeom,
150: destination, shape);
151: for (Iterator<Coordinate> iter = shape.coordIterator(); iter
152: .hasNext();) {
153: bb.addCoordinate(iter.next(), destination);
154: }
155: }
156: }
157:
158: bb.removeGeometries(empty);
159: monitor.worked(10);
160: refreshBounds.expandToInclude(feature.getBounds());
161: EditUtils.instance.refreshLayer(selectedLayer, feature,
162: refreshBounds, true, false);
163:
164: command.rollback(new SubProgressMonitor(monitor, 10));
165: unselectcommand.rollback(new SubProgressMonitor(monitor, 10));
166: }
167:
168: private EditGeom setCurrentGeom(EditGeom newCurrentGeom,
169: PrimitiveShape destination, PrimitiveShape shape) {
170: if (currentGeom != null && newCurrentGeom != null
171: && shape == currentShape) {
172: handler.setCurrentShape(destination);
173: }
174: return newCurrentGeom;
175: }
176:
177: }
|