use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.
the class StaleFiles method delete.
/**
* Generic method called by other methods of this class.
*/
public static final boolean delete(final Project project, final String topDir, final Path g) {
if (null == topDir)
return true;
// Collect the set of files to keep
final HashSet<String> keepers = new HashSet<String>();
for (final Layer l : project.getRootLayerSet().getLayers()) {
for (final Patch p : l.getAll(Patch.class)) {
String path = g.get(p);
if (null != path)
keepers.add(path);
}
}
// Iterate all directories, recursively
final String extension = g.extension();
final LinkedList<File> subdirs = new LinkedList<File>();
subdirs.add(new File(topDir));
final AtomicInteger counter = new AtomicInteger(0);
final ExecutorService exec = Utils.newFixedThreadPool(Math.max(2, Runtime.getRuntime().availableProcessors()), "Stale-file-remover");
while (!subdirs.isEmpty()) {
final File fdir = subdirs.removeFirst();
final String absPath = fdir.getAbsolutePath();
for (final String s : fdir.list()) {
final String path = absPath + "/" + s;
if (s.endsWith(extension)) {
if (keepers.contains(path))
continue;
// Else, delete the file, which is by definition stale
exec.submit(new Runnable() {
public void run() {
if (!new File(path).delete()) {
Utils.log2("Failed to delete: " + path);
counter.incrementAndGet();
}
}
});
} else {
final File f = new File(path);
if (f.isDirectory()) {
subdirs.add(f);
}
}
}
}
// Do not accept more tasks, but execute all submitted tasks
exec.shutdown();
// Wait maximum for an unreasonable amount of time
try {
exec.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
IJError.print(e);
}
if (counter.get() > 0) {
Utils.log("ERROR: failed to delete " + counter.get() + " files.\n See the stdout log for details.");
}
return 0 == counter.get();
}
use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.
the class TMLHandler method makeLayerThing.
private LayerThing makeLayerThing(String type, final HashMap<String, String> ht_attributes) {
try {
type = type.toLowerCase();
if (0 == type.indexOf("t2_")) {
type = type.substring(3);
}
// long id = -1;
// final String sid = ht_attributes.get("id");
// if (null != sid) id = Long.parseLong(sid);
long oid = -1;
final String soid = ht_attributes.get("oid");
if (null != soid)
oid = Long.parseLong(soid);
if (type.equals("node")) {
if (null == last_tree) {
throw new NullPointerException("Can't create a node for null last_tree!");
}
final Node<?> node = last_tree.newNode(ht_attributes);
taggables.add(node);
// Put node into the list of nodes with that layer id, to update to proper Layer pointer later
final long ndlid = Long.parseLong(ht_attributes.get("lid"));
List<Node<?>> list = node_layer_table.get(ndlid);
if (null == list) {
list = new ArrayList<Node<?>>();
node_layer_table.put(ndlid, list);
}
list.add(node);
// Set node as root node or add as child to last node in the stack
if (null == last_root_node) {
last_root_node = node;
} else {
final String sconf = ht_attributes.get("c");
nodes.getLast().add((Node) node, null == sconf ? Node.MAX_EDGE_CONFIDENCE : Byte.parseByte(sconf));
}
// color?
final String scolor = ht_attributes.get("color");
if (null != scolor) {
final Color color = Utils.getRGBColorFromHex(scolor);
Collection<Node<?>> nodes = node_colors.get(color);
if (null == nodes) {
nodes = new ArrayList<Node<?>>();
node_colors.put(color, nodes);
}
nodes.add(node);
}
// Put node into stack of nodes (to be removed on closing the tag)
nodes.add(node);
} else if (type.equals("profile")) {
Profile profile = new Profile(this.project, oid, ht_attributes, ht_links);
profile.addToDatabase();
ht_displayables.put(oid, profile);
addToLastOpenLayer(profile);
last_displayable = profile;
return null;
} else if (type.equals("pipe")) {
Pipe pipe = new Pipe(this.project, oid, ht_attributes, ht_links);
pipe.addToDatabase();
ht_displayables.put(new Long(oid), pipe);
ht_zdispl.put(new Long(oid), pipe);
last_displayable = pipe;
addToLastOpenLayerSet(pipe);
return null;
} else if (type.equals("polyline")) {
Polyline pline = new Polyline(this.project, oid, ht_attributes, ht_links);
pline.addToDatabase();
last_displayable = pline;
ht_displayables.put(new Long(oid), pline);
ht_zdispl.put(new Long(oid), pline);
addToLastOpenLayerSet(pline);
return null;
} else if (type.equals("connector")) {
final Connector con = new Connector(this.project, oid, ht_attributes, ht_links);
if (ht_attributes.containsKey("origin")) {
legacy.add(new Runnable() {
public void run() {
con.readLegacyXML(al_layer_sets.get(al_layer_sets.size() - 1), ht_attributes, ht_links);
}
});
}
con.addToDatabase();
last_connector = con;
last_tree = con;
last_displayable = con;
ht_displayables.put(new Long(oid), con);
ht_zdispl.put(new Long(oid), con);
addToLastOpenLayerSet(con);
return null;
} else if (type.equals("path")) {
if (null != reca) {
reca.add(ht_attributes.get("d"));
return null;
}
return null;
} else if (type.equals("area")) {
reca = new ReconstructArea();
if (null != last_area_list) {
last_area_list_layer_id = Long.parseLong(ht_attributes.get("layer_id"));
}
return null;
} else if (type.equals("area_list")) {
AreaList area = new AreaList(this.project, oid, ht_attributes, ht_links);
// why? This looks like an onion
area.addToDatabase();
last_area_list = area;
last_displayable = area;
ht_displayables.put(new Long(oid), area);
ht_zdispl.put(new Long(oid), area);
addToLastOpenLayerSet(area);
return null;
} else if (type.equals("tag")) {
Taggable t = taggables.getLast();
if (null != t) {
Object ob = ht_attributes.get("key");
// defaults to 't'
int keyCode = KeyEvent.VK_T;
// KeyEvent.VK_U is char U, not u
if (null != ob)
keyCode = (int) ((String) ob).toUpperCase().charAt(0);
Tag tag = al_layer_sets.get(al_layer_sets.size() - 1).putTag(ht_attributes.get("name"), keyCode);
// could be null if name is not found
if (null != tag)
t.addTag(tag);
}
} else if (type.equals("ball_ob")) {
// add a ball to the last open Ball
if (null != last_ball) {
last_ball.addBall(Double.parseDouble(ht_attributes.get("x")), Double.parseDouble(ht_attributes.get("y")), Double.parseDouble(ht_attributes.get("r")), Long.parseLong(ht_attributes.get("layer_id")));
}
return null;
} else if (type.equals("ball")) {
Ball ball = new Ball(this.project, oid, ht_attributes, ht_links);
ball.addToDatabase();
last_ball = ball;
last_displayable = ball;
ht_displayables.put(new Long(oid), ball);
ht_zdispl.put(new Long(oid), ball);
addToLastOpenLayerSet(ball);
return null;
} else if (type.equals("stack")) {
Stack stack = new Stack(this.project, oid, ht_attributes, ht_links);
stack.addToDatabase();
last_stack = stack;
last_displayable = stack;
ht_displayables.put(new Long(oid), stack);
ht_zdispl.put(new Long(oid), stack);
addToLastOpenLayerSet(stack);
} else if (type.equals("treeline")) {
Treeline tline = new Treeline(this.project, oid, ht_attributes, ht_links);
tline.addToDatabase();
last_treeline = tline;
last_tree = tline;
last_treeline_data = new StringBuilder();
last_displayable = tline;
ht_displayables.put(oid, tline);
ht_zdispl.put(oid, tline);
addToLastOpenLayerSet(tline);
} else if (type.equals("areatree")) {
AreaTree art = new AreaTree(this.project, oid, ht_attributes, ht_links);
art.addToDatabase();
last_areatree = art;
last_tree = art;
last_displayable = art;
ht_displayables.put(oid, art);
ht_zdispl.put(oid, art);
addToLastOpenLayerSet(art);
} else if (type.equals("dd_item")) {
if (null != last_dissector) {
last_dissector.addItem(Integer.parseInt(ht_attributes.get("tag")), Integer.parseInt(ht_attributes.get("radius")), ht_attributes.get("points"));
}
} else if (type.equals("label")) {
DLabel label = new DLabel(project, oid, ht_attributes, ht_links);
label.addToDatabase();
ht_displayables.put(new Long(oid), label);
addToLastOpenLayer(label);
last_displayable = label;
return null;
} else if (type.equals("annot")) {
last_annotation = new StringBuilder();
return null;
} else if (type.equals("patch")) {
Patch patch = new Patch(project, oid, ht_attributes, ht_links);
patch.addToDatabase();
ht_displayables.put(new Long(oid), patch);
addToLastOpenLayer(patch);
last_patch = patch;
last_displayable = patch;
checkAlphaMasks(patch);
return null;
} else if (type.equals("filter")) {
last_patch_filters.add(newFilter(ht_attributes));
} else if (type.equals("dissector")) {
Dissector dissector = new Dissector(this.project, oid, ht_attributes, ht_links);
dissector.addToDatabase();
last_dissector = dissector;
last_displayable = dissector;
ht_displayables.put(new Long(oid), dissector);
ht_zdispl.put(new Long(oid), dissector);
addToLastOpenLayerSet(dissector);
} else if (type.equals("layer")) {
// find last open LayerSet, if any
for (int i = al_layer_sets.size() - 1; i > -1; ) {
LayerSet set = al_layer_sets.get(i);
Layer layer = new Layer(project, oid, ht_attributes);
layer.addToDatabase();
set.addSilently(layer);
al_layers.add(layer);
Object ot = ht_attributes.get("title");
return new LayerThing(template_layer_thing, project, -1, (null == ot ? null : (String) ot), layer, null);
}
} else if (type.equals("layer_set")) {
LayerSet set = new LayerSet(project, oid, ht_attributes, ht_links);
last_displayable = set;
set.addToDatabase();
ht_displayables.put(new Long(oid), set);
al_layer_sets.add(set);
addToLastOpenLayer(set);
Object ot = ht_attributes.get("title");
return new LayerThing(template_layer_set_thing, project, -1, (null == ot ? null : (String) ot), set, null);
} else if (type.equals("calibration")) {
// find last open LayerSet if any
for (int i = al_layer_sets.size() - 1; i > -1; ) {
LayerSet set = al_layer_sets.get(i);
set.restoreCalibration(ht_attributes);
return null;
}
} else if (type.equals("prop")) {
// Add property to last created Displayable
if (null != last_displayable) {
last_displayable.setProperty(ht_attributes.get("key"), ht_attributes.get("value"));
}
} else if (type.equals("linked_prop")) {
// Add linked property to last created Displayable. Has to wait until the Displayable ids have been resolved to instances.
if (null != last_displayable) {
putLinkedProperty(last_displayable, ht_attributes);
}
} else {
Utils.log2("TMLHandler Unknown type: " + type);
}
} catch (Exception e) {
IJError.print(e);
}
// default:
return null;
}
use of ini.trakem2.display.Patch 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.display.Patch 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.Patch 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);
}
Aggregations