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.tools.edit.AbstractEditTool;
021: import net.refractions.udig.tools.edit.Activator;
022: import net.refractions.udig.tools.edit.Behaviour;
023: import net.refractions.udig.tools.edit.EditToolConfigurationHelper;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.EnablementBehaviour;
026: import net.refractions.udig.tools.edit.MutualExclusiveBehavior;
027: import net.refractions.udig.tools.edit.activator.DrawEndPointsActivator;
028: import net.refractions.udig.tools.edit.activator.DrawGeomsActivator;
029: import net.refractions.udig.tools.edit.activator.EditStateListenerActivator;
030: import net.refractions.udig.tools.edit.activator.SetRenderingFilter;
031: import net.refractions.udig.tools.edit.behaviour.DefaultCancelBehaviour;
032: import net.refractions.udig.tools.edit.behaviour.DoubleClickRunAcceptBehaviour;
033: import net.refractions.udig.tools.edit.behaviour.FreeHandPolygonDrawBehaviour;
034: import net.refractions.udig.tools.edit.behaviour.SelectGeometryBehaviour;
035: import net.refractions.udig.tools.edit.behaviour.WriteChangesBehaviour;
036: import net.refractions.udig.tools.edit.enablement.ValidToolDetectionActivator;
037: import net.refractions.udig.tools.edit.enablement.WithinLegalLayerBoundsBehaviour;
038: import net.refractions.udig.tools.edit.support.ShapeType;
039:
040: import org.geotools.filter.FilterType;
041:
042: import com.vividsolutions.jts.geom.Geometry;
043: import com.vividsolutions.jts.geom.LineString;
044: import com.vividsolutions.jts.geom.MultiLineString;
045: import com.vividsolutions.jts.geom.MultiPolygon;
046: import com.vividsolutions.jts.geom.Polygon;
047:
048: /**
049: * Create shapes by drawing free hand.
050: *
051: * @author jones
052: * @since 1.1.0
053: */
054: public class FreeHandTool extends AbstractEditTool {
055:
056: public FreeHandTool() {
057: super ();
058: }
059:
060: @Override
061: protected void initEnablementBehaviours(
062: List<EnablementBehaviour> helper) {
063: helper.add(new WithinLegalLayerBoundsBehaviour());
064: helper.add(new ValidToolDetectionActivator(new Class[] {
065: Geometry.class, Polygon.class, MultiPolygon.class,
066: LineString.class, MultiLineString.class }));
067: }
068:
069: @Override
070: protected void initActivators(Set<Activator> activators) {
071: activators.add(new EditStateListenerActivator());
072: DrawGeomsActivator drawGeomsActivator = new DrawGeomsActivator(
073: DrawGeomsActivator.DrawType.POLYGON);
074: drawGeomsActivator.setShowMouseLocation(false);
075: activators.add(drawGeomsActivator);
076: activators.add(new DrawEndPointsActivator());
077: activators.add(new SetRenderingFilter());
078: }
079:
080: @Override
081: protected void initAcceptBehaviours(List<Behaviour> acceptBehaviours) {
082: MutualExclusiveBehavior mutualExclusive = new MutualExclusiveBehavior();
083: acceptBehaviours.add(mutualExclusive);
084:
085: mutualExclusive.getBehaviours().add(
086: new WriteChangesBehaviour(Polygon.class) {
087: @Override
088: public boolean isValid(EditToolHandler handler) {
089:
090: return super .isValid(handler)
091: && handler.getCurrentGeom() != null
092: && handler.getCurrentGeom()
093: .getShapeType() == ShapeType.POLYGON;
094: }
095: });
096:
097: mutualExclusive.getBehaviours().add(
098: new WriteChangesBehaviour(LineString.class) {
099: @Override
100: public boolean isValid(EditToolHandler handler) {
101: return super .isValid(handler)
102: && handler.getCurrentGeom() != null
103: && handler.getCurrentGeom()
104: .getShapeType() == ShapeType.LINE;
105: }
106: });
107:
108: }
109:
110: @Override
111: protected void initCancelBehaviours(List<Behaviour> cancelBehaviours) {
112: cancelBehaviours.add(new DefaultCancelBehaviour());
113: }
114:
115: @Override
116: protected void initEventBehaviours(
117: EditToolConfigurationHelper helper) {
118: helper.add(new SelectGeometryBehaviour(new Class[] {
119: Polygon.class, MultiPolygon.class },
120: FilterType.GEOMETRY_BBOX));
121: helper.add(new FreeHandPolygonDrawBehaviour());
122: DoubleClickRunAcceptBehaviour doubleClickRunAcceptBehaviour = new DoubleClickRunAcceptBehaviour();
123: doubleClickRunAcceptBehaviour.setAddPoint(false);
124: helper.add(doubleClickRunAcceptBehaviour);
125: helper.done();
126: }
127:
128: @Override
129: protected String getExtensionID() {
130: return "net.refractions.udig.tools.freeHandTool"; //$NON-NLS-1$
131: }
132:
133: }
|