Changeset 2538


Ignore:
Timestamp:
04/03/07 17:02:23 (13 years ago)
Author:
curtis
Message:
  • Move JFileChooser and FileFilter logic (Swing stuff) into separate loci.formats.gui package.
    • Code that used to call loci.formats.IFormatHandler.getFileChooser should call loci.formats.gui.GUITools.buildFileChooser(IFormatHandler) instead.
    • Code that used to call loci.formats.IFormatHandler.getFileFilters should call loci.formats.gui.GUITools.buildFileFilters(IFormatHandler) instead.
  • Move ImageTester test class into loci.formats.test package.
  • Remove ImageJReader (not really necessary, and too hard to maintain).
  • Tweak MinMaxCalculator to have the correct constructors.
Location:
trunk/loci/formats
Files:
2 added
1 deleted
9 edited
5 moved

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/FileStitcher.java

    r2535 r2538  
    2929import java.lang.reflect.InvocationTargetException; 
    3030import java.util.*; 
    31 import javax.swing.JFileChooser; 
    32 import javax.swing.filechooser.FileFilter; 
    3331 
    3432/** 
     
    133131 
    134132  // -- FileStitcher API methods -- 
     133 
     134  /** Gets the wrapped reader prototype. */ 
     135  public IFormatReader getReader() { return reader; } 
    135136 
    136137  /** 
     
    644645  public String[] getSuffixes() { 
    645646    return reader.getSuffixes(); 
    646   } 
    647  
    648   /* @see IFormatHandler#getFileFilters() */ 
    649   public FileFilter[] getFileFilters() { 
    650     return reader.getFileFilters(); 
    651   } 
    652  
    653   /* @see IFormatHandler#getFileChooser() */ 
    654   public JFileChooser getFileChooser() { 
    655     return reader.getFileChooser(); 
    656647  } 
    657648 
  • trunk/loci/formats/FormatHandler.java

    r2455 r2538  
    2626 
    2727import java.util.Vector; 
    28 import javax.swing.JFileChooser; 
    29 import javax.swing.filechooser.FileFilter; 
    3028 
    3129/** Abstract superclass of all biological file format readers and writers. */ 
     
    3937  /** Valid suffixes for this file format. */ 
    4038  protected String[] suffixes; 
    41  
    42   /** File filters for this file format, for use with a JFileChooser. */ 
    43   protected FileFilter[] filters; 
    44  
    45   /** File chooser for this file format. */ 
    46   protected JFileChooser chooser; 
    4739 
    4840  /** List of status listeners. */ 
     
    114106  public String[] getSuffixes() { return suffixes; } 
    115107 
    116   /* @see IFormatHandler#getFileFilters() */ 
    117   public FileFilter[] getFileFilters() { 
    118     if (filters == null) { 
    119       filters = new FileFilter[] {new ExtensionFileFilter(suffixes, format)}; 
    120     } 
    121     return filters; 
    122   } 
    123  
    124   /* @see IFormatHandler#getFileChooser() */ 
    125   public JFileChooser getFileChooser() { 
    126     if (chooser == null) { 
    127       chooser = FormatTools.buildFileChooser(getFileFilters()); 
    128     } 
    129     return chooser; 
    130   } 
    131  
    132108  // -- StatusReporter API methods -- 
    133109 
  • trunk/loci/formats/FormatReader.java

    r2535 r2538  
    2828import java.io.IOException; 
    2929import java.util.*; 
    30 import javax.swing.filechooser.FileFilter; 
    3130 
    3231/** Abstract superclass of all biological file format readers. */ 
     
    537536  } 
    538537 
    539   // -- IFormatHandler API methods -- 
    540  
    541   /* @see IFormatHandler#getFileFilters() */ 
    542   public FileFilter[] getFileFilters() { 
    543     if (filters == null) { 
    544       filters = new FileFilter[] {new FormatFileFilter(this)}; 
    545     } 
    546     return filters; 
    547   } 
    548  
    549538  // -- Utility methods -- 
    550539 
  • trunk/loci/formats/FormatTools.java

    r2523 r2538  
    2828import java.awt.image.BufferedImage; 
    2929import java.io.IOException; 
    30 import java.lang.reflect.InvocationTargetException; 
    3130import java.lang.reflect.Method; 
    3231import java.util.Arrays; 
    3332import java.util.Hashtable; 
    34 import javax.swing.JFileChooser; 
    35 import javax.swing.SwingUtilities; 
    36 import javax.swing.filechooser.FileFilter; 
    3733 
    3834/** A utility class for format reader and writer implementations. */ 
     
    120116    boolean doMeta = true; 
    121117    boolean thumbs = false; 
     118    boolean minmax = false; 
    122119    boolean merge = false; 
    123120    boolean stitch = false; 
     
    137134          else if (args[i].equals("-nometa")) doMeta = false; 
    138135          else if (args[i].equals("-thumbs")) thumbs = true; 
     136          else if (args[i].equals("-minmax")) minmax = true; 
    139137          else if (args[i].equals("-merge")) merge = true; 
    140138          else if (args[i].equals("-stitch")) stitch = true; 
     
    181179      String[] s = { 
    182180        "To test read a file in " + format + " format, run:", 
    183         "  java " + className + " [-nopix] [-nometa] [-thumbs] [-merge]", 
    184         "    [-stitch] [-separate] [-nocolors] [-omexml] [-normalize] [-fast]", 
    185         "    [-debug] [-range start end] [-series num] [-map id] file", 
     181        "  java " + className + " [-nopix] [-nometa] [-thumbs] [-minmax]", 
     182        "    [-merge] [-stitch] [-separate] [-nocolors] [-omexml] [-normalize]", 
     183        "    [-fast] [-debug] [-range start end] [-series num] [-map id] file", 
    186184        "", 
    187185        "      file: the image file to read", 
     
    189187        "   -nometa: output only core metadata", 
    190188        "   -thumbs: read thumbnails instead of normal pixels", 
     189        "   -minmax: compute min/max statistics", 
    191190        "    -merge: combine separate channels into RGB image", 
    192191        "   -stitch: stitch files with similar names", 
     
    239238    if (separate) reader = new ChannelSeparator(reader); 
    240239    if (merge) reader = new ChannelMerger(reader); 
     240    MinMaxCalculator minMaxCalc = null; 
     241    if (minmax) reader = minMaxCalc = new MinMaxCalculator(reader); 
    241242 
    242243    System.out.println("Initializing reader"); 
     
    378379    String s = seriesCount > 1 ? (" series #" + series) : ""; 
    379380    int pixelType = reader.getPixelType(id); 
     381    int sizeC = reader.getSizeC(id); 
     382 
     383    // get a priori min/max values 
     384    Double[] preGlobalMin = null, preGlobalMax = null; 
     385    Double[] preKnownMin = null, preKnownMax = null; 
     386    Double[] prePlaneMin = null, prePlaneMax = null; 
     387    boolean preIsMinMaxPop = false; 
     388    if (minmax) { 
     389      preGlobalMin = new Double[sizeC]; 
     390      preGlobalMax = new Double[sizeC]; 
     391      preKnownMin = new Double[sizeC]; 
     392      preKnownMax = new Double[sizeC]; 
     393      for (int c=0; c<sizeC; c++) { 
     394        preGlobalMin[c] = minMaxCalc.getChannelGlobalMinimum(id, c); 
     395        preGlobalMax[c] = minMaxCalc.getChannelGlobalMaximum(id, c); 
     396        preKnownMin[c] = minMaxCalc.getChannelKnownMinimum(id, c); 
     397        preKnownMax[c] = minMaxCalc.getChannelKnownMaximum(id, c); 
     398      } 
     399      prePlaneMin = minMaxCalc.getPlaneMinimum(id, 0); 
     400      prePlaneMax = minMaxCalc.getPlaneMaximum(id, 0); 
     401      preIsMinMaxPop = minMaxCalc.isMinMaxPopulated(id); 
     402    } 
    380403 
    381404    // read pixels 
     
    441464        avg + "ms per image, " + initial + "ms overhead)"); 
    442465 
     466      if (minmax) { 
     467        // get computed min/max values 
     468        Double[] globalMin = new Double[sizeC]; 
     469        Double[] globalMax = new Double[sizeC]; 
     470        Double[] knownMin = new Double[sizeC]; 
     471        Double[] knownMax = new Double[sizeC]; 
     472        for (int c=0; c<sizeC; c++) { 
     473          globalMin[c] = minMaxCalc.getChannelGlobalMinimum(id, c); 
     474          globalMax[c] = minMaxCalc.getChannelGlobalMaximum(id, c); 
     475          knownMin[c] = minMaxCalc.getChannelKnownMinimum(id, c); 
     476          knownMax[c] = minMaxCalc.getChannelKnownMaximum(id, c); 
     477        } 
     478        Double[] planeMin = minMaxCalc.getPlaneMinimum(id, 0); 
     479        Double[] planeMax = minMaxCalc.getPlaneMaximum(id, 0); 
     480        boolean isMinMaxPop = minMaxCalc.isMinMaxPopulated(id); 
     481 
     482        // output min/max results 
     483        System.out.println(); 
     484        System.out.println("Min/max values (final / tentative):"); 
     485        for (int c=0; c<sizeC; c++) { 
     486          System.out.println("\tChannel " + c + ":"); 
     487          System.out.println("\t\tGlobal minimum = " + 
     488            globalMin[c] + " / " + preGlobalMin[c]); 
     489          System.out.println("\t\tGlobal maximum = " + 
     490            globalMax[c] + " / " + preGlobalMax[c]); 
     491          System.out.println("\t\tKnown minimum = " + 
     492            knownMin[c] + " / " + preKnownMin[c]); 
     493          System.out.println("\t\tKnown maximum = " + 
     494            knownMax[c] + " / " + preKnownMax[c]); 
     495        } 
     496        System.out.print("\tFirst plane minimum(s) ="); 
     497        if (planeMin == null) System.out.print(" none"); 
     498        else { 
     499          for (int subC=0; subC<planeMin.length; subC++) { 
     500            System.out.print(" " + planeMin[subC]); 
     501          } 
     502        } 
     503        System.out.print(" /"); 
     504        if (prePlaneMin == null) System.out.print(" none"); 
     505        else { 
     506          for (int subC=0; subC<prePlaneMin.length; subC++) { 
     507            System.out.print(" " + prePlaneMin[subC]); 
     508          } 
     509        } 
     510        System.out.println(); 
     511        System.out.print("\tFirst plane maximum(s) ="); 
     512        if (planeMax == null) System.out.print(" none"); 
     513        else { 
     514          for (int subC=0; subC<planeMax.length; subC++) { 
     515            System.out.print(" " + planeMax[subC]); 
     516          } 
     517        } 
     518        System.out.print(" /"); 
     519        if (prePlaneMax == null) System.out.print(" none"); 
     520        else { 
     521          for (int subC=0; subC<prePlaneMax.length; subC++) { 
     522            System.out.print(" " + prePlaneMax[subC]); 
     523          } 
     524        } 
     525        System.out.println(); 
     526        System.out.println("\tMin/max populated = " + 
     527          isMinMaxPop + " / " + preIsMinMaxPop); 
     528      } 
     529 
    443530      // display pixels in image viewer 
    444       ImageViewer viewer = new ImageViewer(); 
    445       viewer.setImages(id, reader, images); 
    446       viewer.setVisible(true); 
     531      // avoid dependencies on optional loci.formats.gui package 
     532//      ImageViewer viewer = new ImageViewer(); 
     533//      viewer.setImages(id, reader, images); 
     534//      viewer.setVisible(true); 
     535//      CTR TODO remove the above three commented out lines 
     536      ReflectedUniverse r = new ReflectedUniverse(); 
     537      try { 
     538        r.exec("import loci.formats.gui.ImageViewer"); 
     539        r.exec("viewer = new ImageViewer()"); 
     540        r.setVar("id", id); 
     541        r.setVar("reader", reader); 
     542        r.setVar("images", images); 
     543        r.setVar("true", true); 
     544        r.exec("viewer.setImages(id, reader, images)"); 
     545        r.exec("viewer.setVisible(true)"); 
     546      } 
     547      catch (ReflectException exc) { 
     548        throw new FormatException(exc); 
     549      } 
    447550    } 
    448551 
     
    776879  } 
    777880 
    778   // -- Utility methods - GUI -- 
    779  
    780   /** 
    781    * Builds a file chooser with the given file filters, 
    782    * as well as an "All supported file types" combo filter. 
    783    */ 
    784   public static JFileChooser buildFileChooser(final FileFilter[] filters) { 
    785     // NB: must construct JFileChooser in the 
    786     // AWT worker thread, to avoid deadlocks 
    787     final JFileChooser[] jfc = new JFileChooser[1]; 
    788     Runnable r = new Runnable() { 
    789       public void run() { 
    790         JFileChooser fc = new JFileChooser(System.getProperty("user.dir")); 
    791         FileFilter[] ff = ComboFileFilter.sortFilters(filters); 
    792         FileFilter combo = null; 
    793         if (ff.length > 1) { 
    794           // By default, some readers might need to open a file to determine 
    795           // if it is the proper type, when the extension alone isn't enough 
    796           // to distinguish. 
    797           // 
    798           // We want to disable that behavior for the "All supported file 
    799           // types" combination filter, because otherwise it is too slow. 
    800           // 
    801           // Also, most of the formats that do this are TIFF-based, and the 
    802           // TIFF reader will already green-light anything with .tif 
    803           // extension, making more thorough checks redundant. 
    804           combo = new ComboFileFilter(ff, "All supported file types", false); 
    805           fc.addChoosableFileFilter(combo); 
    806         } 
    807         for (int i=0; i<ff.length; i++) fc.addChoosableFileFilter(ff[i]); 
    808         if (combo != null) fc.setFileFilter(combo); 
    809         jfc[0] = fc; 
    810       } 
    811     }; 
    812     if (Thread.currentThread().getName().startsWith("AWT-EventQueue")) { 
    813       // current thread is the AWT event queue thread; just execute the code 
    814       r.run(); 
    815     } 
    816     else { 
    817       // execute the code with the AWT event thread 
    818       try { 
    819         SwingUtilities.invokeAndWait(r); 
    820       } 
    821       catch (InterruptedException exc) { return null; } 
    822       catch (InvocationTargetException exc) { return null; } 
    823     } 
    824     return jfc[0]; 
    825   } 
    826  
    827881} 
  • trunk/loci/formats/IFormatHandler.java

    r2454 r2538  
    2525package loci.formats; 
    2626 
    27 import javax.swing.JFileChooser; 
    28 import javax.swing.filechooser.FileFilter; 
    29  
    3027/** Interface for all biological file format readers and writers. */ 
    3128public interface IFormatHandler extends StatusReporter { 
     
    4744  String[] getSuffixes(); 
    4845 
    49   /** Gets file filters for this file format, for use with a JFileChooser. */ 
    50   FileFilter[] getFileFilters(); 
    51  
    52   /** Gets a JFileChooser that recognizes accepted file types. */ 
    53   JFileChooser getFileChooser(); 
    54  
    5546} 
  • trunk/loci/formats/ImageReader.java

    r2535 r2538  
    2727import java.awt.image.BufferedImage; 
    2828import java.io.BufferedReader; 
    29 import java.io.File; 
    3029import java.io.IOException; 
    3130import java.io.InputStreamReader; 
    3231import java.util.*; 
    33 import javax.swing.JFileChooser; 
    34 import javax.swing.filechooser.FileFilter; 
    3532 
    3633/** 
     
    10097   */ 
    10198  private String[] suffixes; 
    102  
    103   /** 
    104    * File filters for this file format, for use with a JFileChooser. 
    105    * Populated the first time getFileFilters() is called. 
    106    */ 
    107   protected FileFilter[] filters; 
    108  
    109   /** 
    110    * File chooser for this file format. 
    111    * Created the first time getFileChooser() is called. 
    112    */ 
    113   protected JFileChooser chooser; 
    11499 
    115100  /** Name of current file. */ 
     
    187172  } 
    188173 
     174  /** Gets all constituent file format readers. */ 
     175  public IFormatReader[] getReaders() { 
     176    IFormatReader[] r = new IFormatReader[readers.length]; 
     177    System.arraycopy(readers, 0, r, 0, readers.length); 
     178    return r; 
     179  } 
     180 
    189181  // -- IFormatReader API methods -- 
    190182 
     
    466458  /* @see IFormatReader#testRead(String[]) */ 
    467459  public boolean testRead(String[] args) throws FormatException, IOException { 
    468     if (args.length == 0) { 
    469       JFileChooser box = getFileChooser(); 
    470       int rval = box.showOpenDialog(null); 
    471       if (rval == JFileChooser.APPROVE_OPTION) { 
    472         File file = box.getSelectedFile(); 
    473         if (file != null) args = new String[] {file.getPath()}; 
    474       } 
    475     } 
    476460    return FormatTools.testRead(this, args); 
    477461  } 
     
    511495    } 
    512496    return suffixes; 
    513   } 
    514  
    515   /* @see IFormatHandler#getFileFilters() */ 
    516   public FileFilter[] getFileFilters() { 
    517     if (filters == null) { 
    518       Vector v = new Vector(); 
    519       for (int i=0; i<readers.length; i++) { 
    520         FileFilter[] ff = readers[i].getFileFilters(); 
    521         for (int j=0; j<ff.length; j++) v.add(ff[j]); 
    522       } 
    523       filters = ComboFileFilter.sortFilters(v); 
    524     } 
    525     return filters; 
    526   } 
    527  
    528   /* @see IFormatHandler#getFileChooser() */ 
    529   public JFileChooser getFileChooser() { 
    530     if (chooser == null) { 
    531       chooser = FormatTools.buildFileChooser(getFileFilters()); 
    532     } 
    533     return chooser; 
    534497  } 
    535498 
  • trunk/loci/formats/ImageWriter.java

    r2454 r2538  
    3131import java.io.InputStreamReader; 
    3232import java.util.*; 
    33 import javax.swing.JFileChooser; 
    34 import javax.swing.filechooser.FileFilter; 
    3533 
    3634/** 
     
    10098   */ 
    10199  private String[] suffixes; 
    102  
    103   /** 
    104    * File filters for all file format writers, for use with a JFileChooser. 
    105    * Populated the first time getFileFilters() is called. 
    106    */ 
    107   protected FileFilter[] filters; 
    108  
    109   /** 
    110    * File chooser for all file format writers. 
    111    * Created the first time getFileChooser() is called. 
    112    */ 
    113   protected JFileChooser chooser; 
    114100 
    115101  /** 
     
    183169  } 
    184170 
     171  /** Gets all constituent file format writers. */ 
     172  public IFormatWriter[] getWriters() { 
     173    IFormatWriter[] w = new IFormatWriter[writers.length]; 
     174    System.arraycopy(writers, 0, w, 0, writers.length); 
     175    return w; 
     176  } 
     177 
    185178  // -- IFormatWriter API methods -- 
    186179 
     
    317310  } 
    318311 
    319   /* @see IFormatHandler#getFileFilters() */ 
    320   public FileFilter[] getFileFilters() { 
    321     if (filters == null) { 
    322       Vector v = new Vector(); 
    323       for (int i=0; i<writers.length; i++) { 
    324         FileFilter[] ff = writers[i].getFileFilters(); 
    325         for (int j=0; j<ff.length; j++) v.add(ff[j]); 
    326       } 
    327       filters = ComboFileFilter.sortFilters(v); 
    328     } 
    329     return filters; 
    330   } 
    331  
    332   /* @see IFormatHandler#getFileChooser() */ 
    333   public JFileChooser getFileChooser() { 
    334     if (chooser == null) { 
    335       chooser = FormatTools.buildFileChooser(getFileFilters()); 
    336     } 
    337     return chooser; 
    338   } 
    339  
    340312  // -- StatusReporter API methods -- 
    341313 
  • trunk/loci/formats/MinMaxCalculator.java

    r2537 r2538  
    5151  // -- Constructors -- 
    5252 
     53  /** Constructs a MinMaxCalculator around a new image reader. */ 
     54  public MinMaxCalculator() { super(); } 
     55 
     56  /** Constructs a MinMaxCalculator with the given reader. */ 
     57  public MinMaxCalculator(IFormatReader r) { super(r); } 
    5358 
    5459  // -- IFormatReader API methods --  
  • trunk/loci/formats/ReaderWrapper.java

    r2535 r2538  
    2828import java.io.*; 
    2929import java.util.Hashtable; 
    30 import javax.swing.JFileChooser; 
    31 import javax.swing.filechooser.FileFilter; 
    3230 
    3331/** 
     
    292290  } 
    293291 
    294   public FileFilter[] getFileFilters() { 
    295     return reader.getFileFilters(); 
    296   } 
    297  
    298   public JFileChooser getFileChooser() { 
    299     return reader.getFileChooser(); 
    300   } 
    301  
    302292  // -- StatusReporter API methods -- 
    303293 
  • trunk/loci/formats/gui/ComboFileFilter.java

    r2178 r2538  
    2323*/ 
    2424 
    25 package loci.formats; 
     25package loci.formats.gui; 
    2626 
    2727import java.io.File; 
  • trunk/loci/formats/gui/ExtensionFileFilter.java

    r2178 r2538  
    2323*/ 
    2424 
    25 package loci.formats; 
     25package loci.formats.gui; 
    2626 
    2727import java.io.File; 
  • trunk/loci/formats/gui/FormatFileFilter.java

    r2178 r2538  
    2323*/ 
    2424 
    25 package loci.formats; 
     25package loci.formats.gui; 
    2626 
    2727import java.io.File; 
    2828import javax.swing.filechooser.FileFilter; 
     29import loci.formats.IFormatReader; 
    2930 
    3031/** A file filter for a biological file format, for use with a JFileChooser. */ 
     
    3637 
    3738  /** Associated file format reader. */ 
    38   private FormatReader reader; 
     39  private IFormatReader reader; 
    3940 
    4041  /** Description. */ 
     
    4445 
    4546  /** Constructs a new filter that accepts the given extension. */ 
    46   public FormatFileFilter(FormatReader reader) { 
     47  public FormatFileFilter(IFormatReader reader) { 
    4748    this.reader = reader; 
    4849    StringBuffer sb = new StringBuffer(reader.getFormat()); 
  • trunk/loci/formats/gui/ImageViewer.java

    r2520 r2538  
    2323*/ 
    2424 
    25 package loci.formats; 
     25package loci.formats.gui; 
    2626 
    2727import java.awt.*; 
     
    3434import javax.swing.event.ChangeEvent; 
    3535import javax.swing.event.ChangeListener; 
     36import loci.formats.*; 
    3637 
    3738/** 
     
    338339    if ("open".equals(cmd)) { 
    339340      wait(true); 
    340       JFileChooser chooser = myReader.getFileChooser(); 
     341      JFileChooser chooser = GUITools.buildFileChooser(myReader); 
    341342      wait(false); 
    342343      int rval = chooser.showOpenDialog(this); 
     
    352353    else if ("save".equals(cmd)) { 
    353354      wait(true); 
    354       JFileChooser chooser = myWriter.getFileChooser(); 
     355      JFileChooser chooser = GUITools.buildFileChooser(myWriter); 
    355356      wait(false); 
    356357      int rval = chooser.showSaveDialog(this); 
Note: See TracChangeset for help on using the changeset viewer.