//
// ProbeTool.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 mpicbg.imglib.cursor.LocalizableByDimCursor;
import mpicbg.imglib.image.Image;
import mpicbg.imglib.type.numeric.RealType;
import imagej.data.Dataset;
import imagej.display.MouseCursor;
import imagej.display.event.mouse.MsMovedEvent;
import imagej.event.Events;
import imagej.event.StatusEvent;
import imagej.tool.BaseTool;
import imagej.tool.Tool;
import imagej.util.IntCoords;
import imagej.util.RealCoords;

/**
 * TODO
 * 
 * @author Rick Lentz
 * @author Grant Harris
 * @author Barry DeZonia
 */
@Tool(name = "Probe", iconPath = "/tools/probe.png",
	description = "Probe Pixel Tool", priority = ProbeTool.PRIORITY)
public class ProbeTool extends BaseTool {

	public static final int PRIORITY = 204;

	// -- ITool methods --

	// TODO - this hatches cursors over and over - very expensive!
	//   Dataset should have thread local cursors for pulling values
	//   out of a Dataset.
	@Override
	public void onMouseMove(final MsMovedEvent evt) {
		final Dataset dataset = evt.getDisplay().getDataset();
		final Image<? extends RealType<?>> image =
			(Image<? extends RealType<?>>) dataset.getImage();
		LocalizableByDimCursor<? extends RealType<?>> cursor =
			(LocalizableByDimCursor<? extends RealType<?>>)
				image.createLocalizableByDimCursor();
		final int x = evt.getX();
		final int y = evt.getY();
		final IntCoords mousePos = new IntCoords(x, y);
		final RealCoords coords =
			evt.getDisplay().getImageCanvas().panelToImageCoords(mousePos);
		if ( ! evt.getDisplay().getImageCanvas().isInImage(mousePos) )
			Events.publish(new StatusEvent(""));
		else {
			final int cx = coords.getIntX();
			final int cy = coords.getIntY();
			// TODO - another performance bottleneck - many array allocations
			final int[] position = cursor.createPositionArray();
			final int[] currPlanePos = evt.getDisplay().getCurrentPlanePosition();
			// FIXME - assumes x & y axes are first two
			position[0] = x;
			position[1] = y;
			for (int i = 2; i < position.length; i++)
				position[i] = currPlanePos[i-2];
			cursor.setPosition(position);
			double doubleValue = cursor.getType().getRealDouble();
			String s = "";
			if (dataset.isFloat())
				s = "" + doubleValue;
			else
				s = "" + ((long) doubleValue);
			Events.publish(
				new StatusEvent("x=" + cx + ", y=" + cy + ", value=" + s));
		}
		cursor.close();
	}

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

}

@@ -2041,20 +2041,8 @@
 /**%0A
- * TODO%0A * %0A
  * @
@@ -2059,16 +2059,16 @@
 k Lentz%0A
+
  * @auth
@@ -2618,257 +2618,8 @@
 ();%0A
-%09%09final Image%3C? extends RealType%3C?%3E%3E image =%0A%09%09%09(Image%3C? extends RealType%3C?%3E%3E) dataset.getImage();%0A%09%09LocalizableByDimCursor%3C? extends RealType%3C?%3E%3E cursor =%0A%09%09%09(LocalizableByDimCursor%3C? extends RealType%3C?%3E%3E)%0A%09%09%09%09image.createLocalizableByDimCursor();%0A
 %09%09fi
@@ -2815,16 +2815,42 @@
 sePos);%0A
+%09%09// mouse not in image ?%0A
 %09%09if ( !
@@ -2954,16 +2954,40 @@
 %09%09else %7B
+  // mouse is over image
 %0A%09%09%09fina
@@ -3047,24 +3047,278 @@
 .getIntY();%0A
+%09%09%09final Image%3C? extends RealType%3C?%3E%3E image =%0A%09%09%09%09(Image%3C? extends RealType%3C?%3E%3E) dataset.getImage();%0A%09%09%09LocalizableByDimCursor%3C? extends RealType%3C?%3E%3E cursor =%0A%09%09%09%09(LocalizableByDimCursor%3C? extends RealType%3C?%3E%3E)%0A%09%09%09%09%09image.createLocalizableByDimCursor();%0A
 %09%09%09// TODO -
@@ -3568,16 +3568,17 @@
 on%5B0%5D = 
+c
 x;%0A%09%09%09po
@@ -3589,16 +3589,17 @@
 on%5B1%5D = 
+c
 y;%0A%09%09%09fo
@@ -3711,16 +3711,22 @@
 on);%0A%09%09%09
+final 
 double d
@@ -3772,16 +3772,35 @@
 uble();%0A
+%09%09%09cursor.close();%0A
 %09%09%09Strin
@@ -3991,30 +3991,12 @@
 ));%0A
+
 %09%09%7D%0A
-%09%09cursor.close();%0A
 %09%7D%0A%0A

fix bug that affected displayed values when not zoomed 1:1 panned (0,0)