package edu.emory.cellbio.ijbat.dm;

import edu.emory.cellbio.ijbat.SlideSet;

import imagej.ImageJ;
import imagej.data.Dataset;
import imagej.io.IOService;

import net.imglib2.exception.IncompatibleTypeException;
import io.scif.io.img.ImgIOException;

import java.io.File;

/**
 *
 * @author Benjamin Nanes
 */
@LinkerInfo( underlying = "java.lang.String", processed = "imagej.data.Dataset", 
        typeCode = "Image2", name = "Image File (read only)")
public class Image2Linker extends Linker {
     
     private IOService ios;

     public Image2Linker(ImageJ context, SlideSet owner) {
          super(context, owner);
          ios = context.get(IOService.class);
     }
     
     public Image2Linker() {
          super();
     }

     @Override
     public Object process(Object underlying) {
          String path = String.valueOf(underlying);
          String wd = owner.getWorkingDirectory();
          wd = wd == null ? "" : wd;
          if(!(new File(path)).isAbsolute())
               path = wd + File.pathSeparator;
          Dataset d;
          try{ d = ios.loadDataset(path); }
          catch(ImgIOException e)
               { throw new IllegalArgumentException("Error loading img: " + e); }
          catch(IncompatibleTypeException e)
               { throw new IllegalArgumentException("Unsuported image format: " + e); }
          return d;
     }

     //@Override
     public Object initialize(Object underlying) {
          Dataset d;
          try {
               d = (Dataset)underlying;
          } catch(ClassCastException e) {
               throw new IllegalArgumentException("This is not an image!" + e);
          }
          String wd = owner.getWorkingDirectory();
          if(wd == null || wd.equals(""))
               throw new IllegalArgumentException("Cannot save images if working directory is not set");
          String path = "linked-images"
                  + File.separator
                  + String.valueOf(d.hashCode());
          //<<<<<<<<<<<<<<<<<< Figure out how to save images?
          return path;
     }

     @Override
     public Class<?> getProcessedClass(Object underlying) {
          return Dataset.class;
     }
     
}

@@ -274,17 +274,97 @@
 %0A%0A/**%0A *
+ Linker class for opening images (%7B@code Dataset%7Ds)%0A * from file references.%0A * 
 %0A
-
  * @auth
@@ -569,16 +569,33 @@
 s Linker
+%3CString, Dataset%3E
  %7B%0A     
@@ -858,21 +858,22 @@
  public 
-Objec
+Datase
 t proces
@@ -874,22 +874,22 @@
 process(
-Object
+String
  underly
@@ -1116,21 +1116,17 @@
 ile.
-pathS
+s
 eparator
 ;%0A  
@@ -1121,16 +1121,23 @@
 eparator
+ + path
 ;%0A      
@@ -1482,18 +1482,25 @@
    /
-/@Override
+** @depricated */
 %0A   
@@ -2207,17 +2207,23 @@
 c Class%3C
-?
+Dataset
 %3E getPro

Fix relative path resolution in Image2Linker