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.awt.Rectangle;
018: import java.util.Timer;
019: import java.util.TimerTask;
020:
021: import net.refractions.udig.core.IProvider;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseWheelEvent;
025: import net.refractions.udig.tools.edit.EditPlugin;
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.DrawSnapAreaCommand;
030: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
031: import net.refractions.udig.tools.edit.support.Point;
032:
033: /**
034: * <p>
035: * Requirements:
036: * <ul>
037: * <li>EventType==WHEEL</li>
038: * <li>Modifier is ALT</li>
039: * </ul>
040: * </p>
041: * <p>
042: * Action:
043: * <ul>
044: * <li>Increases or decreases size of the snap shape depending on number of clicks.</li>
045: * </ul>
046: * </p>
047: * Note: This is not undoable.
048: *
049: * @author jones
050: * @since 1.1.0
051: */
052: public class SetSnapSizeBehaviour implements EventBehaviour {
053:
054: private DrawSnapAreaCommand command;
055:
056: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
057: EventType eventType) {
058:
059: boolean onlyAltDown = e.isAltDown()
060: && (e.modifiers & MapMouseEvent.ALT_DOWN_MASK) == MapMouseEvent.ALT_DOWN_MASK;
061: return onlyAltDown && eventType == EventType.WHEEL;
062: }
063:
064: public UndoableMapCommand getCommand(EditToolHandler handler,
065: final MapMouseEvent e, EventType eventType) {
066: Rectangle oldBounds = null;
067: synchronized (this ) {
068: if (command != null) {
069: oldBounds = command.getValidArea();
070: }
071: if (command == null) {
072: class PointProvider implements IProvider<Point> {
073:
074: public Point get() {
075: return Point.valueOf(e.x, e.y);
076: }
077:
078: }
079: command = new DrawSnapAreaCommand(new PointProvider());
080: handler.getContext().sendSyncCommand(command);
081: }
082: if (task != null)
083: task.cancel();
084:
085: task = new Deactivator(handler);
086: timer.schedule(task, 1000);
087: }
088:
089: int oldRadius = PreferenceUtil.instance().getSnappingRadius();
090: if (!(e instanceof MapMouseWheelEvent))
091: throw new RuntimeException(
092: "Expected a MapMouseWheelEvent but got a: " + e.getClass().getName()); //$NON-NLS-1$
093: MapMouseWheelEvent event = (MapMouseWheelEvent) e;
094: int i = oldRadius + event.clickCount;
095: if (i < 0)
096: i = 0;
097: PreferenceUtil.instance().setSnappingRadius(i);
098: Rectangle bounds;
099: synchronized (this ) {
100: bounds = command.getValidArea();
101: }
102: if (oldBounds != null)
103: bounds.createUnion(oldBounds);
104:
105: handler.getContext().getViewportPane().repaint(bounds.x,
106: bounds.y, bounds.width, bounds.height);
107: return null;
108: }
109:
110: public void handleError(EditToolHandler handler, Throwable error,
111: UndoableMapCommand command) {
112: EditPlugin.log("", error); //$NON-NLS-1$
113: }
114:
115: Timer timer = new Timer();
116: Deactivator task;
117:
118: class Deactivator extends TimerTask {
119:
120: private EditToolHandler handler;
121:
122: public Deactivator(EditToolHandler handler) {
123: this .handler = handler;
124: }
125:
126: @Override
127: public void run() {
128: synchronized (SetSnapSizeBehaviour.this ) {
129: task = null;
130: command.setValid(false);
131: command = null;
132: handler.repaint();
133: handler.getContext().getViewportPane().repaint();
134: }
135: }
136:
137: }
138:
139: }
|