01: package net.refractions.udig.tools.edit.commands;
02:
03: import java.util.Collection;
04:
05: import net.refractions.udig.project.command.AbstractCommand;
06: import net.refractions.udig.project.command.UndoableMapCommand;
07: import net.refractions.udig.project.internal.Layer;
08: import net.refractions.udig.tool.edit.internal.Messages;
09: import net.refractions.udig.tools.edit.EditToolHandler;
10: import net.refractions.udig.tools.edit.support.EditBlackboard;
11: import net.refractions.udig.tools.edit.support.EditGeom;
12: import net.refractions.udig.tools.edit.support.EditUtils;
13: import net.refractions.udig.tools.edit.support.Point;
14:
15: import org.eclipse.core.runtime.IProgressMonitor;
16: import org.geotools.feature.Feature;
17:
18: import com.vividsolutions.jts.geom.Envelope;
19: import com.vividsolutions.jts.geom.MultiPolygon;
20: import com.vividsolutions.jts.geom.Polygon;
21:
22: /**
23: * Adds a geometry to the edit blackboard.
24: *
25: * @author jones
26: * @since 1.1.0
27: */
28: public class AddGeomCommand extends AbstractCommand implements
29: UndoableMapCommand {
30:
31: private Collection<EditGeom> added;
32: private Feature feature;
33: private EditBlackboard blackboard;
34: private Layer editLayer;
35: private Envelope refreshBounds;
36: private EditToolHandler handler;
37: private Point mouseLocation;
38:
39: /**
40: * new instance
41: * @param handler used to obtain the blackboard for the currently selected layer.
42: * @param feature the feature to add.
43: * @param mouseLocation If not null it will set the current shape to be the Geom that intersects the mouseLocation
44: */
45: public AddGeomCommand(EditToolHandler handler, Feature feature,
46: Point mouseLocation) {
47: this .feature = feature;
48: editLayer = (Layer) handler.getEditLayer();
49: this .blackboard = handler.getEditBlackboard(editLayer);
50: this .handler = handler;
51: this .mouseLocation = mouseLocation;
52: }
53:
54: /**
55: * New Instance
56: * @param blackboard blackboard to add features to.
57: * @param feature2 the feature to add
58: */
59: public AddGeomCommand(EditBlackboard blackboard2, Feature feature2) {
60: this .feature = feature2;
61: this .blackboard = blackboard2;
62: }
63:
64: public void run(IProgressMonitor monitor) throws Exception {
65: if (editLayer == null)
66: editLayer = (Layer) getMap().getEditManager()
67: .getSelectedLayer();
68: this .added = blackboard.addGeometry(
69: feature.getDefaultGeometry(), feature.getID()).values();
70: if (!added.isEmpty() && mouseLocation != null) {
71: Class type = editLayer.getSchema().getDefaultGeometry()
72: .getType();
73: boolean polygonLayer = Polygon.class.isAssignableFrom(type)
74: || MultiPolygon.class.isAssignableFrom(type);
75: EditGeom geom = EditUtils.instance.getGeomWithMouseOver(
76: added, mouseLocation, polygonLayer);
77: handler.setCurrentShape(geom.getShell());
78: }
79:
80: //make sure the display refreshes
81: this .refreshBounds = feature.getBounds();
82: EditUtils.instance.refreshLayer(editLayer, feature,
83: refreshBounds, false, true);
84: }
85:
86: public String getName() {
87: return Messages.AddGeomCommand_name;
88: }
89:
90: public void rollback(IProgressMonitor monitor) throws Exception {
91: blackboard.removeGeometries(added);
92:
93: refreshBounds.expandToInclude(feature.getBounds());
94: EditUtils.instance.refreshLayer(editLayer, feature,
95: refreshBounds, true, false);
96: }
97:
98: }
|