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.UndoRedoCommand;
021: import net.refractions.udig.project.command.UndoableComposite;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.EditState;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.EventBehaviour;
028: import net.refractions.udig.tools.edit.EventType;
029: import net.refractions.udig.tools.edit.commands.SelectPointCommand;
030: import net.refractions.udig.tools.edit.commands.SetEditStateCommand;
031: import net.refractions.udig.tools.edit.commands.SelectPointCommand.Type;
032: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
033: import net.refractions.udig.tools.edit.support.EditBlackboard;
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.Selection;
037:
038: import org.eclipse.core.runtime.NullProgressMonitor;
039:
040: /**
041: * <p>Requirements:
042: * <ul>
043: * <li>eventType PRESSED</li>
044: * <li>handler has currentGeom</li>
045: * <li>edit state is modified or NONE</li>
046: *</ul>
047: * </p>
048: * <p>Action:
049: * <ul>
050: * <li>adds selected vertex</li>
051: * <li>sets Edit State to Modified</li>
052: *</ul>
053: * </p>
054: * @author jones
055: * @since 1.1.0
056: */
057: public class MouseDownVertexSelectorBehaviour implements EventBehaviour {
058:
059: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
060: EventType eventType) {
061: boolean currentGeomNotNull = handler.getCurrentGeom() != null;
062: boolean eventTypePressed = eventType == EventType.PRESSED;
063: boolean button1Changed = e.button == MapMouseEvent.BUTTON1;
064: EditBlackboard editBlackboard = handler
065: .getEditBlackboard(handler.getEditLayer());
066: boolean overPoint = editBlackboard.overVertex(Point.valueOf(
067: e.x, e.y), PreferenceUtil.instance().getVertexRadius()) != null;
068:
069: return currentGeomNotNull && eventTypePressed && button1Changed
070: && !e.modifiersDown()
071: && e.buttons == MapMouseEvent.BUTTON1 && overPoint;
072: }
073:
074: public UndoableMapCommand getCommand(EditToolHandler handler,
075: MapMouseEvent e, EventType eventType) {
076: if (!isValid(handler, e, eventType))
077: throw new IllegalArgumentException(
078: "Not valid state", new Exception()); //$NON-NLS-1$
079:
080: EditBlackboard editBlackboard = handler
081: .getEditBlackboard(handler.getEditLayer());
082: Point point = handler.getEditBlackboard(handler.getEditLayer())
083: .overVertex(Point.valueOf(e.x, e.y),
084: PreferenceUtil.instance().getVertexRadius());
085: List<EditGeom> geoms = null;
086: if (point != null)
087: geoms = editBlackboard.getGeoms(point.getX(), point.getY());
088: else {
089: EditPlugin.trace(EditPlugin.SELECTION,
090: "VertexSelectorBehaviour: Not over vertex (" //$NON-NLS-1$
091: + e.x + "," + e.y + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
092: return null;
093: }
094: Selection selection = editBlackboard.getSelection();
095: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
096:
097: if (!selection.contains(point))
098: commands.add(new SelectPointCommand(editBlackboard, point,
099: Type.SET));
100:
101: if (geoms != null && geoms.contains(handler.getCurrentGeom())) {
102: if (handler.getCurrentState() == EditState.NONE) {
103: commands.add(new SetEditStateCommand(handler,
104: EditState.MODIFYING));
105: }
106: }
107: if (commands.size() == 0)
108: return null;
109: else {
110: UndoableComposite undoableComposite = new UndoableComposite(
111: commands);
112: undoableComposite.setMap(handler.getContext().getMap());
113: try {
114: undoableComposite.execute(new NullProgressMonitor());
115: } catch (Exception e1) {
116: throw (RuntimeException) new RuntimeException()
117: .initCause(e1);
118: }
119: return new UndoRedoCommand(undoableComposite);
120: }
121: }
122:
123: public void handleError(EditToolHandler handler, Throwable error,
124: UndoableMapCommand command) {
125: EditPlugin.log("", error); //$NON-NLS-1$
126: }
127:
128: }
|