001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.tools.internal;
018:
019: import java.awt.Point;
020: import java.awt.Rectangle;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023:
024: import net.refractions.udig.project.IMap;
025: import net.refractions.udig.project.command.AbstractCommand;
026: import net.refractions.udig.project.command.Command;
027: import net.refractions.udig.project.command.NavCommand;
028: import net.refractions.udig.project.internal.Map;
029: import net.refractions.udig.project.internal.render.ViewportModel;
030: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
031: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
032: import net.refractions.udig.project.ui.commands.IDrawCommand;
033: import net.refractions.udig.project.ui.internal.commands.draw.TranslateCommand;
034: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
035: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
036: import net.refractions.udig.project.ui.tool.AbstractModalTool;
037: import net.refractions.udig.project.ui.tool.ModalTool;
038: import net.refractions.udig.ui.graphics.ViewportGraphics;
039:
040: /**
041: * Provides Pan functionality for MapViewport
042: *
043: * @author Jesse Eichar
044: * @version $Revision: 1.9 $
045: */
046: public class Pan extends AbstractModalTool implements ModalTool {
047: private boolean dragging = false;
048: private Point start = null;
049:
050: private TranslateCommand command;
051:
052: /**
053: * Creates an new instance of Pan
054: */
055: public Pan() {
056: super (MOUSE | MOTION);
057: }
058:
059: /**
060: * @see net.refractions.udig.project.ui.tool.AbstractTool#mouseDragged(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
061: */
062: public void mouseDragged(MapMouseEvent e) {
063: if (dragging) {
064: command.setTranslation(e.x - start.x, e.y - start.y);
065: context.getViewportPane().repaint();
066: }
067: }
068:
069: /**
070: * @see net.refractions.udig.project.ui.tool.AbstractTool#mousePressed(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
071: */
072: public void mousePressed(MapMouseEvent e) {
073:
074: if (e.state == MapMouseEvent.BUTTON1
075: && !(e.state == (MapMouseEvent.ALT_DOWN_MASK))) {
076: ((ViewportPane) context.getMapDisplay())
077: .enableDrawCommands(false);
078: dragging = true;
079: start = e.getPoint();
080: command = context.getDrawFactory().createTranslateCommand(
081: 0, 0);
082: context.sendASyncCommand(command);
083: }
084: }
085:
086: /**
087: * @see net.refractions.udig.project.ui.tool.AbstractTool#mouseReleased(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
088: */
089: public void mouseReleased(MapMouseEvent e) {
090: if (dragging) {
091: ((ViewportPane) context.getMapDisplay())
092: .enableDrawCommands(true);
093: Point end = e.getPoint();
094: NavCommand finalPan = context.getNavigationFactory()
095: .createPanCommandUsingScreenCoords(start.x - end.x,
096: start.y - end.y);
097: context.sendASyncCommand(new PanAndInvalidate(finalPan,
098: command));
099:
100: dragging = false;
101:
102: }
103: }
104:
105: /**
106: * @see net.refractions.udig.project.ui.tool.Tool#dispose()
107: */
108: public void dispose() {
109: super .dispose();
110: }
111:
112: /**
113: * Executes the specified pan command, and only after it is executed, expires the last translate command
114: */
115: private class PanAndInvalidate implements Command, NavCommand {
116:
117: private NavCommand command;
118: private TranslateCommand expire;
119:
120: PanAndInvalidate(NavCommand command, TranslateCommand expire) {
121: this .command = command;
122: this .expire = expire;
123: }
124:
125: public Command copy() {
126: return new PanAndInvalidate(command, expire);
127: }
128:
129: public String getName() {
130: return "PanAndDiscard";
131: }
132:
133: public void run(IProgressMonitor monitor) throws Exception {
134: try {
135: command.run(monitor);
136: } finally {
137: expire.setValid(false);
138: }
139: }
140:
141: public void setViewportModel(ViewportModel model) {
142: command.setViewportModel(model);
143: }
144:
145: public Map getMap() {
146: return command.getMap();
147: }
148:
149: public void setMap(IMap map) {
150: command.setMap(map);
151: }
152:
153: public void rollback(IProgressMonitor monitor) throws Exception {
154: command.rollback(monitor);
155: }
156:
157: }
158: }
|