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