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() + "]";
}
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;
}
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;
}
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);
}
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;
}
Aggregations