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 net.refractions.udig.project.command.UndoableComposite;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
020: import net.refractions.udig.tools.edit.EditPlugin;
021: import net.refractions.udig.tools.edit.EditToolHandler;
022: import net.refractions.udig.tools.edit.EventBehaviour;
023: import net.refractions.udig.tools.edit.EventType;
024: import net.refractions.udig.tools.edit.commands.RemoveSelectedVerticesCommand;
025: import net.refractions.udig.tools.edit.commands.SelectPointCommand;
026: import net.refractions.udig.tools.edit.commands.SelectPointCommand.Type;
027: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
028: import net.refractions.udig.tools.edit.support.EditBlackboard;
029: import net.refractions.udig.tools.edit.support.Point;
030:
031: /**
032: * A behaviour that deletes a vertex from an editGeom.
033: *
034: * <p>Requirements:
035: * <ul>
036: * <li>event type == RELEASE</li>
037: * <li>edit state == MODIFYING </li>
038: * <li>no modifiers down</li>
039: * <li>button 1 released</li>
040: * <li>no buttons down</li>
041: * <li>current shape and geom are set</li>
042: * <li>mouse is not over a vertex of the current shape</li>
043: * <li>mouse is over an edge </li>
044: * </ul>
045: * </p>
046: *
047: * @author jones
048: * @since 1.1.0
049: */
050: public class RemoveVertexBehaviour implements EventBehaviour {
051:
052: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
053: EventType eventType) {
054: boolean legalEventType = eventType == EventType.RELEASED;
055: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
056: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
057:
058: return legalEventType && shapeAndGeomNotNull && button1Released
059: && !e.buttonsDown() && !e.modifiersDown()
060: && overGeomVertex(handler, e);
061: }
062:
063: private boolean overGeomVertex(EditToolHandler handler,
064: MapMouseEvent e) {
065:
066: Point vertexOver = handler.getEditBlackboard(
067: handler.getEditLayer()).overVertex(
068: Point.valueOf(e.x, e.y),
069: PreferenceUtil.instance().getVertexRadius());
070:
071: return handler.getCurrentGeom().hasVertex(vertexOver);
072: }
073:
074: public UndoableMapCommand getCommand(EditToolHandler handler,
075: MapMouseEvent e, EventType eventType) {
076: if (!isValid(handler, e, eventType))
077: throw new IllegalStateException("isValid() return false"); //$NON-NLS-1$
078:
079: Point vertexOver = handler.getEditBlackboard(
080: handler.getEditLayer()).overVertex(
081: Point.valueOf(e.x, e.y),
082: PreferenceUtil.instance().getVertexRadius());
083:
084: UndoableComposite command = new UndoableComposite();
085: EditBlackboard bb = handler.getCurrentShape()
086: .getEditBlackboard();
087: command.getCommands().add(
088: new SelectPointCommand(bb, vertexOver, Type.SET));
089: command.getCommands().add(
090: new RemoveSelectedVerticesCommand(handler));
091:
092: return command;
093: }
094:
095: public void handleError(EditToolHandler handler, Throwable error,
096: UndoableMapCommand command) {
097: EditPlugin.log("", error); //$NON-NLS-1$
098: }
099:
100: }
|