/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2012 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. 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.
 * 
 * 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.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.core.plugins.axispos;

import imagej.data.display.ImageDisplay;
import imagej.ext.menu.MenuConstants;
import imagej.ext.module.DefaultModuleItem;
import imagej.ext.module.ItemIO;
import imagej.ext.plugin.DynamicPlugin;
import imagej.ext.plugin.Menu;
import imagej.ext.plugin.Parameter;
import imagej.ext.plugin.Plugin;

import java.util.ArrayList;

import net.imglib2.meta.Axes;
import net.imglib2.meta.AxisType;

/**
 * Changes the axis to move along to user specified value. This axis of movement
 * is used in the move axis position forward/backward plugins.
 * 
 * @author Barry DeZonia
 */
@Plugin(
	menu = {
		@Menu(label = MenuConstants.IMAGE_LABEL,
			weight = MenuConstants.IMAGE_WEIGHT,
			mnemonic = MenuConstants.IMAGE_MNEMONIC),
		@Menu(label = "Stacks", mnemonic = 's'),
		@Menu(label = "Set Active Axis...") }, headless = true)
public class SetActiveAxis extends DynamicPlugin {

	// -- Constants --

	private static final String AXIS_NAME = "axisName";

	// -- Parameters --

	@Parameter(type = ItemIO.BOTH)
	private ImageDisplay display;

	@Parameter(persist = false, initializer = "initAxisName")
	private String axisName;

	// -- SetActiveAxis methods --

	public ImageDisplay getDisplay() {
		return display;
	}

	public void setDisplay(final ImageDisplay display) {
		this.display = display;
	}

	public AxisType getAxis() {
		return Axes.get(axisName);
	}

	public void setAxis(final AxisType axis) {
		axisName = axis.toString();
	}

	// -- Runnable methods --

	@Override
	public void run() {
		final AxisType axis = getAxis();
		if (axis != null) display.setActiveAxis(axis);
	}

	// -- Initializers --

	protected void initAxisName() {
		@SuppressWarnings("unchecked")
		final DefaultModuleItem<String> axisNameItem =
			(DefaultModuleItem<String>) getInfo().getInput(AXIS_NAME);
		final AxisType[] axes = display.getAxes();
		final ArrayList<String> choices = new ArrayList<String>();
		for (final AxisType a : axes) {
			if (a.isXY()) continue;
			choices.add(a.getLabel());
		}
		axisNameItem.setChoices(choices);
	}

}

@@ -2976,16 +2976,74 @@
 sName;%0A%0A
+%09@Parameter%0A%09private AnimationService animationService;%0A%09%0A
 %09// -- S
@@ -3464,16 +3464,21 @@
 = null) 
+%7B%0A%09%09%09
 display.
@@ -3494,24 +3494,337 @@
 Axis(axis);%0A
+%09%09%09int axisIndex = display.getAxisIndex(axis);%0A%09%09%09long last = display.getExtents().dimension(axisIndex) - 1;%0A%09%09%09Animation a = animationService.getAnimation(display);%0A%09%09%09boolean active = a.isActive();%0A%09%09%09if (active) a.stop();%0A%09%09%09a.setAxis(axis);%0A%09%09%09a.setFirst(0);%0A%09%09%09a.setLast(last);%0A%09%09%09if (active) a.start();%0A%09%09%7D%0A
 %09%7D%0A%0A%09// -- I

Update existing animations when active axis changed