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.handler;
016:
017: import net.refractions.udig.project.IMap;
018: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
019: import net.refractions.udig.project.ui.AnimationUpdater;
020: import net.refractions.udig.tool.edit.internal.Messages;
021: import net.refractions.udig.tools.edit.activator.GridActivator;
022: import net.refractions.udig.tools.edit.animation.MessageBubble;
023: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
024: import net.refractions.udig.tools.edit.support.SnapBehaviour;
025:
026: import org.eclipse.core.commands.AbstractHandler;
027: import org.eclipse.core.commands.ExecutionEvent;
028: import org.eclipse.core.commands.ExecutionException;
029: import org.eclipse.core.commands.IHandler;
030: import org.eclipse.swt.graphics.Point;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Display;
033:
034: /**
035: * Cycles through the different types of snap behaviour. The preference is set and the new state is displayed
036: * in a little pop-up.
037: *
038: * @author Jesse
039: * @since 1.1.0
040: */
041: public class SnapBehaviourCommandHandler extends AbstractHandler
042: implements IHandler {
043:
044: private final IMapDisplay mapDisplay;
045: private MessageBubble messageBubble;
046: private final IMap map;
047: private final GridActivator activator = new GridActivator();
048:
049: public SnapBehaviourCommandHandler(IMapDisplay display, IMap map) {
050: this .mapDisplay = display;
051: this .map = map;
052: }
053:
054: public Object execute(ExecutionEvent event)
055: throws ExecutionException {
056: SnapBehaviour previousBehaviour = PreferenceUtil.instance()
057: .getSnapBehaviour();
058: switch (previousBehaviour) {
059: case OFF:
060: activator.hideGrid(map);
061: previousBehaviour = SnapBehaviour.CURRENT_LAYER;
062: break;
063: case CURRENT_LAYER:
064: activator.hideGrid(map);
065: previousBehaviour = SnapBehaviour.ALL_LAYERS;
066: break;
067: case ALL_LAYERS:
068: previousBehaviour = SnapBehaviour.GRID;
069: activator.showGrid(map);
070: break;
071: case GRID:
072: previousBehaviour = SnapBehaviour.OFF;
073: activator.hideGrid(map);
074: break;
075:
076: default:
077: break;
078: }
079: PreferenceUtil.instance().setSnapBehaviour(previousBehaviour);
080: displayNewStatus(previousBehaviour);
081: return null;
082: }
083:
084: private void displayNewStatus(SnapBehaviour snapBehaviour) {
085: Display display = Display.getCurrent();
086:
087: String message = null;
088: switch (snapBehaviour) {
089: case OFF:
090: message = Messages.SnapBehaviourCommandHandler_off;
091: break;
092: case SELECTED:
093: message = Messages.SnapBehaviourCommandHandler_selected;
094: break;
095: case CURRENT_LAYER:
096: message = Messages.SnapBehaviourCommandHandler_current;
097: break;
098: case ALL_LAYERS:
099: message = Messages.SnapBehaviourCommandHandler_all;
100: break;
101: case GRID:
102: message = Messages.SnapBehaviourCommandHandler_grid;
103: break;
104:
105: default:
106: break;
107: }
108: if (message != null) {
109: if (messageBubble != null && messageBubble.isValid())
110: messageBubble.setValid(false);
111: Control control = (Control) mapDisplay;
112: Point mouseLocation = control.toControl(display
113: .getCursorLocation());
114: messageBubble = new MessageBubble(mouseLocation.x,
115: mouseLocation.y, message, PreferenceUtil.instance()
116: .getMessageDisplayDelay());
117: AnimationUpdater.runTimer(mapDisplay, messageBubble);
118: }
119: }
120:
121: }
|