/*
 * #%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.console;

import imagej.command.Command;
import imagej.command.CommandInfo;
import imagej.command.CommandService;
import imagej.data.Dataset;
import imagej.display.DisplayService;
import imagej.io.IOService;
import imagej.log.LogService;
import imagej.options.OptionsService;
import imagej.plugin.Parameter;
import imagej.plugin.Plugin;
import imagej.service.AbstractService;
import imagej.service.Service;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.io.ImgIOException;

/**
 * Default service for managing interaction with the console.
 * 
 * @author Curtis Rueden
 */
@Plugin(type = Service.class)
public class DefaultConsoleService extends AbstractService implements
	ConsoleService
{

	@Parameter
	private LogService log;

	@Parameter
	private OptionsService optionsService;

	@Parameter
	private CommandService commandService;

	@Parameter
	private IOService ioService;

	@Parameter
	private DisplayService displayService;

	// -- ConsoleService methods --

	@Override
	public void processArgs(final String... args) {
		// TODO: Implement handling of more command line arguments.
		log.debug("Received command line arguments:");
		for (int i = 0; i < args.length; i++) {
			final String arg = args[i];
			log.debug("\t" + arg);
			if (arg.equals("--open")) {
				open(args[i + 1]);
			}
			else if (arg.equals("--run")) {
				if (!run(args[i + 1])) {
					run(args[i + 1], args.length < i + 3 ? null : args[i + 2]);
				}
			}
		}
	}

	// -- Helper methods --

	/** Implements the "--open" command line argument. */
	private void open(final String source) {
		try {
			final Dataset dataset = ioService.loadDataset(source);
			displayService.createDisplay(dataset.getName(), dataset);
		}
		catch (final ImgIOException exc) {
			log.error("Error loading dataset '" + source + "'", exc);
		}
		catch (final IncompatibleTypeException exc) {
			log.error("Error loading dataset '" + source + "'", exc);
		}
	}

	/** Implements the "--run" command line argument. */
	private boolean run(final String className) {
		return commandService.run(className) != null;
	}

	/** Implements the "--run <label> <optionString>" legacy handling */
	private boolean run(String menuLabel, @SuppressWarnings("unused") final String optionString) {
		final String label = menuLabel.replace('_', ' ');
		CommandInfo<Command> info = null;
		for (final CommandInfo<Command> info2 : commandService.getCommands()) {
			if (label.equals(info2.getTitle())) {
				info = info2;
				break;
			}
		}
		if (info == null) return false;
		// TODO: parse the optionString a la ImageJ1
		return commandService.run(info.getClassName()) != null;
	}

}

@@ -2076,46 +2076,8 @@
 ce;%0A
-import imagej.options.OptionsService;%0A
 impo
@@ -2558,61 +2558,8 @@
 g;%0A%0A
-%09@Parameter%0A%09private OptionsService optionsService;%0A%0A
 %09@Pa
@@ -3952,36 +3952,8 @@
 bel,
- @SuppressWarnings(%22unused%22)
  fin

Fix a couple of compilation warnings in DefaultConsoleService