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

import imagej.ImageJ;
import imagej.data.Dataset;
import imagej.event.EventService;
import imagej.ext.display.Display;
import imagej.ext.display.DisplayService;
import imagej.service.AbstractService;
import imagej.service.Service;

import java.util.List;

/**
 * Default service for working with {@link ImageDisplay}s.
 * 
 * @author Barry DeZonia
 * @author Curtis Rueden
 * @author Grant Harris
 */
@Service
public final class DefaultImageDisplayService extends AbstractService
	implements ImageDisplayService
{

	private final EventService eventService;
	private final DisplayService displayService;

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

	public DefaultImageDisplayService(final ImageJ context,
		final EventService eventService, final DisplayService displayService)
	{
		super(context);
		this.eventService = eventService;
		this.displayService = displayService;

		// there are no longer any defined @EventHandlers. restore later if needed
		//subscribeToEvents(eventService);
	}

	// -- ImageDisplayService methods --

	@Override
	public EventService getEventService() {
		return eventService;
	}

	@Override
	public DisplayService getDisplayService() {
		return displayService;
	}

	// -- DisplayService methods - display discovery --

	@Override
	public ImageDisplay getActiveImageDisplay() {
		final Display<?> activeDisplay = displayService.getActiveDisplay();
		if (activeDisplay == null || !(activeDisplay instanceof ImageDisplay)) {
			return null;
		}
		return (ImageDisplay) activeDisplay;
	}

	@Override
	public Dataset getActiveDataset() {
		return getActiveDataset(getActiveImageDisplay());
	}

	@Override
	public DatasetView getActiveDatasetView() {
		return getActiveDatasetView(getActiveImageDisplay());
	}

	@Override
	public Dataset getActiveDataset(final ImageDisplay display) {
		final DatasetView activeDatasetView = getActiveDatasetView(display);
		return activeDatasetView == null ? null : activeDatasetView.getData();
	}

	@Override
	public DatasetView getActiveDatasetView(final ImageDisplay display) {
		if (display == null) return null;
		final DataView activeView = display.getActiveView();
		if (activeView instanceof DatasetView) {
			return (DatasetView) activeView;
		}
		return null;
	}

	@Override
	public List<ImageDisplay> getImageDisplays() {
		return displayService.getDisplaysOfType(ImageDisplay.class);
	}

	// -- Event handlers --

	/* BDZ 3-1-12 - pretty sure this is no longer necessary

	// CTR FIXME display views should not be disposed here!
	// This is the job of the display itself when display.dispose()
	// and/or display.close() gets called.

	// Disposes views when display window is closed.
	@EventHandler
	protected void onEvent(final WinClosedEvent event) {
		final Display<?> display = event.getDisplay();
		if (!(display instanceof ImageDisplay)) return;
		final ImageDisplay imageDisplay = (ImageDisplay) display;
		final ArrayList<DataView> views = new ArrayList<DataView>(imageDisplay);
		for (final DataView view : views) {
			System.out.println("Disposing of a view in DefaultImageDisplay!!!!");
			view.dispose();
		}
	}
	*/
}

@@ -2814,123 +2814,8 @@
 ice;
-%0A%0A%09%09// there are no longer any defined @EventHandlers. restore later if needed%0A%09%09//subscribeToEvents(eventService);
 %0A%09%7D%0A
@@ -4185,747 +4185,6 @@
 %09%7D%0A%0A
-%09// -- Event handlers --%0A%0A%09/* BDZ 3-1-12 - pretty sure this is no longer necessary%0A%0A%09// CTR FIXME display views should not be disposed here!%0A%09// This is the job of the display itself when display.dispose()%0A%09// and/or display.close() gets called.%0A%0A%09// Disposes views when display window is closed.%0A%09@EventHandler%0A%09protected void onEvent(final WinClosedEvent event) %7B%0A%09%09final Display%3C?%3E display = event.getDisplay();%0A%09%09if (!(display instanceof ImageDisplay)) return;%0A%09%09final ImageDisplay imageDisplay = (ImageDisplay) display;%0A%09%09final ArrayList%3CDataView%3E views = new ArrayList%3CDataView%3E(imageDisplay);%0A%09%09for (final DataView view : views) %7B%0A%09%09%09System.out.println(%22Disposing of a view in DefaultImageDisplay!!!!%22);%0A%09%09%09view.dispose();%0A%09%09%7D%0A%09%7D%0A%09*/%0A
 %7D%0A

Remove obsolete code