Search in sources :

Example 26 with OpenDialog

use of ij.io.OpenDialog in project TrakEM2 by trakem2.

the class Loader method askForXMLTemplate.

/**
 * Ask for the user to provide a template XML file to extract a root TemplateThing.
 */
public TemplateThing askForXMLTemplate(final Project project) {
    // ask for an .xml file or a .dtd file
    // fd.setFilenameFilter(new XMLFileFilter(XMLFileFilter.BOTH));
    final OpenDialog od = new OpenDialog("Select XML Template", OpenDialog.getDefaultDirectory(), null);
    final String filename = od.getFileName();
    if (null == filename || filename.toLowerCase().startsWith("null"))
        return null;
    // if there is a path, read it out if possible
    String dir = od.getDirectory();
    if (null == dir)
        return null;
    if (IJ.isWindows())
        dir = dir.replace('\\', '/');
    if (!dir.endsWith("/"))
        dir += "/";
    TemplateThing[] roots;
    try {
        roots = DTDParser.extractTemplate(dir + filename);
    } catch (final Exception e) {
        IJError.print(e);
        return null;
    }
    if (null == roots || roots.length < 1)
        return null;
    if (roots.length > 1) {
        Utils.showMessage("Found more than one root.\nUsing first root only.");
    }
    return roots[0];
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing) IOException(java.io.IOException) FormatException(loci.formats.FormatException) OpenDialog(ij.io.OpenDialog)

Example 27 with OpenDialog

use of ij.io.OpenDialog in project TrakEM2 by trakem2.

the class FSLoader method openFSProject.

/**
 * Returns TMLHandler.getProjectData() . If the path is null it'll be asked for.
 */
public Object[] openFSProject(String path, final boolean open_displays) {
    // clean path of double-slashes, safely (and painfully)
    if (null != path) {
        path = path.replace('\\', '/');
        path = path.trim();
        int itwo = path.indexOf("//");
        while (-1 != itwo) {
            if (0 == itwo || /* samba disk */
            (5 == itwo && "http:".equals(path.substring(0, 5)))) {
            // do nothing
            } else {
                path = path.substring(0, itwo) + path.substring(itwo + 1);
            }
            itwo = path.indexOf("//", itwo + 1);
        }
    }
    // 
    if (null == path) {
        OpenDialog od = new OpenDialog("Select Project", OpenDialog.getDefaultDirectory(), null);
        String file = od.getFileName();
        if (null == file || file.toLowerCase().startsWith("null"))
            return null;
        String dir = od.getDirectory().replace('\\', '/');
        if (!dir.endsWith("/"))
            dir += "/";
        this.project_file_path = dir + file;
        Utils.log2("project file path 1: " + this.project_file_path);
    } else {
        this.project_file_path = path;
        Utils.log2("project file path 2: " + this.project_file_path);
    }
    Utils.log2("Loader.openFSProject: path is " + path);
    // check if any of the open projects uses the same file path, and refuse to open if so:
    if (null != FSLoader.getOpenProject(project_file_path, this)) {
        Utils.showMessage("The project is already open.");
        return null;
    }
    Object[] data = null;
    // parse file, according to expected format as indicated by the extension:
    final String lcFilePath = this.project_file_path.toLowerCase();
    if (lcFilePath.matches(".*(\\.xml|\\.xml\\.gz)")) {
        InputStream i_stream = null;
        TMLHandler handler = new TMLHandler(this.project_file_path, this);
        if (handler.isUnreadable()) {
            handler = null;
        } else {
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                factory.setValidating(false);
                factory.setXIncludeAware(false);
                SAXParser parser = factory.newSAXParser();
                if (isURL(this.project_file_path)) {
                    i_stream = new java.net.URL(this.project_file_path).openStream();
                } else {
                    i_stream = new BufferedInputStream(new FileInputStream(this.project_file_path));
                }
                if (lcFilePath.endsWith(".gz")) {
                    i_stream = new GZIPInputStream(i_stream);
                }
                InputSource input_source = new InputSource(i_stream);
                parser.parse(input_source, handler);
            } catch (java.io.FileNotFoundException fnfe) {
                Utils.log("ERROR: File not found: " + path);
                handler = null;
            } catch (Exception e) {
                IJError.print(e);
                handler = null;
            } finally {
                if (null != i_stream) {
                    try {
                        i_stream.close();
                    } catch (Exception e) {
                        IJError.print(e);
                    }
                }
            }
        }
        if (null == handler) {
            Utils.showMessage("Error when reading the project .xml file.");
            return null;
        }
        data = handler.getProjectData(open_displays);
    }
    if (null == data) {
        Utils.showMessage("Error when parsing the project .xml file.");
        return null;
    }
    // else, good
    crashDetector();
    return data;
}
Also used : InputSource(org.xml.sax.InputSource) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) OpenDialog(ij.io.OpenDialog) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 28 with OpenDialog

use of ij.io.OpenDialog in project GDSC-SMLM by aherbert.

the class CreateData method saveImage.

/**
 * Save the image to a TIFF file.
 *
 * @param imp the imp
 */
