/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 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.legacy.patches;

import ij.ImagePlus;
import ij.WindowManager;
import imagej.data.display.ImageDisplay;
import imagej.legacy.LegacyOutputTracker;
import imagej.legacy.LegacyService;
import imagej.legacy.Utils;

/**
 * Overrides {@link ImagePlus} methods.
 * 
 * @author Curtis Rueden
 * @author Barry DeZonia
 */
public final class ImagePlusMethods {

	private ImagePlusMethods() {
		// prevent instantiation of utility class
	}

	/** Appends {@link ImagePlus#updateAndDraw()}. */
	public static void updateAndDraw(final LegacyService legacyService, final ImagePlus obj) {
		if (obj == null) return;
		if (!obj.isProcessor()) return;
		if (obj.getWindow() == null) return;
		if (!legacyService.isLegacyMode()) {
			if (!Utils.isLegacyThread(Thread.currentThread())) return;
			legacyService.getLogService().debug("ImagePlus.updateAndDraw(): " + obj);
		}
		legacyService.getImageMap().registerLegacyImage(obj);
		// TODO - add here too?
		//WindowManager.setCurrentWindow(obj.getWindow());
	}

	/** Appends {@link ImagePlus#repaintWindow()}. */
	public static void repaintWindow(final LegacyService legacyService, final ImagePlus obj) {
		if (obj == null) return;
		if (obj.getWindow() == null) return;
		if (!legacyService.isLegacyMode()) {
			if (!Utils.isLegacyThread(Thread.currentThread())) return;
			legacyService.getLogService().debug("ImagePlus.repaintWindow(): " + obj);
		}
		legacyService.getImageMap().registerLegacyImage(obj);
		// TODO - add here too?
		//WindowManager.setCurrentWindow(obj.getWindow());
	}

	/** Appends {@link ImagePlus#show(String message)}. */
	public static void show(final LegacyService legacyService, final ImagePlus obj,
		@SuppressWarnings("unused") final String message)
	{
		if (obj == null) return;
		if (!legacyService.isLegacyMode()) {
			if (!Utils.isLegacyThread(Thread.currentThread())) return;
			legacyService.getLogService().debug("ImagePlus.show(): " + obj);
		}
		legacyService.getImageMap().registerLegacyImage(obj);
		WindowManager.setCurrentWindow(obj.getWindow());
	}

	/** Appends {@link ImagePlus#hide()}. */
	public static void hide(final LegacyService legacyService, final ImagePlus obj) {
		if (legacyService.isLegacyMode()) return;
		if (obj == null) return;
		if (!legacyService.isLegacyMode() && !Utils.isLegacyThread(Thread.currentThread())) return;
		legacyService.getLogService().debug("ImagePlus.hide(): " + obj);
		LegacyOutputTracker.removeOutput(obj);
		// Original method
		//LegacyOutputTracker.getClosedImps().add(obj);
		// Alternate method
		// begin alternate
		ImageDisplay disp = legacyService.getImageMap().lookupDisplay(obj);
		if (disp == null) {
			legacyService.getImageMap().unregisterLegacyImage(obj);
		}
		else {
			disp.close();
		}
		// end alternate
	}

	/** Appends {@link ImagePlus#close()}. */
	// TODO: LegacyOutputTracker should not be a singleton
	public static void close(final LegacyService legacyService, final ImagePlus obj) {
		if (obj == null) return;
		if (!legacyService.isLegacyMode() && !Utils.isLegacyThread(Thread.currentThread())) return;
		LegacyOutputTracker.addClosed(obj);
	}
}

@@ -2681,32 +2681,41 @@
 : %22 + obj);%0A%09%09%7D%0A
+%09%09try %7B%0A%09
 %09%09legacyService.
@@ -2746,32 +2746,148 @@
 gacyImage(obj);%0A
+%09%09%7D catch (UnsupportedOperationException e) %7B%0A%09%09%09// ignore: the dummy legacy service does not have an image map%0A%09%09%7D%0A
 %09%09// TODO - add 
@@ -4498,16 +4498,25 @@
 ternate%0A
+%09%09try %7B%0A%09
 %09%09ImageD
@@ -4575,24 +4575,25 @@
 lay(obj);%0A%09%09
+%09
 if (disp == 
@@ -4600,16 +4600,17 @@
 null) %7B%0A
+%09
 %09%09%09legac
@@ -4662,18 +4662,20 @@
 obj);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09else %7B
@@ -4675,16 +4675,17 @@
 %09else %7B%0A
+%09
 %09%09%09disp.
@@ -4693,16 +4693,133 @@
 lose();%0A
+%09%09%09%7D%0A%09%09%7D catch (UnsupportedOperationException e) %7B%0A%09%09%09// ignore: the dummy legacy service does not have an image map%0A
 %09%09%7D%0A%09%09//

work around limitations of the DummyLegacyService