//
// AbstractDataView.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.data.display;

import imagej.ImageJ;
import imagej.data.Data;
import imagej.data.Extents;
import imagej.data.Position;
import imagej.data.display.event.DataViewDeselectedEvent;
import imagej.data.display.event.DataViewSelectedEvent;
import imagej.data.display.event.DataViewSelectionEvent;
import imagej.event.EventService;
import net.imglib2.meta.AxisType;

/**
 * Abstract superclass for {@link DataView}s.
 * 
 * @author Curtis Rueden
 */
public abstract class AbstractDataView implements DataView {

	private final Data data;

	protected final EventService eventService;

	private long[] planeDims;
	private long[] position;
	private Position planePosition;

	/** Indicates the view is no longer in use. */
	private boolean disposed;
	
	/**
	 * True if view is selected, false if not.
	 */
	private boolean selected;

	public AbstractDataView(final Data data) {
		eventService = ImageJ.get(EventService.class);
		this.data = data;
		data.incrementReferences();
	}

	// -- DataView methods --

	@Override
	public Data getData() {
		return data;
	}

	@Override
	public Position getPlanePosition() {
		return planePosition;
	}

	@Override
	public long getPlaneIndex() {
		return planePosition.getIndex();
	}

	@Override
	public long getPosition(final AxisType axis) {
		// FIXME
		final int dim = data.getAxisIndex(axis);
		if (dim < 0) {
			throw new IllegalArgumentException("Unknown axis: " + axis);
		}
		return position[dim];
	}
	
	@Override
	public void setPosition(final AxisType axis, final long value) {
		// FIXME
		final int dim = data.getAxisIndex(axis);
		if (dim < 0) {
			throw new IllegalArgumentException("Unknown axis: " + axis);
		}
		position[dim] = value;
		if (dim >= 2)
			planePosition.setPosition(value, dim-2);
	}

	@Override
	public void dispose() {
		if (disposed) return;
		disposed = true;
		data.decrementReferences();
	}

	// -- Helper methods --

	@Override
	public void setSelected(boolean isSelected) {
		if (selected != isSelected) {
			selected = isSelected;
			DataViewSelectionEvent event = isSelected? new DataViewSelectedEvent(this): new DataViewDeselectedEvent(this);
			eventService.publish(event);
		}
	}

	@Override
	public boolean isSelected() {
		return selected;
	}

	@Override
	public boolean isVisible() {
		return true;
	}

	public void setDimensions(long[] dims) {
		position = new long[dims.length];
		planeDims = new long[dims.length-2];
		for (int i = 0; i < planeDims.length; i++)
			planeDims[i] = dims[i+2];
		Extents extents = new Extents(planeDims);
		planePosition = extents.createPosition();
		planePosition.first();
	}
	
}

@@ -2385,17 +2385,16 @@
 sposed;%0A
-%09
 %0A%09/**%0A%09 
@@ -3081,17 +3081,16 @@
 im%5D;%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -3338,20 +3338,17 @@
 im %3E= 2)
-%0A%09%09%09
+ 
 planePos
@@ -3375,17 +3375,19 @@
 lue, dim
--
+ - 
 2);%0A%09%7D%0A%0A
@@ -3414,147 +3414,8 @@
 oid 
-dispose() %7B%0A%09%09if (disposed) return;%0A%09%09disposed = true;%0A%09%09data.decrementReferences();%0A%09%7D%0A%0A%09// -- Helper methods --%0A%0A%09@Override%0A%09public void 
 setS
@@ -3422,16 +3422,22 @@
 elected(
+final 
 boolean 
@@ -3511,16 +3511,22 @@
 ted;%0A%09%09%09
+final 
 DataView
@@ -3547,17 +3547,21 @@
  event =
- 
+%0A%09%09%09%09
 isSelect
@@ -3562,16 +3562,17 @@
 Selected
+ 
 ? new Da
@@ -3596,16 +3596,22 @@
 nt(this)
+%0A%09%09%09%09%09
 : new Da
@@ -3804,16 +3804,27 @@
 ue;%0A%09%7D%0A%0A
+%09@Override%0A
 %09public 
@@ -3828,16 +3828,147 @@
 ic void 
+dispose() %7B%0A%09%09if (disposed) return;%0A%09%09disposed = true;%0A%09%09data.decrementReferences();%0A%09%7D%0A%0A%09// -- Helper methods --%0A%0A%09protected void 
 setDimen
@@ -3973,16 +3973,22 @@
 ensions(
+final 
 long%5B%5D d
@@ -4064,17 +4064,19 @@
 s.length
--
+ - 
 2%5D;%0A%09%09fo
@@ -4144,15 +4144,23 @@
 ms%5Bi
-+
+ + 
 2%5D;%0A%09%09
+final 
 Exte
@@ -4269,12 +4269,11 @@
 t();%0A%09%7D%0A
-%09
 %0A%7D%0A

Clean up style