01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.behaviour;
16:
17: import java.util.ArrayList;
18: import java.util.List;
19:
20: import net.refractions.udig.project.ILayer;
21: import net.refractions.udig.project.command.UndoableComposite;
22: import net.refractions.udig.project.command.UndoableMapCommand;
23: import net.refractions.udig.tools.edit.Behaviour;
24: import net.refractions.udig.tools.edit.EditPlugin;
25: import net.refractions.udig.tools.edit.EditState;
26: import net.refractions.udig.tools.edit.EditToolHandler;
27: import net.refractions.udig.tools.edit.commands.AddFeaturesCommand;
28: import net.refractions.udig.tools.edit.commands.SplitFeatureCommand;
29: import net.refractions.udig.tools.edit.support.GeometryCreationUtil;
30:
31: import org.eclipse.core.runtime.NullProgressMonitor;
32: import org.geotools.filter.Filter;
33:
34: import com.vividsolutions.jts.geom.Envelope;
35: import com.vividsolutions.jts.geom.LineString;
36:
37: /**
38: * <p>Requirements:
39: * <ul>
40: * <li>Current Shape is not null</li>
41: * <li>At least one different shape on black board</li>
42: * </ul>
43: * </p>
44: * <p>Action:
45: * <ul>
46: * <li>Splits all the geoms on the blackboard (except the current geom) using
47: * the current Shape</li>
48: * </ul>
49: * </p>
50: * @author jones
51: * @since 1.1.0
52: */
53: public class SplitGeometryBehaviour implements Behaviour {
54:
55: public boolean isValid(EditToolHandler handler) {
56: return handler.getCurrentShape() != null;
57: }
58:
59: public UndoableMapCommand getCommand(EditToolHandler handler) {
60: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
61: LineString line = GeometryCreationUtil.createGeom(
62: LineString.class, handler.getCurrentShape());
63: ILayer editLayer = handler.getEditLayer();
64: Envelope bounds = line.getEnvelopeInternal();
65: if (bounds.getWidth() < 0.000001)
66: bounds.init(bounds.getMinX() - 0.5, bounds.getMaxX() + 0.5,
67: bounds.getMinY(), bounds.getMaxY());
68: if (bounds.getHeight() < 0.000001)
69: bounds.init(bounds.getMinX(), bounds.getMaxX(), bounds
70: .getMinY() - 0.5, bounds.getMaxY() + 0.5);
71:
72: Filter filter = editLayer.createBBoxFilter(bounds,
73: new NullProgressMonitor());
74: commands.add(new AddFeaturesCommand(handler
75: .getEditBlackboard(editLayer), editLayer, filter));
76:
77: commands.add(new SplitFeatureCommand(handler, EditState.NONE));
78: return new UndoableComposite(commands);
79: }
80:
81: public void handleError(EditToolHandler handler, Throwable error,
82: UndoableMapCommand command) {
83: EditPlugin.log("", error); //$NON-NLS-1$
84: }
85:
86: }
|