//
// PanTool.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.core.tools;

import imagej.data.display.ImageDisplay;
import imagej.ext.MouseCursor;
import imagej.ext.display.Display;
import imagej.ext.display.event.input.KyPressedEvent;
import imagej.ext.display.event.input.MsDraggedEvent;
import imagej.ext.display.event.input.MsPressedEvent;
import imagej.ext.tool.AbstractTool;
import imagej.ext.tool.Tool;
import imagej.util.IntCoords;

/**
 * Tool for panning the display.
 * 
 * @author Rick Lentz
 * @author Grant Harris
 * @author Curtis Rueden
 */
@Tool(name = "Pan",
	description = "Scrolling tool (or press space bar and drag)",
	iconPath = "/icons/tools/pan.png", priority = PanTool.PRIORITY)
public class PanTool extends AbstractTool {

	public static final int PRIORITY = ZoomTool.PRIORITY + 1;

	private static final int PAN_AMOUNT = 10;

	// TODO - Add customization to set pan amount

	private int lastX, lastY;

	@Override
	public void onKeyDown(final KyPressedEvent evt) {
		final Display<?> display = evt.getDisplay();
		if (!(display instanceof ImageDisplay)) return;
		final ImageDisplay imageDisplay = (ImageDisplay) display;

		final int panX, panY;
		switch (evt.getCode()) {
			case UP:
				panX = 0;
				panY = -PAN_AMOUNT;
				break;
			case DOWN:
				panX = 0;
				panY = PAN_AMOUNT;
				break;
			case LEFT:
				panX = -PAN_AMOUNT;
				panY = 0;
				break;
			case RIGHT:
				panX = PAN_AMOUNT;
				panY = 0;
				break;
			default:
				panX = panY = 0;
		}
		if (panX == 0 && panY == 0) return;

		imageDisplay.getCanvas().pan(new IntCoords(panX, panY));
		evt.consume();
	}

	@Override
	public void onMouseDown(final MsPressedEvent evt) {
		lastX = evt.getX();
		lastY = evt.getY();
		evt.consume();
	}

	@Override
	public void onMouseDrag(final MsDraggedEvent evt) {
		final Display<?> display = evt.getDisplay();
		if (!(display instanceof ImageDisplay)) return;
		final ImageDisplay imageDisplay = (ImageDisplay) display;

		final int xDelta = lastX - evt.getX();
		final int yDelta = lastY - evt.getY();
		imageDisplay.getCanvas().pan(new IntCoords(xDelta, yDelta));
		lastX = evt.getX();
		lastY = evt.getY();
		evt.consume();
	}

	@Override
	public MouseCursor getCursor() {
		return MouseCursor.HAND;
	}

}

@@ -1912,32 +1912,84 @@
 MsPressedEvent;%0A
+import imagej.ext.display.event.input.MsWheelEvent;%0A
 import imagej.ex
@@ -3787,32 +3787,414 @@
 .consume();%0A%09%7D%0A%0A
+%09@Override%0A%09public void onMouseWheel(final MsWheelEvent evt) %7B%0A%09%09final Display%3C?%3E display = evt.getDisplay();%0A%09%09if (!(display instanceof ImageDisplay)) return;%0A%09%09final ImageDisplay imageDisplay = (ImageDisplay) display;%0A%0A%09%09// pan up or down%0A%09%09final int rotation = evt.getWheelRotation();%0A%09%09imageDisplay.getCanvas().pan(new IntCoords(0, rotation * PAN_AMOUNT));%0A%09%09evt.consume();%0A%09%7D%0A%0A
 %09@Override%0A%09publ

Add mouse wheel handling to pan tool