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.SetEditStateCommand;
030: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
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 geom are set</li>
044: * <li>mouse is over the first vertex of the currentShape</li>
045: *</ul>
046: * </p>
047: * <p>Action:
048: * <ul>
049: * <li></li>
050: *</ul>
051: * </p>
052: * @author jones
053: * @since 1.1.0
054: */
055: public class AcceptWhenOverFirstVertexBehaviour implements
056: EventBehaviour {
057:
058: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
059: EventType eventType) {
060: boolean legalState = handler.getCurrentState() == EditState.CREATING;
061: boolean legalEventType = eventType == EventType.RELEASED;
062: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
063: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
064:
065: return legalState && legalEventType && shapeAndGeomNotNull
066: && button1Released && !e.buttonsDown()
067: && !e.modifiersDown() && overFirstVertex(handler, e);
068: }
069:
070: private boolean overFirstVertex(EditToolHandler handler,
071: MapMouseEvent e) {
072: Point vertexOver = handler.getEditBlackboard(
073: handler.getEditLayer()).overVertex(
074: Point.valueOf(e.x, e.y),
075: PreferenceUtil.instance().getVertexRadius());
076:
077: return handler.getCurrentShape().getNumPoints() > 0
078: && handler.getCurrentShape().getPoint(0).equals(
079: vertexOver);
080: }
081:
082: public UndoableMapCommand getCommand(EditToolHandler handler,
083: MapMouseEvent e, EventType eventType) {
084: if (!isValid(handler, e, eventType))
085: throw new IllegalArgumentException(
086: "Current state is not legal"); //$NON-NLS-1$
087: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
088:
089: commands.add(handler.getCommand(handler.getAcceptBehaviours()));
090: if (handler.getCurrentState() == EditState.CREATING)
091: commands.add(new SetEditStateCommand(handler,
092: EditState.MODIFYING));
093:
094: UndoableComposite undoableComposite = new UndoableComposite(
095: commands);
096: undoableComposite.setMap(handler.getContext().getMap());
097: try {
098: undoableComposite.execute(new NullProgressMonitor());
099: } catch (Exception e1) {
100: throw (RuntimeException) new RuntimeException()
101: .initCause(e1);
102: }
103: return new UndoRedoCommand(undoableComposite);
104: }
105:
106: public void handleError(EditToolHandler handler, Throwable error,
107: UndoableMapCommand command) {
108: EditPlugin.log("", error); //$NON-NLS-1$
109: }
110:
111: }
|