Search in sources :

Example 21 with Node

use of ini.trakem2.display.Node in project TrakEM2 by trakem2.

the class Compare method appendAndFork.

/**
 * Recursive.
 */
private static void appendAndFork(final ProjectThing parent, Chain chain, HashSet<ProjectThing> hs_c_done, final ArrayList<Chain> chains, final LayerSet ls, final Pattern exclude) throws Exception {
    if (null != exclude && exclude.matcher(parent.getTitle()).matches()) {
        Utils.logAll("Excluding node " + parent + " with title " + parent.getTitle() + ", and all its children nodes.");
        return;
    }
    final ArrayList<ProjectThing> children = parent.getChildren();
    if (null == children)
        return;
    if (null == hs_c_done)
        hs_c_done = new HashSet<ProjectThing>();
    for (final ProjectThing child : children) {
        if (hs_c_done.contains(child))
            continue;
        if (null != exclude && exclude.matcher(child.getTitle()).matches()) {
            Utils.log2("Excluding child " + child + " with title " + child.getTitle());
            continue;
        }
        hs_c_done.add(child);
        if (child.getObject() instanceof Line3D) {
            final Line3D pipe = (Line3D) child.getObject();
            // not from the same LayerSet, maybe from a nested one.
            if (!pipe.getLayerSet().equals(ls) || pipe.length() < 2)
                continue;
            if (null == chain) {
                chain = new Chain(pipe);
                chains.add(chain);
            } else {
                chain.append(pipe);
            }
            // find other children in the parent who contain children with child pipes
            boolean first = true;
            final Chain base = chain.duplicate();
            for (final ProjectThing c : children) {
                // already visited
                if (hs_c_done.contains(c))
                    continue;
                // c is at the same tree level as child (which contains a pipe directly)
                final ArrayList<Line3D> child_pipes = c.findChildrenOfType(Line3D.class);
                if (child_pipes.size() > 0) {
                    Chain ca;
                    if (first) {
                        // just append
                        ca = chain;
                        first = false;
                    } else {
                        // otherwise make a copy to branch out
                        // can't duplicate from chain itself, because it would have the previous child added to it.
                        ca = base.duplicate();
                        chains.add(ca);
                    }
                    appendAndFork(c, ca, hs_c_done, chains, ls, exclude);
                }
            }
            // pipe wrapping ProjectThing objects cannot have any children
            continue;
        }
        // if it does not have direct pipe children, cut chain - but keep inspecting
        if (0 == child.findChildrenOfType(Line3D.class).size()) {
            chain = null;
        }
        // inspect others down the unvisited tree nodes
        appendAndFork(child, chain, hs_c_done, chains, ls, exclude);
    }
}
Also used : ProjectThing(ini.trakem2.tree.ProjectThing) Line3D(ini.trakem2.display.Line3D) HashSet(java.util.HashSet)

Example 22 with Node

use of ini.trakem2.display.Node in project TrakEM2 by trakem2.

the class Project method getParentTitle.

/**
 * Returns the title of the enclosing abstract node in the ProjectTree.
 */
public String getParentTitle(final Displayable d) {
    try {
        ProjectThing thing = findProjectThing(d);
        ProjectThing parent = (ProjectThing) thing.getParent();
        if (d instanceof Profile) {
            // skip the profile_list
            parent = (ProjectThing) parent.getParent();
        }
        if (null == parent)
            Utils.log2("null parent for " + d);
        if (null != parent && null == parent.getObject()) {
            Utils.log2("null ob for parent " + parent + " of " + d);
        }
        // the abstract thing should be enclosing a String object
        return parent.getObject().toString();
    } catch (Exception e) {
        IJError.print(e);
        return null;
    }
}
Also used : ProjectThing(ini.trakem2.tree.ProjectThing) Profile(ini.trakem2.display.Profile)

Example 23 with Node

use of ini.trakem2.display.Node in project TrakEM2 by trakem2.

the class Project method getMeaningfulTitle2.

