//
// AWTDisplayController.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.gui.swing.display;

import imagej.display.DisplayController;
import imagej.display.ImageDisplayWindow;
import imagej.model.AxisLabel;
import imagej.model.Dataset;
import imagej.process.Index;

import java.awt.image.BufferedImage;

import loci.formats.gui.AWTImageTools;
import mpicbg.imglib.image.Image;

/**
 * TODO
 * 
 * @author Grant Harris
 * @author Curtis Rueden
 */
public class AWTDisplayController implements DisplayController {

	private Dataset dataset;
	private Image<?> image;
	private int[] dims;
	private int[] planeDims;
	private AxisLabel[] dimLabels;
	private String imageName;
	private int xIndex, yIndex;
	private int[] pos;
	private final ImageDisplayWindow imgWindow;
	private final SimpleImageDisplay display;
	private Object currentPlane;

	public AWTDisplayController(final SimpleImageDisplay display) {
		this.display = display;
		imgWindow = display.getImageDisplayWindow();
		setDataset(display.getDataset());
	}

	@Override
	public Object getCurrentPlane() {
		return currentPlane;
	}

	@Override
	public AxisLabel[] getDimLabels() {
		return dimLabels;
	}

	@Override
	public int[] getDims() {
		return dims;
	}

	@Override
	public int[] getPos() {
		return pos;
	}

	@Override
	public void setDataset(final Dataset dataset) {
		this.dataset = dataset;
		image = dataset.getImage();
		dims = image.getDimensions();
		dimLabels = dataset.getMetadata().getAxes();
		imageName = dataset.getMetadata().getName();
		// extract width and height
		xIndex = yIndex = -1;
		for (int i = 0; i < dimLabels.length; i++) {
			if (dimLabels[i] == AxisLabel.X) {
				xIndex = i;
			}
			if (dimLabels[i] == AxisLabel.Y) {
				yIndex = i;
			}
		}
		if (xIndex < 0) {
			throw new IllegalArgumentException("No X dimension");
		}
		if (yIndex < 0) {
			throw new IllegalArgumentException("No Y dimension");
		}
		planeDims = new int[dims.length-2];
		int d = 0;
		for (int i = 0; i < dims.length; i++) {
			if ((i == xIndex) || (i == yIndex))
				continue;
			planeDims[d++] = dims[i];
		}
		imgWindow.setTitle(imageName);
		// display first image plane
		pos = new int[dims.length - 2];
		updatePosition();
		//FIXME - disabled. make this UI call in SimpleImageDisplay to
		// avoid incorrectly calculating image canvas dimensions.
		// Reenable if actually needed.
		display.getImageDisplayWindow().pack();
		
		// TODO
		// maybe the best way to handle a change in Dataset, rather than pack() everytime,
		// would be to fire an event here that says my Dataset has changed. the display
		// that owns this Dataset could subscribe to Dataset changed events and it can
		// decide whether its appropriate for the frame to be repacked.
	}

	@Override
	public void updatePosition(final int posIndex, final int newValue) {
		pos[posIndex] = newValue;
		updatePosition();
	}

	// -- Helper methods --

	private void updatePosition() {
		final BufferedImage bImage = getImagePlane();
		if (bImage != null) {
			display.getImageCanvas().setImage(bImage);
		}
		// this is ugly... just trying it out.
		// imgPanel.setNavigationImageEnabled(true);
		imgWindow.setLabel(makeLabel());
	}

	private String makeLabel() {
		final StringBuilder sb = new StringBuilder();
		for (int i = 0, p = -1; i < dims.length; i++) {
			if (AxisLabel.isXY(dimLabels[i])) {
				continue;
			}
			p++;
			if (dims[i] == 1) {
				continue;
			}
			sb.append(dimLabels[i] + ": " + (pos[p] + 1) + "/" + dims[i] +
				"; ");
		}
		sb.append(dims[xIndex] + "x" + dims[yIndex] + "; ");
		sb.append(dataset.getPixelType());
		return sb.toString();
	}

	private BufferedImage getImagePlane() {
		// FIXME - how to get a subset with different axes?
		final int no = Index.positionToRaster(planeDims, pos);
		final Object plane = dataset.getPlane(no);
		currentPlane = plane;
		if (plane instanceof byte[]) {
			return AWTImageTools.makeImage((byte[]) plane, dims[xIndex],
				dims[yIndex], dataset.isSigned());
		}
		else if (plane instanceof short[]) {
			return AWTImageTools.makeImage((short[]) plane, dims[xIndex],
				dims[yIndex], dataset.isSigned());
		}
		else if (plane instanceof int[]) {
			return AWTImageTools.makeImage((int[]) plane, dims[xIndex],
				dims[yIndex], dataset.isSigned());
		}
		else if (plane instanceof float[]) {
			return AWTImageTools.makeImage((float[]) plane, dims[xIndex],
				dims[yIndex]);
		}
		else if (plane instanceof double[]) {
			return AWTImageTools.makeImage((double[]) plane, dims[xIndex],
				dims[yIndex]);
		}
		else {
			throw new IllegalStateException("Unknown data type: " +
				plane.getClass().getName());
		}
	}

}

@@ -2845,24 +2845,89 @@
 rn pos;%0A%09%7D%0A%0A
+%09@Override%0A%09public Dataset getDataset() %7B%0A%09%09return dataset;%0A%09%7D%0A%09%0A
 %09@Override%0A%09

add ability to getDataset() of display controller