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

import imagej.ImageJ;
import imagej.data.DataObject;
import imagej.data.Dataset;
import imagej.data.roi.Overlay;
import imagej.display.event.DisplayCreatedEvent;
import imagej.event.Events;
import imagej.object.ObjectManager;
import imagej.plugin.Plugin;
import imagej.plugin.PluginEntry;
import imagej.plugin.PluginException;
import imagej.plugin.PluginManager;
import imagej.plugin.PluginModule;
import imagej.plugin.process.PluginPostprocessor;
import imagej.util.Log;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Displays output {@link Dataset}s upon completion of a plugin execution.
 * 
 * @author Curtis Rueden
 */
@Plugin(type = PluginPostprocessor.class)
public class DisplayPostprocessor implements PluginPostprocessor {

	@Override
	public void process(final PluginModule<?> module) {
		final Map<String, Object> outputs = module.getOutputs();
		handleOutput(outputs.values());
	}

	/** Displays output datasets. */
	public void handleOutput(final Object value) {
		if (value instanceof Collection) {
			final Collection<?> collection = (Collection<?>) value;
			for (final Object item : collection)
				handleOutput(item);
		}
		else if (value instanceof Dataset) {
			final Dataset dataset = (Dataset) value;
			if (!isDisplayed(dataset))
				displayDataset(dataset);
		}
		else if (value instanceof Overlay) {
			final Overlay overlay = (Overlay) value;
			if (! isDisplayed(overlay)) {
				displayOverlay(overlay);
			}
		} else {
			// ignore non-Dataset output
		}
	}

	// -- Helper methods --

	/** Determines whether the given data object is currently displayed onscreen. */
	private boolean isDisplayed(final DataObject dataset) {
		// TODO: Keep a reference count instead of manually counting here?
		final ObjectManager objectManager = ImageJ.get(ObjectManager.class);
		final List<Display> displays = objectManager.getObjects(Display.class);
		for (final Display display : displays) {
			for (final DisplayView view : display.getViews()) {
				if (dataset == view.getDataObject()) return true;
			}
		}
		return false;
	}

	private void displayDataset(final Dataset dataset) {
		// get available display plugins from the plugin manager
		final PluginManager pluginManager = ImageJ.get(PluginManager.class);
		final List<PluginEntry<Display>> plugins =
			pluginManager.getPlugins(Display.class);

		for (final PluginEntry<Display> pe : plugins) {
			try {
				final Display displayPlugin = pe.createInstance();
				// display dataset using the first compatible DisplayPlugin
				// TODO: prompt user with dialog box if multiple matches
				if (displayPlugin.canDisplay(dataset)) {
					displayPlugin.display(dataset);
					Events.publish(new DisplayCreatedEvent(displayPlugin));
					break;
				}
			}
			catch (final PluginException e) {
				Log.error("Invalid display plugin: " + pe, e);
			}
		}
	}
	
	private void displayOverlay(final Overlay overlay) {
		// Add the overlay to the currently active display
		final DisplayManager displayManager = ImageJ.get(DisplayManager.class);
		displayManager.getActiveDisplay().display(overlay);
	}

}

@@ -1792,242 +1792,57 @@
 gej.
-display.event.DisplayCreatedEvent;%0Aimport imagej.event.Events;%0Aimport imagej.object.ObjectManager;%0Aimport imagej.plugin.Plugin;%0Aimport imagej.plugin.PluginEntry;%0Aimport imagej.plugin.PluginException;%0Aimport imagej.plugin.PluginManager
+object.ObjectManager;%0Aimport imagej.plugin.Plugin
 ;%0Aim
@@ -1927,32 +1927,8 @@
 sor;
-%0Aimport imagej.util.Log;
 %0A%0Aim
@@ -2108,16 +2108,41 @@
  Rueden%0A
+ * @author Lee Kamentsky%0A
  */%0A@Plu
@@ -2770,27 +2770,119 @@
 et))
+ %7B
 %0A%09%09%09%09
-displayDataset
+final DisplayManager displayManager = ImageJ.get(DisplayManager.class);%0A%09%09%09%09displayManager.createDisplay
 (dat
@@ -2888,16 +2888,21 @@
 taset);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09el
@@ -2988,17 +2988,16 @@
 %09%09%09if (!
- 
 isDispla
@@ -3049,17 +3049,19 @@
 %09%09%09%7D%0A%09%09%7D
- 
+%0A%09%09
 else %7B%0A%09
@@ -3076,26 +3076,31 @@
 ore 
-non-Dataset
+unsupported
  output
+ type
 %0A%09%09%7D
@@ -3668,789 +3668,8 @@
 %09%7D%0A%0A
-%09private void displayDataset(final Dataset dataset) %7B%0A%09%09// get available display plugins from the plugin manager%0A%09%09final PluginManager pluginManager = ImageJ.get(PluginManager.class);%0A%09%09final List%3CPluginEntry%3CDisplay%3E%3E plugins =%0A%09%09%09pluginManager.getPlugins(Display.class);%0A%0A%09%09for (final PluginEntry%3CDisplay%3E pe : plugins) %7B%0A%09%09%09try %7B%0A%09%09%09%09final Display displayPlugin = pe.createInstance();%0A%09%09%09%09// display dataset using the first compatible DisplayPlugin%0A%09%09%09%09// TODO: prompt user with dialog box if multiple matches%0A%09%09%09%09if (displayPlugin.canDisplay(dataset)) %7B%0A%09%09%09%09%09displayPlugin.display(dataset);%0A%09%09%09%09%09Events.publish(new DisplayCreatedEvent(displayPlugin));%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09catch (final PluginException e) %7B%0A%09%09%09%09Log.error(%22Invalid display plugin: %22 + pe, e);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09pri

Use DisplayManager.createDisplay method, + cleanup.