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.UndoRedoCommand;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.project.ui.AnimationUpdater;
020: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
021: import net.refractions.udig.tool.edit.internal.Messages;
022: import net.refractions.udig.tools.edit.EditPlugin;
023: import net.refractions.udig.tools.edit.EditState;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.EventBehaviour;
026: import net.refractions.udig.tools.edit.EventType;
027: import net.refractions.udig.tools.edit.animation.MessageBubble;
028: import net.refractions.udig.tools.edit.commands.AddVertexCommand;
029: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
030: import net.refractions.udig.tools.edit.support.EditBlackboard;
031: import net.refractions.udig.tools.edit.support.Point;
032:
033: import org.eclipse.core.runtime.NullProgressMonitor;
034:
035: /**
036: * <p>Requirements:
037: * <ul>
038: * <li>event type == RELEASE</li>
039: * <li>edit state == CREATING </li>
040: * <li>no modifiers down</li>
041: * <li>button 1 released</li>
042: * <li>no buttons down</li>
043: * <li>current shape and geometry are set</li>
044: * <li>mouse is not over a vertex of the current shape</li>
045: * </ul>
046: * </p>
047: * <p>Action:
048: * <ul>
049: * <li>Add a vertex as the last vertex in the shape</li>
050: * </ul>
051: * </p>
052: * @author Jesse
053: * @since 1.1.0
054: */
055: public class AddVertexWhileCreatingBehaviour implements EventBehaviour {
056:
057: private IEditValidator validator = IEditValidator.TRUE;
058:
059: public void setEditValidator(IEditValidator validator) {
060: this .validator = validator;
061: }
062:
063: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
064: EventType eventType) {
065: boolean legalState = handler.getCurrentState() == EditState.CREATING;
066: boolean legalEventType = eventType == EventType.RELEASED;
067: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
068: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
069:
070: return legalState && legalEventType && shapeAndGeomNotNull
071: && button1Released && !e.buttonsDown()
072: && !e.modifiersDown() && !overShapeVertex(handler, e);
073: }
074:
075: private boolean overShapeVertex(EditToolHandler handler,
076: MapMouseEvent e) {
077:
078: Point vertexOver = handler.getEditBlackboard(
079: handler.getEditLayer()).overVertex(
080: Point.valueOf(e.x, e.y),
081: PreferenceUtil.instance().getVertexRadius());
082:
083: return handler.getCurrentShape().hasVertex(vertexOver);
084: }
085:
086: public void handleError(EditToolHandler handler, Throwable error,
087: UndoableMapCommand command) {
088: EditPlugin.log("", error); //$NON-NLS-1$
089: }
090:
091: public UndoableMapCommand getCommand(EditToolHandler handler,
092: MapMouseEvent e, EventType eventType) {
093:
094: String reasonForFaliure = validator.isValid(handler, e,
095: eventType);
096: if (reasonForFaliure != null) {
097: AnimationUpdater
098: .runTimer(
099: handler.getContext().getMapDisplay(),
100: new MessageBubble(
101: e.x,
102: e.y,
103: Messages.AddVertexWhileCreatingBehaviour_illegal
104: + "\n" + reasonForFaliure, PreferenceUtil.instance().getMessageDisplayDelay())); //$NON-NLS-1$
105: return null;
106: }
107:
108: Point valueOf = Point.valueOf(e.x, e.y);
109: EditBlackboard editBlackboard = handler
110: .getEditBlackboard(handler.getEditLayer());
111: Point destination = handler.getEditBlackboard(
112: handler.getEditLayer()).overVertex(valueOf,
113: PreferenceUtil.instance().getVertexRadius());
114: if (destination == null)
115: destination = valueOf;
116:
117: AddVertexCommand addVertexCommand = new AddVertexCommand(
118: handler, editBlackboard, destination);
119: try {
120: addVertexCommand.setMap(handler.getContext().getMap());
121: addVertexCommand.run(new NullProgressMonitor());
122: } catch (Exception e1) {
123: throw (RuntimeException) new RuntimeException()
124: .initCause(e1);
125: }
126: return new UndoRedoCommand(addVertexCommand);
127: }
128:
129: }
|