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.util.ArrayList;
018: import java.util.List;
019:
020: import net.refractions.udig.project.command.UndoableComposite;
021: import net.refractions.udig.project.command.UndoableMapCommand;
022: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
023: import net.refractions.udig.tools.edit.EditPlugin;
024: import net.refractions.udig.tools.edit.EditState;
025: import net.refractions.udig.tools.edit.EditToolHandler;
026: import net.refractions.udig.tools.edit.EventBehaviour;
027: import net.refractions.udig.tools.edit.EventType;
028: import net.refractions.udig.tools.edit.commands.SelectPointCommand;
029: import net.refractions.udig.tools.edit.commands.SetEditStateCommand;
030: import net.refractions.udig.tools.edit.commands.SelectPointCommand.Type;
031: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
032: import net.refractions.udig.tools.edit.support.EditBlackboard;
033: import net.refractions.udig.tools.edit.support.EditGeom;
034: import net.refractions.udig.tools.edit.support.Point;
035: import net.refractions.udig.tools.edit.support.Selection;
036:
037: /**
038: * <p>Requirements:
039: * <ul>
040: * <li>eventType RELEASED</li>
041: * <li>handler has currentGeom</li>
042: * <li>edit state is modified or NONE</li>
043: * <li>only one of ctrl or shift is</li>
044: * <li>Mouse is over a vertex</li>
045: *</ul>
046: * </p>
047: * <p>Action:
048: * <ul>
049: * <li>if no modifiers clears selection and adds selected vertex</li>
050: * <li>if shift down adds selected vertex</li>
051: * <li>if ctrl down removes vertex if in selection or adds selected vertex if not</li>
052: * <li>sets Edit State to Modified</li>
053: *</ul>
054: * </p>
055: * @author jones
056: * @since 1.1.0
057: */
058: public class VertexSelectorBehaviour implements EventBehaviour {
059:
060: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
061: EventType eventType) {
062: boolean currentGeomNotNull = handler.getCurrentGeom() != null;
063: boolean eventTypePressed = eventType == EventType.RELEASED;
064: boolean twoModifiersDown = e.isShiftDown() && e.isControlDown();
065: boolean singleModifierDown = !(twoModifiersDown);
066: boolean altUp = !e.isAltDown();
067: boolean button1Changed = (e.button ^ MapMouseEvent.BUTTON1) == 0;
068:
069: if (!(currentGeomNotNull && eventTypePressed
070: && singleModifierDown && altUp && button1Changed))
071: return false;
072: EditGeom geom = handler.getCurrentGeom();
073: return geom.hasVertex(geom.getEditBlackboard().overVertex(
074: Point.valueOf(e.x, e.y),
075: PreferenceUtil.instance().getVertexRadius()));
076: }
077:
078: public UndoableMapCommand getCommand(EditToolHandler handler,
079: MapMouseEvent e, EventType eventType) {
080: if (!isValid(handler, e, eventType))
081: throw new IllegalArgumentException(
082: "Not valid state", new Exception()); //$NON-NLS-1$
083:
084: EditBlackboard editBlackboard = handler
085: .getEditBlackboard(handler.getEditLayer());
086: Point point = handler.getEditBlackboard(handler.getEditLayer())
087: .overVertex(Point.valueOf(e.x, e.y),
088: PreferenceUtil.instance().getVertexRadius());
089: List<EditGeom> geoms = null;
090: if (point != null)
091: geoms = editBlackboard.getGeoms(point.getX(), point.getY());
092: else {
093: EditPlugin.trace(EditPlugin.SELECTION,
094: "VertexSelectorBehaviour: Not over vertex (" //$NON-NLS-1$
095: + e.x + "," + e.y + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
096: }
097: Selection selection = editBlackboard.getSelection();
098: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
099: if (e.isShiftDown()) {
100: if (geoms != null
101: && geoms.contains(handler.getCurrentGeom())
102: && !editBlackboard.getSelection().contains(point)) {
103: commands.add(new SelectPointCommand(editBlackboard,
104: point, Type.ADD));
105: }
106: } else if (e.isControlDown()) {
107: if (geoms != null
108: && geoms.contains(handler.getCurrentGeom())) {
109: if (selection.contains(point))
110: commands.add(new SelectPointCommand(editBlackboard,
111: point, Type.REMOVE));
112: else {
113: commands.add(new SelectPointCommand(editBlackboard,
114: point, Type.ADD));
115: }
116: }
117: } else {
118: if (selection.size() != 1 || !selection.contains(point))
119: commands.add(new SelectPointCommand(editBlackboard,
120: point, Type.SET));
121: }
122: if (geoms != null && geoms.contains(handler.getCurrentGeom())) {
123: if (handler.getCurrentState() == EditState.NONE) {
124: commands.add(new SetEditStateCommand(handler,
125: EditState.MODIFYING));
126: }
127: }
128: if (commands.size() == 0)
129: return null;
130: else
131: return new UndoableComposite(commands);
132: }
133:
134: public void handleError(EditToolHandler handler, Throwable error,
135: UndoableMapCommand command) {
136: EditPlugin.log("", error); //$NON-NLS-1$
137: }
138:
139: }
|