private void saveImage(ImagePlus imp) {
    if (!settings.getSaveImage()) {
        return;
    }
    final String[] path = ImageJUtils.decodePath(settings.getImageFilename());
    final OpenDialog chooser = new OpenDialog("Image_File", path[0], path[1]);
    if (chooser.getFileName() != null) {
        settings.setImageFilename(chooser.getDirectory() + chooser.getFileName());
        settings.setImageFilename(FileUtils.replaceExtension(settings.getImageFilename(), "tiff"));
        final FileSaver fs = new FileSaver(imp);
        boolean ok;
        if (imp.getStackSize() > 1) {
            ok = fs.saveAsTiffStack(settings.getImageFilename());
        } else {
            ok = fs.saveAsTiff(settings.getImageFilename());
        // The following call throws a NoSuchMethodError.
        // ok = IJ.saveAsTiff(imp, settings.imageFilename);
        }
        if (!ok) {
            IJ.log("Failed to save image to file: " + settings.getImageFilename());
        }
    }
}
Also used : FileSaver(ij.io.FileSaver) OpenDialog(ij.io.OpenDialog)

Example 29 with OpenDialog

use of ij.io.OpenDialog in project GDSC-SMLM by aherbert.

the class CreateData method saveImageResults.

private void saveImageResults(MemoryPeakResults results) {
    if (!settings.getSaveImageResults()) {
        return;
    }
    final String[] path = ImageJUtils.decodePath(settings.getImageResultsFilename());
    final OpenDialog chooser = new OpenDialog("Image_Results_File", path[0], path[1]);
    if (chooser.getFileName() != null) {
        settings.setImageResultsFilename(chooser.getDirectory() + chooser.getFileName());
        settings.setImageResultsFilename(FileUtils.replaceExtension(settings.getImageResultsFilename(), "xls"));
        final TextFilePeakResults r = new TextFilePeakResults(settings.getImageResultsFilename(), false);
        r.copySettings(results);
        r.begin();
        r.addAll(results.toArray());
        r.end();
    }
}
Also used : TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) OpenDialog(ij.io.OpenDialog)

Example 30 with OpenDialog

use of ij.io.OpenDialog in project GDSC-SMLM by aherbert.

the class CreateData method saveLocalisations.

/**
 * Save the localisations to a text file.
 *
 * @param localisations the localisations
 */
private void saveLocalisations(List<LocalisationModel> localisations) {
    if (!settings.getSaveLocalisations()) {
        return;
    }
    sortLocalisationsByTime(localisations);
    // Collections.sort(localisations, new Comparator<LocalisationModel>(){
    // 
    // public int compare(LocalisationModel o1, LocalisationModel o2)
    // {
    // int cellx1 = (int)(o1.getX() / settings.cellSize);
    // int cellx2 = (int)(o2.getX() / settings.cellSize);
    // int result = cellx2 - cellx1;
    // if (result != 0)
    // return result;
    // int celly1 = (int)(o1.getY() / settings.cellSize);
    // int celly2 = (int)(o2.getY() / settings.cellSize);
    // result = celly2 - celly1;
    // if (result != 0)
    // return result;
    // return (o1.getZ() == o2.getZ()) ? 0 : (o1.getZ() == 0) ? -1 : 1;
    // }});
    final LoadLocalisationsSettings.Builder settings = SettingsManager.readLoadLocalisationsSettings(0).toBuilder();
    final String[] path = ImageJUtils.decodePath(settings.getLocalisationsFilename());
    final OpenDialog chooser = new OpenDialog("Localisations_File", path[0], path[1]);
    if (chooser.getFileName() != null) {
        settings.setLocalisationsFilename(FileUtils.replaceExtension(chooser.getDirectory() + chooser.getFileName(), "xls"));
        SettingsManager.writeSettings(settings.build());
        try (BufferedWriter output = Files.newBufferedWriter(Paths.get(settings.getLocalisationsFilename()))) {
            output.write(createResultsFileHeader());
            output.write("#T\tId\tX\tY\tZ\tIntensity");
            output.newLine();
            for (final LocalisationModel l : localisations) {
                final StringBuilder sb = new StringBuilder();
                sb.append(l.getTime()).append('\t');
                sb.append(l.getId()).append('\t');
                sb.append(l.getX()).append('\t');
                sb.append(l.getY()).append('\t');
                sb.append(l.getZ()).append('\t');
                sb.append(l.getIntensity());
                output.write(sb.toString());
                output.newLine();
            }
        } catch (final Exception ex) {
            // Q. Add better handling of errors?
            ex.printStackTrace();
            IJ.log("Failed to save localisations to file: " + settings.getLocalisationsFilename());
        }
    }
}
Also used : LoadLocalisationsSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.LoadLocalisationsSettings) LocalisationModel(uk.ac.sussex.gdsc.smlm.model.LocalisationModel) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) IOException(java.io.IOException) DataException(uk.ac.sussex.gdsc.core.data.DataException) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) OpenDialog(ij.io.OpenDialog) BufferedWriter(java.io.BufferedWriter)

Aggregations

OpenDialog (ij.io.OpenDialog)34 IOException (java.io.IOException)13 ImagePlus (ij.ImagePlus)6 FormatException (loci.formats.FormatException)6 BufferedWriter (java.io.BufferedWriter)5 File (java.io.File)5 NullArgumentException (org.apache.commons.math3.exception.NullArgumentException)4 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)3 FilePeakResults (gdsc.smlm.results.FilePeakResults)3 AmiraMeshDecoder (amira.AmiraMeshDecoder)2 FilterSettings (gdsc.smlm.ij.settings.FilterSettings)2 FileSaver (ij.io.FileSaver)2 Layer (ini.trakem2.display.Layer)2 Patch (ini.trakem2.display.Patch)2 YesNoDialog (ini.trakem2.display.YesNoDialog)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 ArrayList (java.util.ArrayList)2