/*
 * #%L
 * ImageJ2 software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2022 ImageJ2 developers.
 * %%
 * 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.
 * #L%
 */

package net.imagej.legacy.convert;

import ij.ImagePlus;
import net.imagej.Dataset;
import net.imglib2.util.Cast;
import org.scijava.convert.AbstractConverter;
import org.scijava.convert.ConvertService;
import org.scijava.convert.Converter;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.util.Types;

import java.lang.reflect.Type;

/**
 * Converts a string that matches an {@link ImagePlus} title or ID to {@link Dataset}.
 * (The {@link ImagePlus} must be known to the {@link ij.WindowManager}. This
 * means it must be visible)
 * <p>
 * Similar to {@link StringToImagePlusConverter} but output type is {@link Dataset}.
 *
 * @author Matthias Arzt
 */
@Plugin(type = Converter.class)
public class StringToDatasetConverter
	extends AbstractConverter<String, Dataset>
{

	@Parameter
	ConvertService cs;

	private final Converter<String, ImagePlus> toImagePlus =
		new StringToImagePlusConverter();

	@Override
	public boolean canConvert(Object src, Type dest)
	{
		return canConvert(src, Types.raw(dest));
	}

	@Override
	public boolean canConvert(Object src, Class<?> dest)
	{
		return super.canConvert(src, dest) &&
			toImagePlus.canConvert(src, ImagePlus.class);
	}

	@Override
	public <T> T convert(Object src, Class<T> dest)
	{
		if (!(src instanceof String) || !Dataset.class.equals(dest))
			return null;
		ImagePlus imp = toImagePlus.convert(src, ImagePlus.class);
		if (imp == null) return null;
		return Cast.unchecked(cs.convert(imp, Dataset.class));
	}

	@Override
	public Class<String> getInputType()
	{
		return String.class;
	}

	@Override
	public Class<Dataset> getOutputType()
	{
		return Dataset.class;
	}
}

@@ -1498,78 +1498,32 @@
 ort 
-ij.ImagePlus;%0Aimport net.imagej.Dataset;%0Aimport net.imglib2.util.Cast;
+java.lang.reflect.Type;%0A
 %0Aimp
@@ -1546,32 +1546,40 @@
 convert.Abstract
+Delegate
 Converter;%0Aimpor
@@ -1729,69 +1729,55 @@
 in;%0A
+%0A
 import 
-org.scijava.util.Type
+ij.ImagePlu
 s;%0A
-%0A
 import 
-java.lang.reflect.Type
+net.imagej.Dataset
 ;%0A%0A/
@@ -2184,16 +2184,24 @@
 Abstract
+Delegate
 Converte
@@ -2209,16 +2209,27 @@
 %3CString,
+ ImagePlus,
  Dataset
@@ -2232,17 +2232,16 @@
 aset%3E%0A%7B%0A
-%0A
 %09@Parame
@@ -2245,16 +2245,24 @@
 ameter%0A%09
+private 
 ConvertS
@@ -2269,17 +2269,29 @@
 ervice c
-s
+onvertService
 ;%0A%0A%09priv
@@ -2434,31 +2434,38 @@
 rc, 
-Type
+Class%3C?%3E
  dest)
-%0A%09
+ 
 %7B%0A%09%09
-return 
+if (!super.
 canC
@@ -2480,23 +2480,68 @@
 rc, 
-Types.raw(dest)
+dest)) return false;%0A%09%09return srcImageSupported((String) src
 );%0A%09
@@ -2597,35 +2597,28 @@
 rc, 
-Class%3C?%3E
+Type
  dest)
-%0A%09
+ 
 %7B%0A%09%09
-return 
+if (!
 supe
@@ -2644,206 +2644,118 @@
 est)
- &&%0A%09%09%09toImagePlus.canConvert(
+) return false;%0A%09%09return 
 src
-, 
 Image
-Plus.class);%0A%09%7D%0A%0A%09@Override%0A%09public %3CT%3E T convert(Object src, Class%3CT%3E dest)%0A%09%7B%0A%09%09if (!(src instanceof String) %7C%7C !Dataset.class.equals(dest))%0A%09%09%09return null;
+Supported((String) src);%0A%09%7D%0A%0A%09private boolean srcImageSupported(String src) %7B
 %0A%09%09I
@@ -2763,19 +2763,24 @@
 agePlus 
-imp
+srcImage
  = toIma
@@ -2803,111 +2803,105 @@
 rc, 
-ImagePlus.class);%0A%09%09if (imp == null) return null;%0A%09%09return Cast.unchecked(cs.convert(imp, Dataset.class
+getDelegateType());%0A%09%09return srcImage != null && convertService.supports(srcImage, getOutputType(
 ));%0A
@@ -3061,11 +3061,100 @@
 lass;%0A%09%7D
+%0A%0A%09@Override%0A%09protected Class%3CImagePlus%3E getDelegateType() %7B%0A%09%09return ImagePlus.class;%0A%09%7D
 %0A%7D%0A

refactor as Delegate