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.behaviour;
016:
017: import java.awt.Color;
018: import java.awt.Rectangle;
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022:
023: import net.refractions.udig.project.command.UndoableMapCommand;
024: import net.refractions.udig.project.ui.internal.commands.draw.DrawShapeCommand;
025: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
026: import net.refractions.udig.tools.edit.EditPlugin;
027: import net.refractions.udig.tools.edit.EditState;
028: import net.refractions.udig.tools.edit.EditToolHandler;
029: import net.refractions.udig.tools.edit.EventBehaviour;
030: import net.refractions.udig.tools.edit.EventType;
031: import net.refractions.udig.tools.edit.LockingBehaviour;
032: import net.refractions.udig.tools.edit.commands.SelectPointCommand;
033: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
034: import net.refractions.udig.tools.edit.support.EditGeom;
035: import net.refractions.udig.tools.edit.support.Point;
036: import net.refractions.udig.tools.edit.support.PrimitiveShape;
037: import net.refractions.udig.ui.graphics.ViewportGraphics;
038:
039: /**
040: * <p>
041: * Requirements:
042: * <ul>
043: * <li> Mouse is dragged </li>
044: * <li> Current State != Creating </li>
045: * <li> CurrentShape != null </li>
046: * <li> no modifiers or shift down</li>
047: * <li> only mouse 1 down </li>
048: * </ul>
049: * </p>
050: * <p>
051: * Action:
052: * <ul>
053: * <li>draw rectangle</li>
054: * <li>add behaviour that will select vertices on mouse Released</li>
055: * <li>Locks the EditTool handler until mouse is released so other behaviours won't interfere</li>
056: * </ul>
057: * </p>
058: *
059: * @author jones
060: * @since 1.1.0
061: */
062: public class SelectionBoxBehaviour implements LockingBehaviour {
063:
064: private DrawShapeCommand drawShapeCommand;
065: private SelectBehaviour selectBehaviour;
066:
067: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
068: EventType eventType) {
069: boolean legalState = handler.getCurrentState() != EditState.CREATING;
070: boolean legalEventType = eventType == EventType.DRAGGED;
071: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
072: boolean legalModifiers = e.isShiftDown() || !e.modifiersDown();
073:
074: return legalState && legalEventType && shapeAndGeomNotNull
075: && legalModifiers && e.buttons == MapMouseEvent.BUTTON1;
076: }
077:
078: public UndoableMapCommand getCommand(EditToolHandler handler,
079: MapMouseEvent e, EventType eventType) {
080:
081: if (selectBehaviour == null) {
082: handler.lock(this );
083: selectBehaviour = new SelectBehaviour();
084: handler.getBehaviours().add(selectBehaviour);
085: Color selectionColor = PreferenceUtil.instance()
086: .getSelectionColor();
087: drawShapeCommand = new DrawShapeCommand(new Rectangle(e.x,
088: e.y, 1, 1), selectionColor,
089: ViewportGraphics.LINE_DASH, 1);
090: drawShapeCommand.setFill(new Color(selectionColor.getRed(),
091: selectionColor.getGreen(),
092: selectionColor.getBlue(), 100));
093:
094: handler.getContext().getViewportPane().addDrawCommand(
095: drawShapeCommand);
096: }
097: Point start = handler.getMouseTracker().getDragStarted();
098: drawShapeCommand.setShape(new Rectangle(Math.min(start.getX(),
099: e.x), Math.min(start.getY(), e.y), Math.abs(start
100: .getX()
101: - e.x), Math.abs(start.getY() - e.y)));
102: EditPlugin.log(drawShapeCommand.getShape().toString(), null);
103:
104: handler.repaint();
105:
106: return null;
107: }
108:
109: public void handleError(EditToolHandler handler, Throwable error,
110: UndoableMapCommand command) {
111: EditPlugin.log("", error); //$NON-NLS-1$
112: }
113:
114: class SelectBehaviour implements LockingBehaviour {
115:
116: public boolean isValid(EditToolHandler handler,
117: MapMouseEvent e, EventType eventType) {
118:
119: boolean legalEventType = eventType == EventType.RELEASED;
120:
121: return legalEventType;
122: }
123:
124: public UndoableMapCommand getCommand(EditToolHandler handler,
125: MapMouseEvent e, EventType eventType) {
126: List<EventBehaviour> behaviours = handler.getBehaviours();
127: behaviours.remove(selectBehaviour);
128:
129: drawShapeCommand.setValid(false);
130:
131: Set<Point> points = new HashSet<Point>();
132: Rectangle rect = (Rectangle) drawShapeCommand.getShape();
133: EditGeom geom = handler.getCurrentGeom();
134: for (int x = rect.x, maxx = (int) rect.getMaxX(); x < maxx; x++) {
135: for (int y = rect.y, maxy = (int) rect.getMaxY(); y < maxy; y++) {
136: Point valueOf = Point.valueOf(x, y);
137: if (geom.hasVertex(valueOf))
138: points.add(valueOf);
139: }
140: }
141:
142: handler.unlock(this );
143: selectBehaviour = null;
144: drawShapeCommand = null;
145: if (e.isShiftDown())
146: return new SelectPointCommand(handler.getCurrentGeom()
147: .getEditBlackboard(), points,
148: SelectPointCommand.Type.ADD);
149: else {
150: return new SelectPointCommand(handler.getCurrentGeom()
151: .getEditBlackboard(), points,
152: SelectPointCommand.Type.SET);
153: }
154:
155: }
156:
157: public void handleError(EditToolHandler handler,
158: Throwable error, UndoableMapCommand command) {
159: EditPlugin.log("", error); //$NON-NLS-1$
160: }
161:
162: public Object getKey(EditToolHandler handler) {
163: return SelectionBoxBehaviour.this ;
164: }
165:
166: }
167:
168: public Object getKey(EditToolHandler handler) {
169: return this;
170: }
171:
172: }
|