Changeset 4833


Ignore:
Timestamp:
02/20/09 14:44:19 (11 years ago)
Author:
curtis
Message:

Show available output formats in the Bio-Formats Exporter file filter.
Unfortunately, this change necessitates the use of JFileChooser
regardless of ImageJ's "Use JFileChooser for Open/Save" setting.
This update also fixes the Bio-Formats Exporter when recorded as a macro.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/loci-plugins/src/loci/plugins/Exporter.java

    r4820 r4833  
    2626package loci.plugins; 
    2727 
    28 import ij.*; 
     28import ij.IJ; 
     29import ij.ImagePlus; 
     30import ij.ImageStack; 
     31import ij.Macro; 
    2932import ij.gui.GenericDialog; 
    3033import ij.io.FileInfo; 
     34import ij.io.OpenDialog; 
    3135import ij.io.SaveDialog; 
     36import ij.plugin.frame.Recorder; 
    3237import ij.process.*; 
    3338import java.awt.Image; 
    3439import java.awt.image.BufferedImage; 
    35 import java.io.*; 
     40import java.io.ByteArrayOutputStream; 
     41import java.io.File; 
     42import java.io.IOException; 
     43import java.io.PrintStream; 
     44import javax.swing.JFileChooser; 
     45import javax.swing.JOptionPane; 
     46import javax.swing.filechooser.FileFilter; 
    3647import loci.formats.*; 
     48import loci.formats.gui.ExtensionFileFilter; 
     49import loci.formats.gui.GUITools; 
    3750import loci.formats.meta.MetadataRetrieve; 
    3851import loci.formats.meta.MetadataStore; 
    3952 
    4053/** 
    41  * Core logic for the LOCI Exporter ImageJ plugin. 
     54 * Core logic for the Bio-Formats Exporter ImageJ plugin. 
    4255 * 
    4356 * <dl><dt><b>Source code:</b></dt> 
     
    8497    if (outfile == null || outfile.length() == 0) { 
    8598      // open a dialog prompting for the filename to save 
    86       SaveDialog sd = new SaveDialog("LOCI Bio-Formats Exporter", "", ""); 
    87       String dir = sd.getDirectory(); 
    88       String name = sd.getFileName(); 
     99 
     100      //SaveDialog sd = new SaveDialog("Bio-Formats Exporter", "", ""); 
     101      //String dir = sd.getDirectory(); 
     102      //String name = sd.getFileName(); 
     103 
     104      // NB: Copied and adapted from ij.io.SaveDIalog.jSaveDispatchThread, 
     105      // so that the save dialog has a file filter for choosing output format. 
     106 
     107      String dir = null, name = null; 
     108      JFileChooser fc = GUITools.buildFileChooser(new ImageWriter(), false); 
     109      fc.setDialogTitle("Bio-Formats Exporter"); 
     110      String defaultDir = OpenDialog.getDefaultDirectory(); 
     111      if (defaultDir != null) fc.setCurrentDirectory(new File(defaultDir)); 
     112 
     113      // set OME-TIFF as the default output format 
     114      FileFilter[] ff = fc.getChoosableFileFilters(); 
     115      FileFilter defaultFilter = null; 
     116      for (int i=0; i<ff.length; i++) { 
     117        if (ff[i] instanceof ExtensionFileFilter) { 
     118          ExtensionFileFilter eff = (ExtensionFileFilter) ff[i]; 
     119          if (i == 0 || eff.getExtension().equals("ome.tif")) { 
     120            defaultFilter = eff; 
     121            break; 
     122          } 
     123        } 
     124      } 
     125      if (defaultFilter != null) fc.setFileFilter(defaultFilter); 
     126 
     127      int returnVal = fc.showSaveDialog(IJ.getInstance()); 
     128      if (returnVal != JFileChooser.APPROVE_OPTION) { 
     129        Macro.abort(); 
     130        return; 
     131      } 
     132      File f = fc.getSelectedFile(); 
     133      if (f.exists()) { 
     134        int ret = JOptionPane.showConfirmDialog(fc, 
     135          "The file " + f.getName() + " already exists. \n" + 
     136          "Would you like to replace it?", "Replace?", 
     137          JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 
     138        if (ret != JOptionPane.OK_OPTION) f = null; 
     139      } 
     140      if (f == null) Macro.abort(); 
     141      else { 
     142        dir = fc.getCurrentDirectory().getPath() + File.separator; 
     143        name = fc.getName(f); 
     144 
     145        // ensure filename matches selected filter 
     146        FileFilter filter = fc.getFileFilter(); 
     147        if (filter instanceof ExtensionFileFilter) { 
     148          ExtensionFileFilter eff = (ExtensionFileFilter) filter; 
     149          String[] ext = eff.getExtensions(); 
     150          String lName = name.toLowerCase(); 
     151          boolean hasExtension = false; 
     152          for (int i=0; i<ext.length; i++) { 
     153            if (lName.endsWith("." + ext[i])) { 
     154              hasExtension = true; 
     155              break; 
     156            } 
     157          } 
     158          if (!hasExtension && ext.length > 0) { 
     159            // append chosen extension 
     160            name = name + "." + ext[0]; 
     161          } 
     162        } 
     163 
     164        // do some ImageJ bookkeeping 
     165        OpenDialog.setDefaultDirectory(dir); 
     166        if (Recorder.record) Recorder.recordPath("save", dir+name); 
     167      } 
     168 
    89169      if (dir == null || name == null) return; 
    90170      outfile = new File(dir, name).getAbsolutePath(); 
     
    98178        fi.description.indexOf("xml") == -1 ? null : fi.description; 
    99179      MetadataStore store = MetadataTools.createOMEXMLMetadata(xml); 
    100       if (store == null) IJ.error("OME-Java library not found."); 
     180      if (store == null) IJ.error("OME-XML Java library not found."); 
    101181      else if (store instanceof MetadataRetrieve) { 
    102182        if (xml == null) { 
     
    172252      if (codecs != null && codecs.length > 1) { 
    173253        GenericDialog gd = 
    174           new GenericDialog("LOCI Bio-Formats Exporter Options"); 
     254          new GenericDialog("Bio-Formats Exporter Options"); 
    175255        if (codecs != null) { 
    176256          gd.addChoice("Compression type: ", codecs, codecs[0]); 
Note: See TracChangeset for help on using the changeset viewer.