/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.swing.tool;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import org.geotools.swing.event.MapMouseEvent;
/**
* A map panning tool for JMapPane. Allows the user to drag the map
* with the mouse.
*
* @author Michael Bedward
* @since 2.6
* @source $URL$
* @version $Id$
*/
public class PanTool extends CursorTool {
private static final ResourceBundle stringRes = ResourceBundle.getBundle("org/geotools/swing/Text");
/** Tool name */
public static final String TOOL_NAME = stringRes.getString("tool_name_pan");
/** Tool tip text */
public static final String TOOL_TIP = stringRes.getString("tool_tip_pan");
/** Cursor */
public static final String CURSOR_IMAGE = "/org/geotools/swing/icons/mActionPan.png";
/** Cursor hotspot coordinates */
public static final Point CURSOR_HOTSPOT = new Point(15, 15);
/** Icon for the control */
public static final String ICON_IMAGE = "/org/geotools/swing/icons/mActionPan.png";
private Cursor cursor;
private Point panePos;
boolean panning;
/**
* Constructor
*/
public PanTool() {
Toolkit tk = Toolkit.getDefaultToolkit();
ImageIcon imgIcon = new ImageIcon(getClass().getResource(CURSOR_IMAGE));
cursor = tk.createCustomCursor(imgIcon.getImage(), CURSOR_HOTSPOT, TOOL_NAME);
panning = false;
}
/**
* Respond to a mouse button press event from the map mapPane. This may
* signal the start of a mouse drag. Records the event's window position.
* @param ev the mouse event
*/
@Override
public void onMousePressed(MapMouseEvent ev) {
panePos = ev.getPoint();
panning = true;
}
/**
* Respond to a mouse dragged event. Calls {@link org.geotools.swing.JMapPane#moveImage()}
* @param ev the mouse event
*/
@Override
public void onMouseDragged(MapMouseEvent ev) {
if (panning) {
Point pos = ev.getPoint();
if (!pos.equals(panePos)) {
getMapPane().moveImage(pos.x - panePos.x, pos.y - panePos.y);
panePos = pos;
}
}
}
/**
* If this button release is the end of a mouse dragged event, requests the
* map mapPane to repaint the display
* @param ev the mouse event
*/
@Override
public void onMouseReleased(MapMouseEvent ev) {
panning = false;
getMapPane().repaint();
}
/**
* Get the mouse cursor for this tool
*/
@Override
public Cursor getCursor() {
return cursor;
}
/**
* Returns false to indicate that this tool does not draw a box
* on the map display when the mouse is being dragged
*/
@Override
public boolean drawDragBox() {
return false;
}
}