use of ini.trakem2.persistence.DBLoader in project TrakEM2 by trakem2.
the class DBLoader method updateInDatabase.
/**
* The ImagePlus, if updated, is saved in the 'tiff_working' column always.
*/
private void updateInDatabase(Patch patch, String key) throws Exception {
if (key.equals("tiff_snapshot")) {
/* // DEPRECATED, now using mipmaps
InputStream i_stream = null;
try {
ImagePlus imp = new ImagePlus("s", snaps.get(patch.getId())); // not calling fetchSnapshot because old code could end in a loop.
if (null == imp) {
Utils.log2("DBLoader: snapshot ImagePlus is null!");
stmt_update_snap.setNull(1, java.sql.Types.BINARY);
} else {
i_stream = createZippedStream(imp);
stmt_update_snap.setBinaryStream(1, i_stream, i_stream.available());
flush(imp);
}
stmt_update_snap.setLong(2, patch.getId());
stmt_update_snap.executeUpdate();
} catch (Exception e) {
IJError.print(e);
} finally {
if (null != i_stream) try { i_stream.close(); } catch (Exception e1) { IJError.print(e1); }
}
*/
return;
}
StringBuffer sb = new StringBuffer("UPDATE ab_patches SET ");
boolean update_imp = false;
if (key.equals("tiff_working")) {
sb.append("imp_type=").append(patch.getType()).append(", tiff_working=?");
update_imp = true;
} else if (key.equals("remove_tiff_working")) {
sb.append("tiff_working=NULL");
} else if (key.equals("min_and_max")) {
sb.append("min=").append(patch.getMin()).append(", max=").append(patch.getMax());
} else {
// try the Displayable level
updateInDatabase((Displayable) patch, key);
return;
}
PreparedStatement st = connection.prepareStatement(sb.append(" WHERE id=").append(patch.getId()).toString());
int i = 1;
InputStream i_stream2 = null;
try {
if (update_imp) {
// WARNING if the cache is very small relative to the size of the images, this strategy may fail
ImagePlus imp = mawts.get(patch.getId());
i_stream2 = createZippedStream(imp);
st.setBinaryStream(i, i_stream2, i_stream2.available());
// defensive programming: if later I add any other ..
i++;
}
st.executeUpdate();
if (null != i_stream2)
i_stream2.close();
} catch (Exception e) {
IJError.print(e);
if (null != i_stream2)
try {
i_stream2.close();
} catch (Exception e2) {
IJError.print(e2);
}
}
}
use of ini.trakem2.persistence.DBLoader in project TrakEM2 by trakem2.
the class Project method destroy.
public boolean destroy() {
if (null == loader) {
return true;
}
if (loader.hasChanges() && !getBooleanProperty("no_shutdown_hook")) {
// DBLoader always returns false
if (ControlWindow.isGUIEnabled()) {
final YesNoDialog yn = ControlWindow.makeYesNoDialog("TrakEM2", "There are unsaved changes in project " + title + ". Save them?");
if (yn.yesPressed()) {
save();
}
} else {
Utils.log2("WARNING: closing project '" + title + "' with unsaved changes.");
}
}
try {
if (null != autosaving)
autosaving.cancel(true);
} catch (Throwable t) {
}
al_open_projects.remove(this);
// flush all memory
if (null != loader) {
// the last project is destroyed twice for some reason, if several are open. This is a PATCH
// and disconnect
loader.destroy();
loader = null;
}
if (null != layer_set)
layer_set.destroy();
// AFTER loader.destroy() call.
ControlWindow.remove(this);
if (null != template_tree)
template_tree.destroy();
if (null != project_tree)
project_tree.destroy();
if (null != layer_tree)
layer_tree.destroy();
Polyline.flushTraceCache(this);
// flag to mean: we're closing
this.template_tree = null;
// close all open Displays
Display.close(this);
Search.removeTabs(this);
synchronized (ptcache) {
ptcache.clear();
}
return true;
}
use of ini.trakem2.persistence.DBLoader in project TrakEM2 by trakem2.
the class Project method openDBProject.
/**
* Open a TrakEM2 project from the database. Queries the database for existing projects and if more than one, asks which one to open.
*/
public static Project openDBProject() {
if (Utils.wrongImageJVersion())
return null;
DBLoader loader = new DBLoader();
if (!loader.isReady())
return null;
// check connection
if (!loader.isConnected()) {
Utils.showMessage("Can't talk to database.");
loader.destroy();
return null;
}
// query the database for existing projects
Project[] projects = loader.getProjects();
if (null == projects) {
Utils.showMessage("Can't talk to database (null list of projects).");
loader.destroy();
return null;
}
Project project = null;
if (0 == projects.length) {
Utils.showMessage("No projects in this database.");
loader.destroy();
return null;
} else if (1 == projects.length) {
project = projects[0];
} else {
// ask to choose one
String[] titles = new String[projects.length];
for (int i = 0; i < projects.length; i++) {
titles[i] = projects[i].title;
}
GenericDialog gd = new GenericDialog("Choose");
gd.addMessage("Choose project to open:");
gd.addChoice("project: ", titles, titles[titles.length - 1]);
gd.showDialog();
if (gd.wasCanceled()) {
loader.destroy();
return null;
}
project = projects[gd.getNextChoiceIndex()];
}
// check if the selected project is open already
for (final Project p : al_open_projects) {
if (loader.isIdenticalProjectSource(p.loader) && p.id == project.id && p.title.equals(project.title)) {
Utils.showMessage("A project with title " + p.title + " and id " + p.id + " from the same database is already open.");
loader.destroy();
return null;
}
}
// now, open the selected project
// assign loader
project.loader = loader;
// grab the XML template
TemplateThing template_root = loader.getTemplateRoot(project);
if (null == template_root) {
Utils.showMessage("Failed to retrieve the template tree.");
project.destroy();
return null;
}
project.template_tree = new TemplateTree(project, template_root);
synchronized (project.ht_unique_tt) {
project.ht_unique_tt.clear();
project.ht_unique_tt.putAll(template_root.getUniqueTypes(new HashMap<String, TemplateThing>()));
}
// create the project Thing, to be root of the whole user Thing tree (and load all its objects)
// to collect all created displayables, and then reassign to the proper layers.
HashMap<Long, Displayable> hs_d = new HashMap<Long, Displayable>();
try {
// create a template for the project Thing
TemplateThing project_template = new TemplateThing("project");
project.ht_unique_tt.put("project", project_template);
project_template.addChild(template_root);
project.root_pt = loader.getRootProjectThing(project, template_root, project_template, hs_d);
// restore parent/child and attribute ownership and values (now that all Things exist)
project.root_pt.setup();
} catch (Exception e) {
Utils.showMessage("Failed to retrieve the Thing tree for the project.");
IJError.print(e);
project.destroy();
return null;
}
// create the user objects tree
project.project_tree = new ProjectTree(project, project.root_pt);
// restore the expanded state of each node
loader.restoreNodesExpandedState(project);
// create the layers templates
project.createLayerTemplates();
// fetch the root layer thing and the root layer set (will load all layers and layer sets, with minimal contents of patches; gets the basic objects -profile, pipe, etc.- from the project.root_pt). Will open all existing displays for each layer.
LayerThing root_layer_thing = null;
try {
root_layer_thing = loader.getRootLayerThing(project, project.root_pt, Project.layer_set_template, Project.layer_template);
if (null == root_layer_thing) {
project.destroy();
Utils.showMessage("Could not retrieve the root layer thing.");
return null;
}
// set the child/parent relationships now that everything exists
root_layer_thing.setup();
project.layer_set = (LayerSet) root_layer_thing.getObject();
if (null == project.layer_set) {
project.destroy();
Utils.showMessage("Could not retrieve the root layer set.");
return null;
}
// set the active layer to each ZDisplayable
project.layer_set.setup();
// debug:
// Utils.log2("$$$ root_lt: " + root_layer_thing + " ob: " + root_layer_thing.getObject().getClass().getName() + "\n children: " + ((LayerSet)root_layer_thing.getObject()).getLayers().size());
project.layer_tree = new LayerTree(project, root_layer_thing);
project.root_lt = root_layer_thing;
} catch (Exception e) {
Utils.showMessage("Failed to retrieve the Layer tree for the project.");
IJError.print(e);
project.destroy();
return null;
}
// 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);
// now open the displays that were stored for later, if any:
Display.openLater();
return project;
}
use of ini.trakem2.persistence.DBLoader in project TrakEM2 by trakem2.
the class ProjectThing method getPopupItems.
public JMenuItem[] getPopupItems(final ActionListener listener) {
JMenuItem item = null;
ArrayList<JMenuItem> al_items = new ArrayList<JMenuItem>();
JMenu menu = new JMenu("Add...");
// call the project unique type
final ArrayList<TemplateThing> tc = project.getTemplateThing(template.getType()).getChildren();
if (null != tc) {
for (final TemplateThing tt : tc) {
item = new JMenuItem("new " + tt.getType());
item.addActionListener(listener);
menu.add(item);
}
item = new JMenuItem("many...");
item.addActionListener(listener);
menu.add(item);
}
if (0 != menu.getItemCount()) {
if (template.getType().equals("profile_list") && null != al_children && al_children.size() > 0) {
// can't add a new profile unless linked to another one.
item.setEnabled(false);
}
al_items.add(menu);
}
// generic for all:
// a 'Show' command on a non-basic type is a render preview.
addPopupItem("Unhide", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, Event.ALT_MASK, true));
addPopupItem("Hide", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0, true));
addPopupItem("Info", listener, al_items);
addPopupItem("Rename...", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0, true));
// enable duplicating for basic types only
if (Project.isBasicType(getType())) {
addPopupItem("Duplicate", listener, al_items);
}
addPopupItem("Select in display", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true));
if (null != object && object instanceof Displayable) {
addPopupItem("Show centered in Display", listener, al_items);
}
if (null != object && object instanceof Tree<?>) {
addPopupItem("Show tabular view", listener, al_items);
}
// plugins
JMenuItem plugin_menu = Utils.addPlugIns("Project Tree", project, new Callable<Displayable>() {
public Displayable call() {
if (object instanceof Displayable)
return (Displayable) object;
return null;
}
});
if (null != plugin_menu)
al_items.add(plugin_menu);
addPopupItem("Measure", listener, al_items);
addPopupItem("Show in 3D", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, 0, true));
addPopupItem("Remove from 3D view", listener, al_items);
if (template.getType().equals("project")) {
if (project.getLoader() instanceof DBLoader) {
addPopupItem("Export project...", listener, al_items);
} else if (project.getLoader() instanceof FSLoader) {
addPopupItem("Save", listener, al_items).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true));
addPopupItem("Save as...", listener, al_items);
}
}
if (!(template.getType().equals("project") && project.getLoader() instanceof FSLoader)) {
addPopupItem("Delete...", listener, al_items);
}
JMenuItem[] items = new JMenuItem[al_items.size()];
al_items.toArray(items);
return items;
}
Aggregations