001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.zoom;
034:
035: import java.awt.*;
036: import java.awt.event.MouseEvent;
037: import java.awt.geom.NoninvertibleTransformException;
038: import java.awt.geom.Point2D;
039:
040: import javax.swing.*;
041:
042: import com.vividsolutions.jts.geom.Envelope;
043: import com.vividsolutions.jump.workbench.ui.cursortool.DragTool;
044: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
045:
046: /**
047: * Pans the image in the current task window.
048: * Image handling is designed to minimize flickering and latency.
049: *
050: * @author Jon Aquino
051: * @version 1.0
052: */
053: public class PanTool extends DragTool {
054: // MD - incorporates fco lavin's fix for eliminating flicker
055: private boolean dragging = false;
056: private Image origImage;
057: private Image auxImage = null;
058:
059: public PanTool() {
060: }
061:
062: public Cursor getCursor() {
063: return createCursor(IconLoader.icon("Hand.gif").getImage());
064: }
065:
066: public Icon getIcon() {
067: return IconLoader.icon("BigHand.gif");
068: }
069:
070: public void mouseDragged(MouseEvent e) {
071: try {
072: if (!dragging) {
073: dragging = true;
074: getPanel().getRenderingManager().setPaintingEnabled(
075: false);
076: cacheImage();
077: }
078:
079: drawImage(e.getPoint());
080: super .mouseDragged(e);
081: } catch (Throwable t) {
082: getPanel().getContext().handleThrowable(t);
083: }
084: }
085:
086: public void mouseReleased(MouseEvent e) {
087: if (!dragging) {
088: return;
089: }
090:
091: getPanel().getRenderingManager().setPaintingEnabled(true);
092: dragging = false;
093: super .mouseReleased(e);
094: }
095:
096: protected Shape getShape(Point2D source, Point2D destination) {
097: return null;
098: }
099:
100: protected void gestureFinished()
101: throws NoninvertibleTransformException {
102: reportNothingToUndoYet();
103:
104: double xDisplacement = getModelDestination().x
105: - getModelSource().x;
106: double yDisplacement = getModelDestination().y
107: - getModelSource().y;
108: Envelope oldEnvelope = getPanel().getViewport()
109: .getEnvelopeInModelCoordinates();
110: getPanel().getViewport().zoom(
111: new Envelope(oldEnvelope.getMinX() - xDisplacement,
112: oldEnvelope.getMaxX() - xDisplacement,
113: oldEnvelope.getMinY() - yDisplacement,
114: oldEnvelope.getMaxY() - yDisplacement));
115: }
116:
117: private void cacheImage() {
118: origImage = createImageIfNeeded(origImage);
119: getPanel().paint(origImage.getGraphics());
120:
121: }
122:
123: private void drawImage(Point p)
124: throws NoninvertibleTransformException {
125: double dx = p.getX() - getViewSource().getX();
126: double dy = p.getY() - getViewSource().getY();
127:
128: auxImage = createImageIfNeeded(auxImage);
129: auxImage.getGraphics().setColor(Color.WHITE);
130: auxImage.getGraphics().fillRect(0, 0,
131: auxImage.getWidth(getPanel()),
132: auxImage.getHeight(getPanel()));
133: auxImage.getGraphics().drawImage(origImage, (int) dx, (int) dy,
134: getPanel());
135: getPanel().getGraphics().drawImage(auxImage, 0, 0, getPanel());
136: }
137:
138: /**
139: * Creates a new BufferedImage if the given image doesn't exist
140: * or is the wrong size for the panel.
141: * @param currImage an image buffer
142: * @return a new image, or the existing one if it's compatible
143: */
144: private Image createImageIfNeeded(Image currImage) {
145: if (currImage == null
146: || currImage.getHeight(null) != getPanel().getHeight()
147: || currImage.getWidth(null) != getPanel().getWidth()) {
148: Graphics2D g = (Graphics2D) getPanel().getGraphics();
149: Image img = g
150: .getDeviceConfiguration()
151: .createCompatibleImage(getPanel().getWidth(),
152: getPanel().getHeight(), Transparency.OPAQUE);
153: return img;
154:
155: }
156: //return getPanel().createBlankPanelImage();
157: return currImage;
158: }
159: }
|