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.awt.Rectangle;
018: import java.awt.geom.AffineTransform;
019: import java.awt.geom.GeneralPath;
020: import java.util.List;
021: import java.util.Set;
022:
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.activator.EditStateListenerActivator;
030: import net.refractions.udig.tools.edit.activator.ResetHandlerActivator;
031: import net.refractions.udig.tools.edit.activator.SetRenderingFilter;
032: import net.refractions.udig.tools.edit.behaviour.DefaultCancelBehaviour;
033: import net.refractions.udig.tools.edit.behaviour.ShapeCreationBehaviour;
034: import net.refractions.udig.tools.edit.behaviour.WriteChangesBehaviour;
035: import net.refractions.udig.tools.edit.behaviour.ShapeCreationBehaviour.ShapeFactory;
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 com.vividsolutions.jts.geom.Geometry;
041: import com.vividsolutions.jts.geom.LineString;
042: import com.vividsolutions.jts.geom.LinearRing;
043: import com.vividsolutions.jts.geom.MultiLineString;
044: import com.vividsolutions.jts.geom.MultiPolygon;
045: import com.vividsolutions.jts.geom.Polygon;
046:
047: /**
048: * Tool for drawing and resizing rectangles.
049: *
050: * @author jones
051: * @since 1.1.0
052: */
053: public class RectangleTool extends AbstractEditTool {
054: @Override
055: protected void initEnablementBehaviours(
056: List<EnablementBehaviour> helper) {
057: helper.add(new WithinLegalLayerBoundsBehaviour());
058: helper.add(new ValidToolDetectionActivator(new Class[] {
059: Geometry.class, MultiLineString.class,
060: LineString.class, LinearRing.class, Polygon.class,
061: MultiPolygon.class }));
062: }
063:
064: @Override
065: protected void initActivators(Set<Activator> activators) {
066: activators.add(new EditStateListenerActivator());
067: activators.add(new ResetHandlerActivator());
068: activators.add(new SetRenderingFilter());
069: }
070:
071: @Override
072: protected void initAcceptBehaviours(List<Behaviour> acceptBehaviours) {
073: acceptBehaviours.add(new WriteChangesBehaviour(Polygon.class) {
074: @Override
075: public boolean isValid(EditToolHandler handler) {
076: return super .isValid(handler)
077: && handler.getCurrentGeom().getShapeType() == ShapeType.POLYGON;
078: }
079: });
080: acceptBehaviours
081: .add(new WriteChangesBehaviour(LinearRing.class) {
082: @Override
083: public boolean isValid(EditToolHandler handler) {
084: return super .isValid(handler)
085: && handler.getCurrentGeom()
086: .getShapeType() == ShapeType.LINE;
087: }
088: });
089:
090: }
091:
092: @Override
093: protected void initCancelBehaviours(List<Behaviour> cancelBehaviours) {
094: cancelBehaviours.add(new DefaultCancelBehaviour());
095: }
096:
097: @Override
098: protected void initEventBehaviours(
099: EditToolConfigurationHelper helper) {
100:
101: helper.add(new ShapeCreationBehaviour(getShapeFactory()));
102: helper.done();
103: }
104:
105: protected ShapeFactory getShapeFactory() {
106: return new ShapeCreationBehaviour.ShapeFactory() {
107:
108: @Override
109: public GeneralPath create(int width, int height) {
110: GeneralPath path = new GeneralPath();
111: path.append(new Rectangle(width, height)
112: .getPathIterator(new AffineTransform()), false);
113: return path;
114: }
115:
116: };
117: }
118:
119: @Override
120: protected String getExtensionID() {
121: return "net.refractions.udig.tool.edit.createRectangle"; //$NON-NLS-1$
122: }
123:
124: }
|