public String getMeaningfulTitle2(final Displayable d) {
    final ProjectThing thing = findProjectThing(d);
    // happens if there is no associated node
    if (null == thing)
        return d.getTitle();
    if (!thing.getType().equals(d.getTitle())) {
        return new StringBuilder(!thing.getType().equals(d.getTitle()) ? d.getTitle() + " [" : "[").append(thing.getType()).append(']').toString();
    }
    // Else, search upstream for a ProjectThing whose name differs from its type
    Thing parent = (ProjectThing) thing.getParent();
    while (null != parent) {
        String type = parent.getType();
        Object ob = parent.getObject();
        if (ob.getClass() == Project.class)
            break;
        if (!ob.equals(type)) {
            return ob.toString() + " [" + thing.getType() + "]";
        }
        parent = parent.getParent();
    }
    if (d.getTitle().equals(thing.getType()))
        return "[" + thing.getType() + "]";
    return d.getTitle() + " [" + thing.getType() + "]";
}
Also used : DBObject(ini.trakem2.persistence.DBObject) ProjectThing(ini.trakem2.tree.ProjectThing) LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) ProjectThing(ini.trakem2.tree.ProjectThing) Thing(ini.trakem2.tree.Thing)

Example 24 with Node

use of ini.trakem2.display.Node in project TrakEM2 by trakem2.

the class Project method openDBProject.

/**
 * Open a TrakEM2 project from the database. Queries the database for existing projects and if more than one, asks which one to open.
 */
public static Project openDBProject() {
    if (Utils.wrongImageJVersion())
        return null;
    DBLoader loader = new DBLoader();
    if (!loader.isReady())
        return null;
    // check connection
    if (!loader.isConnected()) {
        Utils.showMessage("Can't talk to database.");
        loader.destroy();
        return null;
    }
    // query the database for existing projects
    Project[] projects = loader.getProjects();
    if (null == projects) {
        Utils.showMessage("Can't talk to database (null list of projects).");
        loader.destroy();
        return null;
    }
    Project project = null;
    if (0 == projects.length) {
        Utils.showMessage("No projects in this database.");
        loader.destroy();
        return null;
    } else if (1 == projects.length) {
        project = projects[0];
    } else {
        // ask to choose one
        String[] titles = new String[projects.length];
        for (int i = 0; i < projects.length; i++) {
            titles[i] = projects[i].title;
        }
        GenericDialog gd = new GenericDialog("Choose");
        gd.addMessage("Choose project to open:");
        gd.addChoice("project: ", titles, titles[titles.length - 1]);
        gd.showDialog();
        if (gd.wasCanceled()) {
            loader.destroy();
            return null;
        }
        project = projects[gd.getNextChoiceIndex()];
    }
    // check if the selected project is open already
    for (final Project p : al_open_projects) {
        if (loader.isIdenticalProjectSource(p.loader) && p.id == project.id && p.title.equals(project.title)) {
            Utils.showMessage("A project with title " + p.title + " and id " + p.id + " from the same database is already open.");
            loader.destroy();
            return null;
        }
    }
    // now, open the selected project
    // assign loader
    project.loader = loader;
    // grab the XML template
    TemplateThing template_root = loader.getTemplateRoot(project);
    if (null == template_root) {
        Utils.showMessage("Failed to retrieve the template tree.");
        project.destroy();
        return null;
    }
    project.template_tree = new TemplateTree(project, template_root);
    synchronized (project.ht_unique_tt) {
        project.ht_unique_tt.clear();
        project.ht_unique_tt.putAll(template_root.getUniqueTypes(new HashMap<String, TemplateThing>()));
    }
    // create the project Thing, to be root of the whole user Thing tree (and load all its objects)
    // to collect all created displayables, and  then reassign to the proper layers.
    HashMap<Long, Displayable> hs_d = new HashMap<Long, Displayable>();
    try {
        // create a template for the project Thing
        TemplateThing project_template = new TemplateThing("project");
        project.ht_unique_tt.put("project", project_template);
        project_template.addChild(template_root);
        project.root_pt = loader.getRootProjectThing(project, template_root, project_template, hs_d);
        // restore parent/child and attribute ownership and values (now that all Things exist)
        project.root_pt.setup();
    } catch (Exception e) {
        Utils.showMessage("Failed to retrieve the Thing tree for the project.");
        IJError.print(e);
        project.destroy();
        return null;
    }
    // create the user objects tree
    project.project_tree = new ProjectTree(project, project.root_pt);
    // restore the expanded state of each node
    loader.restoreNodesExpandedState(project);
    // create the layers templates
    project.createLayerTemplates();
    // fetch the root layer thing and the root layer set (will load all layers and layer sets, with minimal contents of patches; gets the basic objects -profile, pipe, etc.- from the project.root_pt). Will open all existing displays for each layer.
    LayerThing root_layer_thing = null;
    try {
        root_layer_thing = loader.getRootLayerThing(project, project.root_pt, Project.layer_set_template, Project.layer_template);
        if (null == root_layer_thing) {
            project.destroy();
            Utils.showMessage("Could not retrieve the root layer thing.");
            return null;
        }
        // set the child/parent relationships now that everything exists
        root_layer_thing.setup();
        project.layer_set = (LayerSet) root_layer_thing.getObject();
        if (null == project.layer_set) {
            project.destroy();
            Utils.showMessage("Could not retrieve the root layer set.");
            return null;
        }
        // set the active layer to each ZDisplayable
        project.layer_set.setup();
        // debug:
        // Utils.log2("$$$ root_lt: " + root_layer_thing + "   ob: " + root_layer_thing.getObject().getClass().getName() + "\n children: " + ((LayerSet)root_layer_thing.getObject()).getLayers().size());
        project.layer_tree = new LayerTree(project, root_layer_thing);
        project.root_lt = root_layer_thing;
    } catch (Exception e) {
        Utils.showMessage("Failed to retrieve the Layer tree for the project.");
        IJError.print(e);
        project.destroy();
        return null;
    }
    // if all when well, register as open:
    al_open_projects.add(project);
    // create the project control window, containing the trees in a double JSplitPane
    ControlWindow.add(project, project.template_tree, project.project_tree, project.layer_tree);
    // now open the displays that were stored for later, if any:
    Display.openLater();
    return project;
}
Also used : Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) HashMap(java.util.HashMap) LayerThing(ini.trakem2.tree.LayerThing) TemplateTree(ini.trakem2.tree.TemplateTree) ProjectTree(ini.trakem2.tree.ProjectTree) LayerTree(ini.trakem2.tree.LayerTree) DBLoader(ini.trakem2.persistence.DBLoader) GenericDialog(ij.gui.GenericDialog) TemplateThing(ini.trakem2.tree.TemplateThing)

