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

import imagej.ImageJ;
import imagej.data.Dataset;
import imagej.data.DatasetService;
import imagej.event.EventService;
import imagej.ext.module.ModuleService;
import imagej.io.event.FileOpenedEvent;
import imagej.service.AbstractService;
import imagej.service.Service;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.ImgPlus;
import net.imglib2.io.ImgIOException;
import net.imglib2.io.ImgOpener;

/**
 * Service that provides convenience methods for I/O.
 * 
 * @author Curtis Rueden
 */
@Service
public final class IOService extends AbstractService {

	private EventService eventService;
	private ModuleService moduleService;
	private DatasetService datasetService;

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

	public IOService(final ImageJ context, final EventService eventService,
		final ModuleService moduleService, final DatasetService datasetService)
	{
		super(context);
		this.eventService = eventService;
		this.moduleService = moduleService;
		this.datasetService = datasetService;
	}

	// -- IOService methods --

	public EventService getEventService() {
		return eventService;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public DatasetService getDatasetService() {
		return datasetService;
	}

	/** Loads a dataset from a source (such as a file on disk). */
	public Dataset loadDataset(final String source) throws ImgIOException,
		IncompatibleTypeException
	{
		if (source == null) return null;
		final ImgOpener imageOpener = new ImgOpener();
		imageOpener.addStatusListener(new StatusDispatcher(eventService));
		@SuppressWarnings({ "cast", "rawtypes" })
		final ImgPlus imgPlus = (ImgPlus) imageOpener.openImg(source);
		@SuppressWarnings("unchecked")
		final Dataset dataset = datasetService.create(imgPlus);
		eventService.publish(new FileOpenedEvent(source));
		return dataset;
	}

	/** Reverts the given dataset to its original source. */
	public void revertDataset(final Dataset dataset) throws ImgIOException,
		IncompatibleTypeException
	{
		final String source = dataset.getSource();
		if (source == null) return; // no way to revert
		final Dataset revertedDataset = loadDataset(source);
		revertedDataset.copyInto(dataset);
	}

	// TODO: Add a saveDataset method, and use it in SaveAsImage plugin.

}

@@ -2266,16 +2266,94 @@
 gOpener;
+%0Aimport net.imglib2.type.NativeType;%0Aimport net.imglib2.type.numeric.RealType;
 %0A%0A/**%0A *
@@ -2474,16 +2474,18 @@
 OService
+%3CT
  extends
@@ -2485,16 +2485,54 @@
 extends 
+RealType%3CT%3E & NativeType%3CT%3E%3E%0A%09extends 
 Abstract
@@ -2538,17 +2538,17 @@
 tService
- 
+%0A
 %7B%0A%0A%09priv
@@ -3624,52 +3624,8 @@
 ));%0A
-%09%09@SuppressWarnings(%7B %22cast%22, %22rawtypes%22 %7D)%0A
 %09%09fi
@@ -3635,16 +3635,19 @@
  ImgPlus
+%3CT%3E
  imgPlus
@@ -3652,18 +3652,8 @@
 us =
- (ImgPlus)
  ima
@@ -3682,41 +3682,8 @@
 e);%0A
-%09%09@SuppressWarnings(%22unchecked%22)%0A
 %09%09fi

Fix compile errors, really