Search in sources :

Example 16 with Thing

use of ini.trakem2.tree.Thing 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 17 with Thing

use of ini.trakem2.tree.Thing 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 18 with Thing

use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.

the class Project method getShortMeaningfulTitle.

public String getShortMeaningfulTitle(final ProjectThing thing, final Displayable d) {
    if (thing.getObject() != d) {
        return thing.toString();
    }
    ProjectThing parent = (ProjectThing) thing.getParent();
    String title = "#" + d.getId();
    while (null != parent) {
        Object ob = parent.getObject();
        String type = parent.getType();
        if (!ob.equals(type)) {
            // meaning, something else was typed in as a title
            title = ob.toString() + " [" + type + "] " + title;
            break;
        }
        parent = (ProjectThing) parent.getParent();
    }
    // if nothing found, prepend the type
    if ('#' == title.charAt(0))
        title = Project.getName(d.getClass()) + " " + title;
    return title;
}
Also used : DBObject(ini.trakem2.persistence.DBObject) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 19 with Thing

use of ini.trakem2.tree.Thing 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)

Example 20 with Thing

use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.

the class Project method openFSProject.

/**
 * Opens a project from an .xml file. If the path is null it'll be asked for.
 *  Only one project may be opened at a time.
 */
@SuppressWarnings("unchecked")
public static synchronized Project openFSProject(final String path, final boolean open_displays) {
    if (Utils.wrongImageJVersion())
        return null;
    final FSLoader loader = new FSLoader();
    final Object[] data = loader.openFSProject(path, open_displays);
    if (null == data) {
        loader.destroy();
        return null;
    }
    final TemplateThing root_tt = (TemplateThing) data[0];
    final ProjectThing root_pt = (ProjectThing) data[1];
    final LayerThing root_lt = (LayerThing) data[2];
    final HashMap<ProjectThing, Boolean> ht_pt_expanded = (HashMap<ProjectThing, Boolean>) data[3];
    final Project project = (Project) root_pt.getObject();
    project.createLayerTemplates();
    project.template_tree = new TemplateTree(project, root_tt);
    project.root_tt = root_tt;
    project.root_pt = root_pt;
    project.project_tree = new ProjectTree(project, project.root_pt);
    project.layer_tree = new LayerTree(project, root_lt);
    project.root_lt = root_lt;
    project.layer_set = (LayerSet) root_lt.getObject();
    // 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);
    // set ProjectThing nodes expanded state, now that the trees exist
    try {
        java.lang.reflect.Field f = JTree.class.getDeclaredField("expandedState");
        f.setAccessible(true);
        Hashtable<Object, Object> ht_exp = (Hashtable<Object, Object>) f.get(project.project_tree);
        for (Map.Entry<ProjectThing, Boolean> entry : ht_pt_expanded.entrySet()) {
            ProjectThing pt = entry.getKey();
            Boolean expanded = entry.getValue();
            // project.project_tree.expandPath(new TreePath(project.project_tree.findNode(pt, project.project_tree).getPath()));
            // WARNING the above is wrong in that it will expand the whole thing, not just set the state of the node!!
            // So the ONLY way to do it is to start from the child-most leafs of the tree, and apply the expanding to them upward. This is RIDICULOUS, how can it be so broken
            // so, hackerous:
            DefaultMutableTreeNode nd = DNDTree.findNode(pt, project.project_tree);
            // else Utils.log2("path: " + new TreePath(nd.getPath()));
            if (null == nd) {
                Utils.log2("Can't find node for " + pt);
            } else {
                ht_exp.put(new TreePath(nd.getPath()), expanded);
            }
        }
        // very important!!
        project.project_tree.updateUILater();
    } catch (Exception e) {
        IJError.print(e);
    }
    // open any stored displays
    if (open_displays) {
        final Bureaucrat burro = Display.openLater();
        if (null != burro) {
            final Runnable ru = new Runnable() {

                public void run() {
                    // wait until the Bureaucrat finishes
                    try {
                        burro.join();
                    } catch (InterruptedException ie) {
                    }
                    // restore to non-changes (crude, but works)
                    project.loader.setChanged(false);
                    Utils.log2("C set to false");
                }
            };
            new Thread() {

                public void run() {
                    setPriority(Thread.NORM_PRIORITY);
                    // avoiding "can't call invokeAndWait from the EventDispatch thread" error
                    try {
                        javax.swing.SwingUtilities.invokeAndWait(ru);
                    } catch (Exception e) {
                        Utils.log2("ERROR: " + e);
                    }
                }
            }.start();
            // SO: WAIT TILL THE END OF TIME!
            new Thread() {

                public void run() {
                    try {
                        // ah, the pain in my veins. I can't take this shitty setup anymore.
                        Thread.sleep(4000);
                        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

                            public void run() {
                                project.getLoader().setChanged(false);
                                Utils.log2("D set to false");
                            }
                        });
                        // repainting to fix gross errors in tree rendering
                        project.getTemplateTree().updateUILater();
                        // idem
                        project.getProjectTree().updateUILater();
                    } catch (Exception ie) {
                    }
                }
            }.start();
        } else {
            // help the helpless users
            Display.createDisplay(project, project.layer_set.getLayer(0));
        }
    }
    project.restartAutosaving();
    return project;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashMap(java.util.HashMap) TemplateTree(ini.trakem2.tree.TemplateTree) Bureaucrat(ini.trakem2.utils.Bureaucrat) ProjectTree(ini.trakem2.tree.ProjectTree) LayerTree(ini.trakem2.tree.LayerTree) ProjectThing(ini.trakem2.tree.ProjectThing) LayerThing(ini.trakem2.tree.LayerThing) Hashtable(java.util.Hashtable) FSLoader(ini.trakem2.persistence.FSLoader) TreePath(javax.swing.tree.TreePath) TemplateThing(ini.trakem2.tree.TemplateThing) DBObject(ini.trakem2.persistence.DBObject) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

DBObject (ini.trakem2.persistence.DBObject)16 ProjectThing (ini.trakem2.tree.ProjectThing)15 TemplateThing (ini.trakem2.tree.TemplateThing)9 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)9 LayerThing (ini.trakem2.tree.LayerThing)8 TreePath (javax.swing.tree.TreePath)8 HashMap (java.util.HashMap)7 Displayable (ini.trakem2.display.Displayable)6 ZDisplayable (ini.trakem2.display.ZDisplayable)6 Thing (ini.trakem2.tree.Thing)6 GenericDialog (ij.gui.GenericDialog)5 Layer (ini.trakem2.display.Layer)5 LayerSet (ini.trakem2.display.LayerSet)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 ProjectTree (ini.trakem2.tree.ProjectTree)4 JPopupMenu (javax.swing.JPopupMenu)4 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)4 Project (ini.trakem2.Project)3 Profile (ini.trakem2.display.Profile)3