//
// UIService.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.ui;

import imagej.AbstractService;
import imagej.ImageJ;
import imagej.Service;
import imagej.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;

/**
 * Service for the ImageJ user interface.
 * 
 * @author Curtis Rueden
 */
@Service
public final class UIService extends AbstractService {

	/** The active user interface. */
	private UserInterface userInterface;

	/** Available user interfaces. */
	private List<UserInterface> availableUIs;

	// -- Constructors --

	public UIService() {
		// NB: Required by SezPoz.
		super(null);
		throw new UnsupportedOperationException();
	}

	public UIService(final ImageJ context) {
		super(context);
	}

	// -- UIService methods --

	/** Processes the given command line arguments. */
	public void processArgs(final String[] args) {
		Log.info("Received command line arguments:");
		for (final String arg : args)
			Log.info("\t" + arg);
		userInterface.processArgs(args);
	}

	/** Gets the active user interface. */
	public UserInterface getUI() {
		return userInterface;
	}

	/** Gets the user interfaces available on the classpath. */
	public List<UserInterface> getAvailableUIs() {
		return availableUIs;
	}

	// -- IService methods --

	@Override
	public void initialize() {
		final List<UserInterface> uis = discoverUIs();
		availableUIs = Collections.unmodifiableList(uis);
		if (uis.size() > 0) {
			final UserInterface ui = uis.get(0);
			Log.info("Launching user interface: " + ui.getClass().getName());
			ui.initialize();
			userInterface = ui;
		}
		else {
			Log.warn("No user interfaces found.");
			userInterface = null;
		}
	}

	// -- Helper methods --

	/** Discovers user interfaces using SezPoz. */
	private List<UserInterface> discoverUIs() {
		final List<UserInterface> uis = new ArrayList<UserInterface>();
		for (final IndexItem<UI, UserInterface> item : Index.load(UI.class,
			UserInterface.class))
		{
			try {
				final UserInterface ui = item.instance();
				Log.info("Discovered user interface: " + ui.getClass().getName());
				uis.add(ui);
			}
			catch (final InstantiationException e) {
				Log.warn("Invalid user interface: " + item, e);
			}
		}
		return uis;
	}

}

@@ -1713,32 +1713,104 @@
 imagej.Service;%0A
+import imagej.ext.plugin.PluginService;%0Aimport imagej.tool.ToolService;%0A
 import imagej.ut
@@ -2109,16 +2109,101 @@
 vice %7B%0A%0A
+%09private final PluginService pluginService;%0A%09private final ToolService toolService;%0A%0A
 %09/** The
@@ -2528,61 +2528,335 @@
 text
-) %7B%0A%09%09super(context);%0A%09%7D%0A%0A%09// -- UIService methods --
+, final PluginService pluginService, final ToolService toolService) %7B%0A%09%09super(context);%0A%09%09this.pluginService = pluginService;%0A%09%09this.toolService = toolService;%0A%09%7D%0A%0A%09// -- UIService methods --%0A%0A%09public PluginService getPluginService() %7B%0A%09%09return pluginService;%0A%09%7D%0A%0A%09public ToolService getToolService() %7B%0A%09%09return toolService;%0A%09%7D
 %0A%0A%09/

Add more explicit dependencies to UI service: