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.impl;
016:
017: import java.util.List;
018: import java.util.Set;
019:
020: import net.refractions.udig.core.IProvider;
021: import net.refractions.udig.core.StaticProvider;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.AbstractEditTool;
024: import net.refractions.udig.tools.edit.Activator;
025: import net.refractions.udig.tools.edit.Behaviour;
026: import net.refractions.udig.tools.edit.EditToolConfigurationHelper;
027: import net.refractions.udig.tools.edit.EditToolHandler;
028: import net.refractions.udig.tools.edit.EnablementBehaviour;
029: import net.refractions.udig.tools.edit.MutualExclusiveBehavior;
030: import net.refractions.udig.tools.edit.activator.DrawCurrentGeomVerticesActivator;
031: import net.refractions.udig.tools.edit.activator.DrawGeomsActivator;
032: import net.refractions.udig.tools.edit.activator.EditStateListenerActivator;
033: import net.refractions.udig.tools.edit.activator.SetRenderingFilter;
034: import net.refractions.udig.tools.edit.behaviour.CursorControlBehaviour;
035: import net.refractions.udig.tools.edit.behaviour.DefaultCancelBehaviour;
036: import net.refractions.udig.tools.edit.behaviour.DoubleClickRunAcceptBehaviour;
037: import net.refractions.udig.tools.edit.behaviour.RemoveVertexBehaviour;
038: import net.refractions.udig.tools.edit.behaviour.SelectGeometryBehaviour;
039: import net.refractions.udig.tools.edit.behaviour.WriteChangesBehaviour;
040: import net.refractions.udig.tools.edit.enablement.ValidToolDetectionActivator;
041:
042: import org.eclipse.swt.SWT;
043: import org.eclipse.swt.graphics.Cursor;
044: import org.geotools.feature.Feature;
045: import org.geotools.filter.FilterType;
046:
047: import com.vividsolutions.jts.geom.Geometry;
048: import com.vividsolutions.jts.geom.LineString;
049: import com.vividsolutions.jts.geom.MultiLineString;
050: import com.vividsolutions.jts.geom.MultiPoint;
051: import com.vividsolutions.jts.geom.MultiPolygon;
052: import com.vividsolutions.jts.geom.Point;
053: import com.vividsolutions.jts.geom.Polygon;
054:
055: /**
056: * A Tool that removes vertices from EditGeoms and selects features.
057: * @author jones
058: * @since 1.1.0
059: */
060: public class RemoveVertexTool extends AbstractEditTool {
061:
062: @Override
063: protected void initActivators(Set<Activator> activators) {
064: activators.add(new EditStateListenerActivator());
065: activators.add(new DrawGeomsActivator(
066: DrawGeomsActivator.DrawType.POLYGON));
067: activators.add(new DrawCurrentGeomVerticesActivator());
068: activators.add(new SetRenderingFilter());
069: }
070:
071: @Override
072: protected void initAcceptBehaviours(List<Behaviour> acceptBehaviours) {
073: MutualExclusiveBehavior mutualExclusive = new MutualExclusiveBehavior();
074: acceptBehaviours.add(mutualExclusive);
075: mutualExclusive.getBehaviours().add(
076: new WriteChangesBehaviour(Polygon.class) {
077: @Override
078: public boolean isValid(EditToolHandler handler) {
079: Feature feature = handler.getContext()
080: .getEditManager().getEditFeature();
081: if (feature == null)
082: return false;
083: Class<? extends Geometry> class1 = feature
084: .getDefaultGeometry().getClass();
085: return super .isValid(handler)
086: && feature != null
087: && (class1 == Polygon.class || class1 == MultiPolygon.class);
088: }
089: });
090: mutualExclusive.getBehaviours().add(
091: new WriteChangesBehaviour(LineString.class) {
092: @Override
093: public boolean isValid(EditToolHandler handler) {
094: Feature feature = handler.getContext()
095: .getEditManager().getEditFeature();
096: if (feature == null)
097: return false;
098: Class<? extends Geometry> class1 = feature
099: .getDefaultGeometry().getClass();
100: return super .isValid(handler)
101: && feature != null
102: && (class1 == LineString.class || class1 == MultiLineString.class);
103: }
104: });
105: mutualExclusive.getBehaviours().add(
106: new WriteChangesBehaviour(Point.class) {
107: @Override
108: public boolean isValid(EditToolHandler handler) {
109: Feature feature = handler.getContext()
110: .getEditManager().getEditFeature();
111: if (feature == null)
112: return false;
113: Class<? extends Geometry> class1 = feature
114: .getDefaultGeometry().getClass();
115: return super .isValid(handler)
116: && feature != null
117: && (class1 == Point.class || class1 == MultiPoint.class);
118: }
119: });
120: }
121:
122: @Override
123: protected void initCancelBehaviours(List<Behaviour> cancelBehaviours) {
124: cancelBehaviours.add(new DefaultCancelBehaviour());
125: }
126:
127: @Override
128: protected void initEventBehaviours(
129: EditToolConfigurationHelper helper) {
130: helper.add(new CursorControlBehaviour(handler,
131: new StaticProvider<String>(
132: Messages.RemoveVertexTool_select_feature),
133: new CursorControlBehaviour.SystemCursorProvider(
134: SWT.CURSOR_CROSS), new StaticProvider<String>(
135: Messages.RemoveVertexTool_remove_vertex),
136: new IProvider<Cursor>() {
137: public Cursor get() {
138: return null;
139: }
140:
141: }, null));
142: // vertex selection OR geometry selection should not both happen so make them a mutual exclusion behaviour
143: helper.startMutualExclusiveList();
144: helper.add(new SelectGeometryBehaviour(
145: new Class[] { Geometry.class },
146: FilterType.GEOMETRY_INTERSECTS));
147: helper.add(new RemoveVertexBehaviour());
148: helper.stopMutualExclusiveList();
149:
150: helper.add(new DoubleClickRunAcceptBehaviour());
151: helper.done();
152: }
153:
154: @Override
155: protected String getExtensionID() {
156: return "net.refractions.udig.tools.addVertexTool"; //$NON-NLS-1$
157: }
158:
159: @Override
160: protected void initEnablementBehaviours(
161: List<EnablementBehaviour> helper) {
162: helper.add(new ValidToolDetectionActivator(new Class[] {
163: Geometry.class, LineString.class,
164: MultiLineString.class, Polygon.class,
165: MultiPolygon.class, Point.class, MultiPoint.class }));
166: }
167:
168: }
|