use of ini.trakem2.display.Displayable 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.display.Displayable 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.display.Displayable in project TrakEM2 by trakem2.
the class DBLoader method fetchLayer.
/**
* Load all objects into the Layer: Profile and Pipe from the hs_pt (full of ProjectThing wrapping them), and Patch, LayerSet, DLabel, etc from the database.
*/
private Layer fetchLayer(Project project, long id, HashMap hs_pt) throws Exception {
ResultSet r = connection.prepareStatement("SELECT * FROM ab_layers WHERE id=" + id).executeQuery();
Layer layer = null;
if (r.next()) {
long layer_id = r.getLong("id");
layer = new Layer(project, layer_id, r.getDouble("z"), r.getDouble("thickness"));
// find the Layer's parent
long parent_id = r.getLong("layer_set_id");
Object set = hs_pt.get(new Long(parent_id));
if (null != set) {
((LayerSet) set).addSilently(layer);
} else {
Utils.log("Loader.fetchLayer: WARNING no parent for layer " + layer);
}
// add the displayables from hs_pt that correspond to this layer (and all other objects that belong to the layer)
HashMap hs_d = new HashMap();
ResultSet rd = connection.prepareStatement("SELECT ab_displayables.id, ab_profiles.id, layer_id, stack_index FROM ab_displayables,ab_profiles WHERE ab_displayables.id=ab_profiles.id AND layer_id=" + layer_id).executeQuery();
while (rd.next()) {
Long idd = new Long(rd.getLong("id"));
Object ob = hs_pt.get(idd);
// Utils.log("Found profile with id=" + idd + " and ob = " + ob);
if (null != ob) {
hs_d.put(new Integer(rd.getInt("stack_index")), ob);
}
}
rd.close();
// fetch LayerSet objects (which are also Displayable), and put them in the hs_pt (this is hackerous)
ResultSet rls = connection.prepareStatement("SELECT * FROM ab_layer_sets, ab_displayables WHERE ab_layer_sets.id=ab_displayables.id AND ab_layer_sets.parent_layer_id=" + id).executeQuery();
while (rls.next()) {
long ls_id = rls.getLong("id");
LayerSet layer_set = new LayerSet(project, ls_id, rls.getString("title"), (float) rls.getDouble("width"), (float) rls.getDouble("height"), rls.getDouble("rot_x"), rls.getDouble("rot_y"), rls.getDouble("rot_z"), (float) rls.getDouble("layer_width"), (float) rls.getDouble("layer_height"), rls.getBoolean("locked"), rls.getInt("snapshots_mode"), new AffineTransform(rls.getDouble("m00"), rls.getDouble("m10"), rls.getDouble("m01"), rls.getDouble("m11"), rls.getDouble("m02"), rls.getDouble("m12")));
hs_pt.put(new Long(ls_id), layer_set);
hs_d.put(new Integer(rls.getInt("stack_index")), layer_set);
layer_set.setLayer(layer, false);
// find the pipes (or other possible ZDisplayable objects) in the hs_pt that belong to this LayerSet and add them silently
ResultSet rpi = connection.prepareStatement("SELECT ab_displayables.id, ab_zdisplayables.id, layer_id, layer_set_id, stack_index FROM ab_displayables,ab_zdisplayables WHERE ab_displayables.id=ab_zdisplayables.id AND layer_set_id=" + ls_id + " ORDER BY stack_index ASC").executeQuery();
while (rpi.next()) {
Long idd = new Long(rpi.getLong("id"));
Object ob = hs_pt.get(idd);
if (null != ob && ob instanceof ZDisplayable) {
layer_set.addSilently((ZDisplayable) ob);
} else {
Utils.log("fetchLayer: failed to add a ZDisplayable to the layer_set. zdispl id = " + idd);
}
}
rpi.close();
}
rls.close();
// add Patch objects from ab_patches joint-called with ab_displayables
ResultSet rp = connection.prepareStatement("SELECT ab_patches.id, ab_displayables.id, layer_id, title, width, height, stack_index, imp_type, locked, min, max, m00, m10, m01, m11, m02, m12 FROM ab_patches,ab_displayables WHERE ab_patches.id=ab_displayables.id AND ab_displayables.layer_id=" + layer_id).executeQuery();
while (rp.next()) {
long patch_id = rp.getLong("id");
Patch patch = new Patch(project, patch_id, rp.getString("title"), (float) rp.getDouble("width"), (float) rp.getDouble("height"), rp.getInt("o_width"), rp.getInt("o_height"), rp.getInt("imp_type"), rp.getBoolean("locked"), rp.getDouble("min"), rp.getDouble("max"), new AffineTransform(rp.getDouble("m00"), rp.getDouble("m10"), rp.getDouble("m01"), rp.getDouble("m11"), rp.getDouble("m02"), rp.getDouble("m12")));
// collecting all Displayable objects to reconstruct links
hs_pt.put(new Long(patch_id), patch);
hs_d.put(new Integer(rp.getInt("stack_index")), patch);
}
rp.close();
// add DLabel objects
ResultSet rl = connection.prepareStatement("SELECT ab_labels.id, ab_displayables.id, layer_id, title, width, height, m00, m10, m01, m11, m02, m12, stack_index, font_name, font_style, font_size, ab_labels.type, locked FROM ab_labels,ab_displayables WHERE ab_labels.id=ab_displayables.id AND ab_displayables.layer_id=" + layer_id).executeQuery();
while (rl.next()) {
long label_id = rl.getLong("id");
DLabel label = new DLabel(project, label_id, rl.getString("title"), (float) rl.getDouble("width"), (float) rl.getDouble("height"), rl.getInt("type"), rl.getString("font_name"), rl.getInt("font_style"), rl.getInt("font_size"), rl.getBoolean("locked"), new AffineTransform(rl.getDouble("m00"), rl.getDouble("m10"), rl.getDouble("m01"), rl.getDouble("m11"), rl.getDouble("m02"), rl.getDouble("m12")));
// collecting all Displayable objects to reconstruct links
hs_pt.put(new Long(label_id), label);
hs_d.put(new Integer(rl.getInt("stack_index")), label);
}
rl.close();
// Add silently to the Layer ordered by stack index
Set e = hs_d.keySet();
Object[] si = new Object[hs_d.size()];
si = e.toArray(si);
// will it sort an array of integers correctly? Who knows!
Arrays.sort(si);
for (int i = 0; i < si.length; i++) {
// Utils.log("Loader layer.addSilently: adding " + (DBObject)hs_d.get(si[i]));
layer.addSilently((DBObject) hs_d.get(si[i]));
}
// find displays and open later, when fully loaded.
ResultSet rdi = connection.prepareStatement("SELECT * FROM ab_displays WHERE layer_id=" + layer.getId()).executeQuery();
while (rdi.next()) {
fetchDisplay(rdi, layer);
}
rdi.close();
}
r.close();
return layer;
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class DBLoader method addToDatabase.
/* Patch methods ****************************************************************/
/**
* Adds the given Patch and its ImagePlus as a new row, filling in the 'tiff_original' column with the zipped ImagePlus.
*/
private void addToDatabase(Patch patch) throws Exception {
InputStream i_stream = null;
try {
ImagePlus imp = mawts.get(patch.getId());
// PreparedStatement st = connection.prepareStatement(new StringBuffer("INSERT INTO ab_patches (id, imp_type, tiff_original) VALUES (").append(patch.getId()).append(',').append(imp.getType()).append(",?)").toString());
stmt_add_patch.setLong(1, patch.getId());
stmt_add_patch.setInt(2, imp.getType());
i_stream = createZippedStream(imp);
if (null == i_stream) {
Utils.log("Loader.addToDatabase(Patch): null stream.");
return;
}
stmt_add_patch.setBinaryStream(3, i_stream, i_stream.available());
stmt_add_patch.setDouble(4, patch.getMin());
stmt_add_patch.setDouble(5, patch.getMax());
stmt_add_patch.executeUpdate();
i_stream.close();
} catch (Exception e) {
if (null != i_stream)
try {
i_stream.close();
} catch (Exception ie) {
IJError.print(ie);
}
Utils.showMessage("Could not add Patch image.");
IJError.print(e);
return;
}
// finally:
addToDatabase((Displayable) patch);
}
use of ini.trakem2.display.Displayable 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