Search in sources :

Example 1 with XMLOptions

use of ini.trakem2.persistence.XMLOptions in project TrakEM2 by trakem2.

the class Patch method exportXML.

/**
 * Opens and closes the tag and exports data. The image is saved in the directory provided in @param any as a String.
 */
@Override
public void exportXML(final StringBuilder sb_body, final String indent, final XMLOptions options) {
    // TODO the Loader should handle the saving of images, not this class.
    final String in = indent + "\t";
    String path = null;
    String path2 = null;
    if (options.export_images) {
        path = options.patches_dir + title;
        // save image without overwriting, and add proper extension (.zip)
        path2 = project.getLoader().exportImage(this, path, false);
    // path2 will be null if the file exists already
    }
    sb_body.append(indent).append("<t2_patch\n");
    String rel_path = null;
    if (null != path && path.equals(path2)) {
        // this happens when a DB project is exported. It may be a different path when it's a FS loader
        // Utils.log2("p id=" + id + "  path==path2");
        rel_path = path2;
        // TrakEM2 uses paths that always have '/' and never '\', so using java.io.File.separatorChar would be an error.
        int i_slash = rel_path.lastIndexOf('/');
        if (i_slash > 0) {
            i_slash = rel_path.lastIndexOf('/', i_slash - 1);
            if (-1 != i_slash) {
                rel_path = rel_path.substring(i_slash + 1);
            }
        }
    } else {
        // Utils.log2("Setting rel_path to " + path2);
        rel_path = path2;
    }
    // For FSLoader projects, saving a second time will save images as null unless calling it
    if (null == rel_path) {
        // Utils.log2("path2 was null");
        final Object ob = project.getLoader().getPath(this);
        path2 = null == ob ? null : (String) ob;
        if (null == path2) {
            // Utils.log2("ERROR: No path for Patch id=" + id + " and title: " + title);
            // at least some clue for recovery
            rel_path = title;
        } else {
            rel_path = path2;
        }
    }
    // Utils.log("Patch path is: " + rel_path);
    super.exportXML(sb_body, in, options);
    final String[] RGB = Utils.getHexRGBColor(color);
    int type = this.type;
    if (-1 == this.type) {
        Utils.log2("Retrieving type for p = " + this);
        final ImagePlus imp = project.getLoader().fetchImagePlus(this);
        if (null != imp)
            type = imp.getType();
    }
    sb_body.append(in).append("type=\"").append(type).append("\"\n").append(in).append("file_path=\"").append(rel_path).append("\"\n").append(in).append("style=\"fill-opacity:").append(alpha).append(";stroke:#").append(RGB[0]).append(RGB[1]).append(RGB[2]).append(";\"\n").append(in).append("o_width=\"").append(o_width).append("\"\n").append(in).append("o_height=\"").append(o_height).append("\"\n");
    if (null != original_path) {
        sb_body.append(in).append("original_path=\"").append(original_path).append("\"\n");
    }
    sb_body.append(in).append("min=\"").append(min).append("\"\n");
    sb_body.append(in).append("max=\"").append(max).append("\"\n");
    final String pps = getPreprocessorScriptPath();
    if (null != pps)
        sb_body.append(in).append("pps=\"").append(project.getLoader().makeRelativePath(pps)).append("\"\n");
    sb_body.append(in).append("mres=\"").append(meshResolution).append("\"\n");
    if (hasCoordinateTransform()) {
        sb_body.append(in).append("ct_id=\"").append(ct_id).append("\"\n");
    }
    if (hasAlphaMask()) {
        sb_body.append(in).append("alpha_mask_id=\"").append(alpha_mask_id).append("\"\n");
    }
    sb_body.append(indent).append(">\n");
    if (hasCoordinateTransform()) {
        if (options.include_coordinate_transform) {
            // Write an XML entry for the CoordinateTransform
            char[] ct_chars = null;
            try {
                ct_chars = readCoordinateTransformFile();
            } catch (final Exception e) {
                IJError.print(e);
            }
            if (null != ct_chars) {
                sb_body.append(ct_chars).append('\n');
            } else {
                Utils.log("ERROR: could not write the CoordinateTransform to the XML file!");
            }
        }
    }
    if (null != filters && filters.length > 0) {
        // specify their own line termination
        for (final IFilter f : filters) sb_body.append(f.toXML(in));
    }
    super.restXML(sb_body, in, options);
    sb_body.append(indent).append("</t2_patch>\n");
}
Also used : IFilter(ini.trakem2.imaging.filters.IFilter) ImagePlus(ij.ImagePlus) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) IOException(java.io.IOException)