Example 25 with Node

use of ini.trakem2.display.Node in project TrakEM2 by trakem2.

the class Project method select.

private final void select(final Object ob, final DNDTree tree) {
    // Find the Thing that contains the object
    final Thing root_thing = (Thing) ((DefaultMutableTreeNode) tree.getModel().getRoot()).getUserObject();
    final Thing child_thing = root_thing.findChild(ob);
    // find the node that contains the Thing, and select it
    DNDTree.selectNode(child_thing, tree);
}
Also used : LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) ProjectThing(ini.trakem2.tree.ProjectThing) Thing(ini.trakem2.tree.Thing)

Aggregations

DBObject (ini.trakem2.persistence.DBObject)16 ProjectThing (ini.trakem2.tree.ProjectThing)12 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)12 TreePath (javax.swing.tree.TreePath)12 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)11 Displayable (ini.trakem2.display.Displayable)9 ZDisplayable (ini.trakem2.display.ZDisplayable)9 ArrayList (java.util.ArrayList)9 GenericDialog (ij.gui.GenericDialog)8 Layer (ini.trakem2.display.Layer)7 LayerThing (ini.trakem2.tree.LayerThing)6 Point (java.awt.Point)6 Map (java.util.Map)6 JMenuItem (javax.swing.JMenuItem)6 JPopupMenu (javax.swing.JPopupMenu)6 Project (ini.trakem2.Project)5 TemplateThing (ini.trakem2.tree.TemplateThing)5 Color (java.awt.Color)5 AreaTree (ini.trakem2.display.AreaTree)4