use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class ProjectTree method actionPerformed.
public void actionPerformed(final ActionEvent ae) {
if (!project.isInputEnabled())
return;
super.dispatcher.exec(new Runnable() {
public void run() {
try {
if (null == selected_node)
return;
final Object ob = selected_node.getUserObject();
if (!(ob instanceof ProjectThing))
return;
final ProjectThing thing = (ProjectThing) ob;
int i_position = 0;
String command = ae.getActionCommand();
final Object obd = thing.getObject();
if (command.startsWith("new ") || command.equals("Duplicate")) {
ProjectThing new_thing = null;
if (command.startsWith("new ")) {
// if it's a Displayable, it will be added to whatever layer is in the front Display
new_thing = thing.createChild(command.substring(4));
} else if (command.equals("Duplicate")) {
// just to keep myself from screwing in the future
if (Project.isBasicType(thing.getType()) && null != thing.getParent()) {
new_thing = ((ProjectThing) thing.getParent()).createClonedChild(thing);
}
// adjust parent
selected_node = (DefaultMutableTreeNode) selected_node.getParent();
}
// add it to the tree
if (null != new_thing) {
DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_thing);
((DefaultTreeModel) ProjectTree.this.getModel()).insertNodeInto(new_node, selected_node, i_position);
TreePath treePath = new TreePath(new_node.getPath());
ProjectTree.this.scrollPathToVisible(treePath);
ProjectTree.this.setSelectionPath(treePath);
}
// bring the display to front
if (new_thing.getObject() instanceof Displayable) {
Display.getFront().getFrame().toFront();
}
} else if (command.equals("many...")) {
ArrayList<TemplateThing> children = thing.getTemplate().getChildren();
if (null == children || 0 == children.size())
return;
String[] cn = new String[children.size()];
int i = 0;
for (final TemplateThing child : children) {
cn[i] = child.getType();
i++;
}
GenericDialog gd = new GenericDialog("Add many children");
gd.addNumericField("Amount: ", 1, 0);
gd.addChoice("New child: ", cn, cn[0]);
gd.addCheckbox("Recursive", true);
gd.showDialog();
if (gd.wasCanceled())
return;
int amount = (int) gd.getNextNumber();
if (amount < 1) {
Utils.showMessage("Makes no sense to create less than 1 child!");
return;
}
project.getRootLayerSet().addChangeTreesStep();
final ArrayList<ProjectThing> nc = thing.createChildren(cn[gd.getNextChoiceIndex()], amount, gd.getNextBoolean());
addLeafs(nc, new Runnable() {
public void run() {
project.getRootLayerSet().addChangeTreesStep();
}
});
} else if (command.equals("Unhide")) {
thing.setVisible(true);
} else if (command.equals("Select in display")) {
boolean shift_down = 0 != (ae.getModifiers() & ActionEvent.SHIFT_MASK);
selectInDisplay(thing, shift_down);
} else if (command.equals("Show centered in Display")) {
if (obd instanceof Displayable) {
Displayable displ = (Displayable) obd;
Display.showCentered(displ.getLayer(), displ, true, 0 != (ae.getModifiers() & ActionEvent.SHIFT_MASK));
}
} else if (command.equals("Show tabular view")) {
((Tree<?>) obd).createMultiTableView();
} else if (command.equals("Show in 3D")) {
Display3D.showAndResetView(thing);
} else if (command.equals("Remove from 3D view")) {
Display3D.removeFrom3D(thing);
} else if (command.equals("Hide")) {
// find all Thing objects in this subtree starting at Thing and hide their Displayable objects.
thing.setVisible(false);
} else if (command.equals("Delete...")) {
// store old state
project.getRootLayerSet().addChangeTreesStep();
remove(true, thing, selected_node);
// store new state
project.getRootLayerSet().addChangeTreesStep();
return;
} else if (command.equals("Rename...")) {
// if (!Project.isBasicType(thing.getType())) {
rename(thing);
// }
} else if (command.equals("Measure")) {
// block displays while measuring
Bureaucrat.createAndStart(new Worker("Measuring") {
public void run() {
startedWorking();
try {
thing.measure();
} catch (Throwable e) {
IJError.print(e);
} finally {
finishedWorking();
}
}
}, thing.getProject());
} else /* else if (command.equals("Export 3D...")) {
GenericDialog gd = ControlWindow.makeGenericDialog("Export 3D");
String[] choice = new String[]{".svg [preserves links and hierarchical grouping]", ".shapes [limited to one profile per layer per profile_list]"};
gd.addChoice("Export to: ", choice, choice[0]);
gd.addNumericField("Z scaling: ", 1, 2);
gd.showDialog();
if (gd.wasCanceled()) return;
double z_scale = gd.getNextNumber();
switch (gd.getNextChoiceIndex()) {
case 0:
Render.exportSVG(thing, z_scale);
break;
case 1:
new Render(thing).save(z_scale);
break;
}
}*/
if (command.equals("Export project...") || command.equals("Save as...")) {
// "Save as..." is for a FS project
Utils.log2("Calling export project at " + System.currentTimeMillis());
thing.getProject().getLoader().saveTask(thing.getProject(), "Save as...");
} else if (command.equals("Save")) {
// overwrite the xml file of a FSProject
// Just do the same as in "Save as..." but without saving the images and overwritting the XML file without asking.
thing.getProject().getLoader().saveTask(thing.getProject(), "Save");
} else if (command.equals("Info")) {
showInfo(thing);
return;
} else if (command.equals("Move up")) {
move(selected_node, -1);
} else if (command.equals("Move down")) {
move(selected_node, 1);
} else if (command.equals("Collapse nodes of children nodes")) {
if (null == selected_node)
return;
Enumeration<?> c = selected_node.children();
while (c.hasMoreElements()) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) c.nextElement();
if (child.isLeaf())
continue;
collapsePath(new TreePath(child.getPath()));
}
} else if (command.equals("Sibling project")) {
sendToSiblingProjectTask(selected_node);
} else {
Utils.log("ProjectTree.actionPerformed: don't know what to do with the command: " + command);
return;
}
} catch (Exception e) {
IJError.print(e);
}
}
});
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class DBLoader method getChildrenProjectThings.
private ArrayList<ProjectThing> getChildrenProjectThings(Project project, long parent_id, String parent_type, HashMap<String, TemplateThing> hs_tt, HashMap<Long, Displayable> hs_d) throws Exception {
final ArrayList<ProjectThing> al_children = new ArrayList<ProjectThing>();
ResultSet r = null;
if (-1 == parent_id)
Utils.log("parent_id = -1 for parent_type=" + parent_type);
if (parent_type.equals("profile_list")) {
// the project_id field is redundant
r = connection.prepareStatement("SELECT ab_things.* FROM ab_things,ab_displayables,ab_layers WHERE ab_things.parent_id=" + parent_id + " AND ab_things.object_id=ab_displayables.id AND ab_displayables.layer_id=ab_layers.id ORDER BY ab_layers.z,ab_things.id ASC").executeQuery();
} else {
r = connection.prepareStatement("SELECT * FROM ab_things WHERE parent_id=" + parent_id + " ORDER BY id").executeQuery();
}
while (r.next()) {
ProjectThing thing = getProjectThing(r, project, hs_tt, hs_d);
if (null != thing)
al_children.add(thing);
}
r.close();
return al_children;
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class DBLoader method getRootProjectThing.
/**
* Get all the Thing objects, recursively, for the root, and their corresponding encapsulated objects. Also, fills in the given ArrayList with all loaded Displayable objects.
*/
public ProjectThing getRootProjectThing(Project project, TemplateThing root_tt, TemplateThing project_tt, HashMap<Long, Displayable> hs_d) {
synchronized (db_lock) {
// connect if disconnected
if (!connectToDatabase()) {
return null;
}
// unpack root_tt (assumes TemplateThing objects have unique types, skips any repeated type to avoid problems in recursive things such as neurite_branch)
HashMap<String, TemplateThing> hs_tt = new HashMap<String, TemplateThing>();
unpack(root_tt, hs_tt);
ProjectThing root = null;
try {
// -1 signals root
ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND type='project' AND parent_id=-1").executeQuery();
if (r.next()) {
long id = r.getLong("id");
root = new ProjectThing(project_tt, project, id, project, getChildrenProjectThings(project, id, project_tt.getType(), hs_tt, hs_d));
}
r.close();
if (null == root) {
Utils.log("Loader.getRootProjectThing: can't find it for project id=" + project.getId());
return null;
}
} catch (Exception e) {
IJError.print(e);
return null;
}
return root;
}
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class DBLoader method restoreNodesExpandedState.
/**
* Affects only those set to true; the rest are left untouched.
*/
public void restoreNodesExpandedState(final Project project) {
// connect if disconnected
if (!connectToDatabase()) {
return;
}
try {
final ProjectTree ptree = project.getProjectTree();
final ResultSet r = connection.prepareStatement("SELECT id,expanded from ab_things where project_id=" + project.getId()).executeQuery();
while (r.next()) {
// "expanded");
boolean expanded = r.getBoolean(2);
if (expanded) {
// DNDTree.findNode(id, ptree);
Thing thing = project.find(r.getLong(1));
if (null != thing)
ptree.setExpandedSilently(thing, true);
}
}
ptree.updateUILater();
} catch (Exception e) {
IJError.print(e);
}
}
use of ini.trakem2.tree.Thing in project TrakEM2 by trakem2.
the class Render method renderObject.
/**
* Accepts a 'profile_list' Thing and composes a new Ob
*/
private void renderObject(Thing profile_list) {
// check preconditions
if (!profile_list.getType().equals("profile_list"))
return;
// do not accept an empty profile_list Thing
final ArrayList<? extends Thing> al = profile_list.getChildren();
if (null == al || al.size() < 2)
return;
// new style: follows profiles links and generates several obs, one per branch, ensuring that there is oly one profile per layer in the generated Ob for the .shapes file.
// 1 - gather all profiles
final HashSet<Profile> hs = new HashSet<Profile>();
for (final Thing child : al) {
Object ob = child.getObject();
if (ob instanceof Profile) {
hs.add((Profile) ob);
} else {
Utils.log2("Render: skipping non Profile class child");
}
}
String name = profile_list.getParent().getTitle();
final ArrayList<String> al_used_names = new ArrayList<String>();
// make unique object name, since it'll be the group
String name2 = name;
int k = 1;
while (ht_objects.containsKey(name2)) {
name2 = name + "-" + k;
k++;
}
name = name2;
al_used_names.add(name);
// 2 - start at the last found profile with the lowest Z, and recurse until done
// Utils.log2("Calling renderSubObjects with " + hs.size() + " profiles");
renderSubObjects(hs, al_used_names);
/* //old style, assumes a single profile per section
Profile[] profiles = new Profile[al.size()];
Iterator it = al.iterator();
int i = 0;
while (it.hasNext()) {
Thing child = (Thing)it.next();
Displayable displ = (Displayable)child.getObject();
profiles[i] = (Profile)displ; //this cast is safe (as long as I'm the only programmer and I remember that Thing objects added to a 'profile_list' Thing are of class Profile only)
i++;
}
// make unique object name, since it'll be the group
String name = profile_list.getParent().getTitle();
String name2 = name;
int k = 1;
while (ht_objects.containsKey(name2)) {
name2 = name + "_" + k;
k++;
}
name = name2;
// store
ht_objects.put(name, new Ob(name, profiles));
*/
}
Aggregations