Example 2 with XMLOptions

use of ini.trakem2.persistence.XMLOptions in project TrakEM2 by trakem2.

the class FSLoader method saveAs.

/**
 * Meant for programmatic access, such as calls to project.saveAs(path, overwrite) which call exactly this method.
 */
@Override
public String saveAs(final String path, final XMLOptions options) {
    if (null == path) {
        Utils.log("Cannot save on null path.");
        return null;
    }
    String path2 = path;
    String extension = ".xml";
    if (// all fine
    path2.endsWith(extension)) // all fine
    {
    } else if (path2.endsWith(".xml.gz"))
        extension = ".xml.gz";
    else {
        // neither matches, add the default ".xml"
        path2 += extension;
    }
    File fxml = new File(path2);
    if (!fxml.canWrite()) {
        // write to storage folder instead
        String path3 = path2;
        path2 = getStorageFolder() + fxml.getName();
        Utils.logAll("WARNING can't write to " + path3 + "\n  --> will write instead to " + path2);
        fxml = new File(path2);
    }
    if (!options.overwriteXMLFile) {
        int i = 1;
        while (fxml.exists()) {
            String parent = fxml.getParent().replace('\\', '/');
            if (!parent.endsWith("/"))
                parent += "/";
            String name = fxml.getName();
            name = name.substring(0, name.length() - 4);
            path2 = parent + name + "-" + i + extension;
            fxml = new File(path2);
            i++;
        }
    }
    Project project = Project.findProject(this);
    path2 = super.saveAs(project, path2, options);
    if (null != path2) {
        project_file_path = path2;
        Utils.logAll("After saveAs, new xml path is: " + path2);
        ControlWindow.updateTitle(project);
        touched_mipmaps.clear();
    }
    return path2;
}
Also used : Project(ini.trakem2.Project) File(java.io.File)

Example 3 with XMLOptions

use of ini.trakem2.persistence.XMLOptions in project TrakEM2 by trakem2.

the class Project method saveTask.

public Bureaucrat saveTask(final String command) {
    return Bureaucrat.createAndStart(new Worker.Task("Saving") {

        public void exec() {
            if (command.equals("Save")) {
                save();
            } else if (command.equals("Save as...")) {
                XMLOptions options = new XMLOptions();
                options.overwriteXMLFile = false;
                options.export_images = false;
                options.include_coordinate_transform = true;
                options.patches_dir = null;
                // Will open a file dialog
                loader.saveAs(project, options);
                restartAutosaving();
            // 
            } else if (command.equals("Save as... without coordinate transforms")) {
                YesNoDialog yn = new YesNoDialog("WARNING", "You are about to save an XML file that lacks the information for the coordinate transforms of each image.\n" + "These transforms are referred to with the attribute 'ct_id' of each 't2_patch' entry in the XML document,\n" + "and the data for the transform is stored in an individual file under the folder 'trakem2.cts/'.\n" + " \n" + "It is advised to keep a complete XML file with all coordinate transforms included along with this new copy.\n" + "Please check NOW that you have such a complete XML copy.\n" + " \n" + "Proceed?");
                if (!yn.yesPressed())
                    return;
                saveWithoutCoordinateTransforms();
            // 
            } else if (command.equals("Delete stale files...")) {
                setTaskName("Deleting stale files");
                GenericDialog gd = new GenericDialog("Delete stale files");
                gd.addMessage("You are about to remove all files under the folder 'trakem2.cts/' which are not referred to from the\n" + "currently loaded project. If you have sibling XML files whose 't2_patch' entries (the images) refer,\n" + "via 'ct_id' attributes, to coordinate transforms in 'trakem2.cts/' that this current XML doesn't,\n" + "they may be LOST FOREVER. Unless you have a version of the XML file with the coordinate transforms\n" + "written in it, as can be obtained by using the 'Project - Save' command.\n" + " \n" + "The same is true for the .zip files that store alpha masks, under folder 'trakem2.masks/'\n" + "and which are referred to from the 'alpha_mask_id' attribute of 't2_patch' entries.\n" + " \n" + "Do you have such complete XML file? Check *NOW*.\n" + " \n" + "Proceed with deleting:");
                gd.addCheckbox("Delete stale coordinate transform files", true);
                gd.addCheckbox("Delete stale alpha mask files", true);
                gd.showDialog();
                if (gd.wasCanceled())
                    return;
                project.getLoader().deleteStaleFiles(gd.getNextBoolean(), gd.getNextBoolean());
            }
        }
    }, project);
}
Also used : YesNoDialog(ini.trakem2.display.YesNoDialog) GenericDialog(ij.gui.GenericDialog) Worker(ini.trakem2.utils.Worker) XMLOptions(ini.trakem2.persistence.XMLOptions)

