01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.behaviour;
16:
17: import net.refractions.udig.core.IProvider;
18: import net.refractions.udig.project.command.UndoableMapCommand;
19: import net.refractions.udig.project.ui.commands.IDrawCommand;
20: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
21: import net.refractions.udig.tools.edit.EditPlugin;
22: import net.refractions.udig.tools.edit.EditToolHandler;
23: import net.refractions.udig.tools.edit.EventBehaviour;
24: import net.refractions.udig.tools.edit.EventType;
25: import net.refractions.udig.tools.edit.commands.DrawSnapAreaCommand;
26: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
27: import net.refractions.udig.tools.edit.support.Point;
28: import net.refractions.udig.tools.edit.support.SnapBehaviour;
29:
30: /**
31: * Shows the snap area around the cursor if snapping is on.
32: *
33: * @author Jesse
34: * @since 1.1.0
35: */
36: public class DrawCreateVertexSnapAreaBehaviour implements
37: EventBehaviour {
38:
39: private IDrawCommand command;
40: Provider provider = new Provider();
41:
42: private static class Provider implements IProvider<Point> {
43: MapMouseEvent e;
44:
45: public Point get() {
46: return Point.valueOf(e.x, e.y);
47: }
48:
49: }
50:
51: public UndoableMapCommand getCommand(EditToolHandler handler,
52: final MapMouseEvent e, EventType eventType) {
53: provider.e = e;
54: boolean exiting = EventType.EXITED == eventType;
55: boolean snapping = PreferenceUtil.instance().getSnapBehaviour() != SnapBehaviour.OFF
56: && PreferenceUtil.instance().getSnapBehaviour() != SnapBehaviour.GRID;
57: if (command == null && snapping && !exiting) {
58: command = new DrawSnapAreaCommand(provider);
59: handler.getContext().sendASyncCommand(command);
60: handler.getDrawCommands().add(command);
61: } else if (!snapping || exiting) {
62: command.setValid(false);
63: handler.getDrawCommands().remove(command);
64: command = null;
65: }
66: handler.repaint();
67: return null;
68: }
69:
70: public void handleError(EditToolHandler handler, Throwable error,
71: UndoableMapCommand command) {
72: EditPlugin.log("", error); //$NON-NLS-1$
73: }
74:
75: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
76: EventType eventType) {
77: boolean snapOn = PreferenceUtil.instance().getSnapBehaviour() != SnapBehaviour.OFF
78: && PreferenceUtil.instance().getSnapBehaviour() != SnapBehaviour.GRID;
79: boolean mouseMoving = (eventType == EventType.MOVED
80: || eventType == EventType.DRAGGED || eventType != EventType.EXITED);
81: boolean shouldTurnOff = (command != null && eventType != EventType.HOVERED);
82: return shouldTurnOff || (snapOn && mouseMoving);
83: }
84:
85: }
|