use of ini.trakem2.tree.Thing 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.Thing 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();
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class TMLHandler method startElement.
public void startElement(String namespace_URI, String local_name, String qualified_name, Attributes attributes) throws SAXException {
if (null == loader)
return;
// Utils.log2("startElement: " + qualified_name);
this.counter++;
if (0 == counter % 100) {
// davi-experimenting: don't talk so much when you have > 600,000 patches to load!
Utils.showStatus("Loading " + counter, false);
}
try {
// failsafe:
qualified_name = qualified_name.toLowerCase();
final HashMap<String, String> ht_attributes = new HashMap<String, String>();
for (int i = attributes.getLength() - 1; i > -1; i--) {
ht_attributes.put(attributes.getQName(i).toLowerCase(), attributes.getValue(i));
}
// get the id, which whenever possible it's the id of the encapsulating Thing object. The encapsulated object id is the oid
// The type is specified by the qualified_name
Thing thing = null;
if (0 == qualified_name.indexOf("t2_")) {
if (qualified_name.equals("t2_display")) {
// store for later, until the layers exist
if (open_displays)
al_displays.add(ht_attributes);
} else {
// a Layer, LayerSet or Displayable object
thing = makeLayerThing(qualified_name, ht_attributes);
if (null != thing) {
if (null == root_lt && thing.getObject() instanceof LayerSet) {
root_lt = (LayerThing) thing;
}
}
}
} else if (qualified_name.equals("project")) {
if (null != this.root_pt) {
Utils.log("WARNING: more than one project definitions.");
return;
}
// Create the project
this.project = new Project(Long.parseLong(ht_attributes.remove("id")), ht_attributes.remove("title"));
// temp, but will be the same anyway
this.project.setTempLoader(this.loader);
this.project.parseXMLOptions(ht_attributes);
// register id
this.project.addToDatabase();
String title = ht_attributes.get("title");
if (null != title)
this.project.setTitle(title);
// Add all unique TemplateThing types to the project
for (Iterator<TemplateThing> it = root_tt.getUniqueTypes(new HashMap<String, TemplateThing>()).values().iterator(); it.hasNext(); ) {
this.project.addUniqueType(it.next());
}
this.project.addUniqueType(this.project_tt);
this.root_pt = new ProjectThing(this.project_tt, this.project, this.project);
// Add a project pointer to all template things
this.root_tt.addToDatabase(this.project);
thing = root_pt;
} else if (qualified_name.startsWith("ict_transform") || qualified_name.startsWith("iict_transform")) {
makeCoordinateTransform(qualified_name, ht_attributes);
} else if (!qualified_name.equals("trakem2")) {
// Any abstract object
thing = makeProjectThing(qualified_name, ht_attributes);
}
if (null != thing) {
// get the previously open thing and add this new_thing to it as a child
int size = al_open.size();
if (size > 0) {
Thing parent = al_open.get(size - 1);
parent.addChild(thing);
// Utils.log2("Adding child " + thing + " to parent " + parent);
}
// add the new thing as open
al_open.add(thing);
}
} catch (Exception e) {
IJError.print(e);
skip = true;
}
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class LayerThing method addChild.
public boolean addChild(Thing child) {
if (!template.canHaveAsChild(child)) {
return false;
}
if (null == al_children)
al_children = new ArrayList<LayerThing>();
synchronized (al_children) {
if (null != child.getObject() && child.getObject() instanceof Layer) {
// this is a patch, but hey, do you want to redesign the events, which are based on layer titles and toString() contents? TODO ...
Layer l = (Layer) child.getObject();
int i = l.getParent().indexOf(l);
// Utils.log2("al_children.size(): " + al_children.size() + ", i=" + i);
if (i >= al_children.size()) {
// TODO happens when importing a stack
al_children.add((LayerThing) child);
} else {
try {
al_children.add(i, (LayerThing) child);
} catch (Exception e) {
Utils.log2("LayerThing.addChild: " + e);
// at the end
al_children.add((LayerThing) child);
}
}
} else {
al_children.add((LayerThing) child);
}
}
child.setParent(this);
return true;
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class LayerTree method getPopupMenu.
/**
* Get a custom, context-sensitive popup menu for the selected node.
*/
private JPopupMenu getPopupMenu(DefaultMutableTreeNode node) {
Object ob = node.getUserObject();
LayerThing thing = null;
if (ob instanceof LayerThing) {
thing = (LayerThing) ob;
} else {
return null;
}
// context-sensitive popup
JMenuItem[] item = thing.getPopupItems(this);
if (0 == item.length)
return null;
JPopupMenu popup = new JPopupMenu();
for (int i = 0; i < item.length; i++) {
if (null == item[i] || "" == item[i].getText())
popup.addSeparator();
else
popup.add(item[i]);
}
return popup;
}
Aggregations