use of ini.trakem2.tree.ProjectThing 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.ProjectThing 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;
}
use of ini.trakem2.tree.ProjectThing in project TrakEM2 by trakem2.
the class Project method removeProjectThing.
/**
* Remove the ProjectThing that contains the given object, which will remove the object itself as well.
*/
public boolean removeProjectThing(Object object, boolean check, boolean remove_empty_parents, int levels) {
if (levels < 0) {
Utils.log2("Project.removeProjectThing: levels must be zero or above.");
return false;
}
// find the Thing
DefaultMutableTreeNode root = (DefaultMutableTreeNode) project_tree.getModel().getRoot();
Enumeration<?> e = root.depthFirstEnumeration();
DefaultMutableTreeNode node = null;
while (e.hasMoreElements()) {
node = (DefaultMutableTreeNode) e.nextElement();
Object ob = node.getUserObject();
if (ob instanceof ProjectThing && ((ProjectThing) ob).getObject() == object) {
if (check && !Utils.check("Remove " + object.toString() + "?"))
return false;
// remove the ProjectThing, its object and the node that holds it.
project_tree.remove(node, false, remove_empty_parents, levels);
return true;
}
// the above could be done more generic with a Thing.contains(Object), but I want to make sure that the object is contained by a ProjectThing and nothing else.
}
// not found:
return false;
}
use of ini.trakem2.tree.ProjectThing in project TrakEM2 by trakem2.
the class Project method exportXML2.
// A separate method to ensure that sb_body instance is garbage collected.
private final void exportXML2(final java.io.Writer writer, final String in, final XMLOptions options) throws Exception {
final StringBuilder sb_body = new StringBuilder();
// 2 - the project itself
sb_body.append(in).append("<project \n").append(in).append("\tid=\"").append(id).append("\"\n").append(in).append("\ttitle=\"").append(title).append("\"\n");
loader.insertXMLOptions(sb_body, in + "\t");
// Write properties, with the additional property of the image_resizing_mode
final HashMap<String, String> props = new HashMap<String, String>(ht_props);
props.put("image_resizing_mode", Loader.getMipMapModeName(mipmaps_mode));
props.put("first_mipmap_level_saved", Integer.toString(this.first_mipmap_level_saved));
for (final Map.Entry<String, String> e : props.entrySet()) {
sb_body.append(in).append('\t').append(e.getKey()).append("=\"").append(e.getValue()).append("\"\n");
}
sb_body.append(in).append(">\n");
// 3 - export ProjectTree abstract hierarchy (skip the root since it wraps the project itself)
project_tree.getExpandedStates(options.expanded_states);
if (null != root_pt.getChildren()) {
final String in2 = in + "\t";
for (final ProjectThing pt : root_pt.getChildren()) {
pt.exportXML(sb_body, in2, options);
}
}
sb_body.append(in).append("</project>\n");
writer.write(sb_body.toString());
}
use of ini.trakem2.tree.ProjectThing in project TrakEM2 by trakem2.
the class Project method getMeaningfulTitle.
/**
* Searches upstream in the Project tree for things that have a user-defined name, stops at the first and returns it along with all the intermediate ones that only have a type and not a title, appended.
*/
public String getMeaningfulTitle(final Displayable d) {
ProjectThing thing = findProjectThing(d);
// happens if there is no associated node
if (null == thing)
return d.getTitle();
String title = new StringBuilder(!thing.getType().equals(d.getTitle()) ? d.getTitle() + " [" : "[").append(thing.getType()).append(' ').append('#').append(d.getId()).append(']').toString();
if (!thing.getType().equals(d.getTitle())) {
return title;
}
ProjectThing parent = (ProjectThing) thing.getParent();
StringBuilder sb = new StringBuilder(title);
while (null != parent) {
Object ob = parent.getObject();
if (ob.getClass() == Project.class)
break;
String type = parent.getType();
if (!ob.equals(type)) {
// meaning, something else was typed in as a title
sb.insert(0, new StringBuilder(ob.toString()).append(' ').append('[').append(type).append(']').append('/').toString());
// title = ob.toString() + " [" + type + "]/" + title;
break;
}
sb.insert(0, '/');
sb.insert(0, type);
// title = type + "/" + title;
parent = (ProjectThing) parent.getParent();
}
// return title;
return sb.toString();
}
Aggregations