/*
 *    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.
 */

/*
 * Duplicate of org.geotools.swing.tool.PanTool but removes
 * restriction of calling the moveImage call with JMapPane
 *
 */

package com.sldeditor.map;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.locale.LocaleUtils;
import org.geotools.swing.tool.CursorTool;

/**
 * A cursor tool to pan the map pane display.
 *
 * @author Michael Bedward
 * @since 2.6
 * @source $URL$
 * @version $Id$
 */
public class PanTool extends CursorTool {

    /** Tool name. */
    public static final String TOOL_NAME = LocaleUtils.getValue("CursorTool", "Pan");

    /** Tool tip text. */
    public static final String TOOL_TIP = LocaleUtils.getValue("CursorTool", "PanTooltip");

    /** Cursor. */
    public static final String CURSOR_IMAGE = "/org/geotools/swing/icons/mActionPan.png";

    /** Cursor hotspot coordinates. */
    private 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.MapPane#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;
    }

    /** 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;
    }
}