Example 4 with XMLOptions

use of ini.trakem2.persistence.XMLOptions in project TrakEM2 by trakem2.

the class Project method saveWithoutCoordinateTransforms.

/**
 * Save an XML file that is stripped of coordinate transforms,
 * and merely refers to them by the 'ct_id' attribute of each 't2_patch' element;
 * this method will NOT overwrite the XML file but save into a new one,
 * which is chosen from a file dialog.
 */
public String saveWithoutCoordinateTransforms() {
    XMLOptions options = new XMLOptions();
    options.overwriteXMLFile = false;
    options.export_images = false;
    options.include_coordinate_transform = false;
    options.patches_dir = null;
    return loader.saveAs(this, options);
}
Also used : XMLOptions(ini.trakem2.persistence.XMLOptions)

Example 5 with XMLOptions

use of ini.trakem2.persistence.XMLOptions in project TrakEM2 by trakem2.

the class Layer method exportXML.

@Override
public void exportXML(final StringBuilder sb_body, final String indent, final XMLOptions options) {
    final String in = indent + "\t";
    // 1 - open tag
    sb_body.append(indent).append("<t2_layer oid=\"").append(id).append("\"\n").append(in).append(" thickness=\"").append(thickness).append("\"\n").append(in).append(" z=\"").append(z).append("\"\n");
    // TODO this search is linear!
    final LayerThing lt = project.findLayerThing(this);
    String title;
    if (null == lt)
        title = null;
    else
        title = lt.getTitle();
    if (null == title)
        title = "";
    // TODO 'title' should be a property of the Layer, not the LayerThing. Also, the LayerThing should not exist: LayerSet and Layer should be directly presentable in a tree. They are not Things as in "objects of the sample", but rather, structural necessities such as Patch.
    sb_body.append(in).append(" title=\"").append(title).append("\"\n");
    sb_body.append(indent).append(">\n");
    // 2 - export children
    if (null != al_displayables) {
        for (final Displayable d : al_displayables) {
            d.exportXML(sb_body, in, options);
        }
    }
    // 3 - close tag
    sb_body.append(indent).append("</t2_layer>\n");
}
Also used : LayerThing(ini.trakem2.tree.LayerThing)

Aggregations

XMLOptions (ini.trakem2.persistence.XMLOptions)4 ImagePlus (ij.ImagePlus)1 GenericDialog (ij.gui.GenericDialog)1 Project (ini.trakem2.Project)1 YesNoDialog (ini.trakem2.display.YesNoDialog)1 IFilter (ini.trakem2.imaging.filters.IFilter)1 LayerThing (ini.trakem2.tree.LayerThing)1 ProjectThing (ini.trakem2.tree.ProjectThing)1 Worker (ini.trakem2.utils.Worker)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 NoninvertibleModelException (mpicbg.models.NoninvertibleModelException)1