//
// LegacyImageMap.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.legacy;

import ij.ImagePlus;

import imagej.data.Dataset;
import imagej.data.event.DatasetDeletedEvent;
import imagej.event.EventSubscriber;
import imagej.event.Events;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * TODO
 *
 * @author Curtis Rueden
 * @author Barry DeZonia
 */
public class LegacyImageMap {

	// -- instance variables --
	
	//private Object lock;
	private Map<ImagePlus, Dataset> imageTable;
	private ImageTranslator imageTranslator;
	private ArrayList<EventSubscriber<?>> subscribers;

	// -- public interface --

	/** default constructor */
	public LegacyImageMap() {
		//lock = new Object();
		//imageTable = new WeakHashMap<ImagePlus, Dataset>();
		imageTable = new ConcurrentHashMap<ImagePlus, Dataset>();
		imageTranslator = new DefaultImageTranslator();
		subscribers = new ArrayList<EventSubscriber<?>>();
		subscribeToEvents();
	}

	/** returns the result of a lookup for a given ImagePlus */
	public Dataset findDataset(ImagePlus imp) {
		//synchronized(lock) {
			return imageTable.get(imp);
		//}
	}

	/** returns the result of a lookup for a given Dataset */
	public ImagePlus findImagePlus(Dataset ds) {
		//synchronized(lock) {
			return getParallelImagePlus(ds);
		//}
	}
	
	/** returns the ImageTranslator used by the LegacyImageMap */
	public ImageTranslator getTranslator() {
		//synchronized(lock) {
			return imageTranslator;
		//}
	}

	/**
	 * Ensures that the given dataset has a corresponding legacy image.
	 *
	 * @return the {@link ImagePlus} object shadowing this dataset.
	 */
	public ImagePlus registerDataset(Dataset dataset) {
		//synchronized(lock) {
			// find image window
			ImagePlus imp = getParallelImagePlus(dataset);
			if (imp == null) {
				// mirror dataset to image window
				imp = imageTranslator.createLegacyImage(dataset);
				imageTable.put(imp, dataset);
			}
			return imp;
		//}
	}
	
	/**
	 * Ensures that the given legacy image has a corresponding dataset.
	 *
	 * @return the {@link Dataset} object shadowing this legacy image.
	 */
	public Dataset registerLegacyImage(ImagePlus imp) {
		//synchronized(lock) {
			Dataset dataset = imageTable.get(imp);
			if (dataset == null) {
				// mirror image window to dataset
				dataset = imageTranslator.createDataset(imp);
				imageTable.put(imp, dataset);
			}
			else {  // dataset was already existing
				// do nothing
			}
			return dataset;
		//}
	}

	/** removes the mapping associated with a given Dataset */
	public void unregisterDataset(Dataset ds) {
		//synchronized(lock) {
		  ImagePlus imp = getParallelImagePlus(ds);
		  if (imp != null) {
				imageTable.remove(imp);
				LegacyUtils.removeImagePlusFromIJ1(imp);
		  }
		//}
	}
	
	// TODO - hatched as maybe useful
	/** removes the mapping associated with a given ImagePlus */
	public void unregisterLegacyImage(ImagePlus imp) {
		//synchronized(lock) {
			imageTable.remove(imp);
			LegacyUtils.removeImagePlusFromIJ1(imp);
		//}
	}

	// -- helpers --

	private ImagePlus getParallelImagePlus(Dataset ds) {
		ImagePlus imp = null;
		for (final ImagePlus key : imageTable.keySet()) {
			final Dataset value = imageTable.get(key);
			if (ds == value) {
				imp = key;
				break;
			}
		}
		return imp;
	}
	
	private void subscribeToEvents() {
		final EventSubscriber<DatasetDeletedEvent> deletionSubscriber =
			new EventSubscriber<DatasetDeletedEvent>() {

				@Override
				public void onEvent(DatasetDeletedEvent event) {
					unregisterDataset(event.getObject());
				}
			};
		subscribers.add(deletionSubscriber);
		Events.subscribe(DatasetDeletedEvent.class, deletionSubscriber);
	}
}

@@ -1925,15 +1925,805 @@
 %0A * 
-TODO%0A *
+An image map between IJ1 %7B@link ImagePlus%7D objects and IJ2 %7B@link Display%7Ds.%0A * Because every %7B@link ImagePlus%7D has a corresponding %7B@link ImageWindow%7D and%0A * vice versa, it works out best to associate each %7B@link ImagePlus%7D with a%0A * %7B@link Display%7D rather than with a %7B@link Dataset%7D.%0A * %3Cp%3E%0A * Any %7B@link Overlay%7Ds present in the %7B@link Display%7D are translated to a%0A * %7B@link Roi%7D attached to the %7B@link ImagePlus%7D, and vice versa.%0A * %3C/p%3E%0A * %3Cp%3E%0A * In the case of one %7B@link Dataset%7D belonging to multiple %7B@link Display%7Ds,%0A * there is a separate %7B@link ImagePlus%7D for each %7B@link Display%7D, with pixels%0A * by reference.%0A * %3C/p%3E%0A * %3Cp%3E%0A * In the case of multiple %7B@link Dataset%7Ds in a single %7B@link Display%7D, only%0A * the first %7B@link Dataset%7D is translated to the %7B@link ImagePlus%7D.%0A * %3C/p%3E%0A * 
 %0A * 

Add javadoc for LegacyImageMap.