Search in sources :

Example 51 with ProjectThing

use of ini.trakem2.tree.ProjectThing in project TrakEM2 by trakem2.

the class ProjectTree method rawSendToSiblingProject.

/**
 * Assumes that both projects have the same TemplateThing structure,
 * and assumes that the parent of the ({@code source_pt} and the {@code landing_parent}
 * instances are of the same type.
 *
 * @param source_pt The {@link ProjectThing} to be cloned.
 * @param transfer_mode Either 0 ("As is") or 1 ("Transformed with the images").
 * @param target_project The sibling project into which insert a clone of the {@code source_pt}.
 * @param landing_parent The ProjectThing in the sibling project that receives the cloned {@code source_pt}.
 */
public boolean rawSendToSiblingProject(// the source ProjectThing to copy to the target project
final ProjectThing source_pt, final int transfer_mode, final Project target_project, final ProjectThing landing_parent) {
    try {
        // Check that all the Layers used by the objects to transfer also exist in the target project!
        // 1 - Cheap way: check if all layers in the target project exist in the source project, by id
        HashSet<Long> lids = new HashSet<Long>();
        for (final Layer layer : this.project.getRootLayerSet().getLayers()) {
            lids.add(layer.getId());
        }
        HashSet<Long> tgt_lids = new HashSet<Long>(lids);
        for (final Layer layer : target_project.getRootLayerSet().getLayers()) {
            lids.remove(layer.getId());
            tgt_lids.add(layer.getId());
        }
        List<Displayable> original_vdata = null;
        final Set<Long> lids_to_operate = new HashSet<Long>();
        if (0 != lids.size()) {
            original_vdata = new ArrayList<Displayable>();
            // All their layers MUST be in the target project.
            for (final ProjectThing child : source_pt.findChildrenOfTypeR(Displayable.class)) {
                final Displayable d = (Displayable) child.getObject();
                if (!tgt_lids.containsAll(d.getLayerIds())) {
                    Utils.log("CANNOT transfer: not all required layers are present in the target project!\n  First object that couldn't be transfered: \n    " + d);
                    return false;
                }
                if (d instanceof VectorData) {
                    original_vdata.add(d);
                    lids_to_operate.addAll(d.getLayerIds());
                }
            }
        }
        // Deep cloning of the ProjectThing to transfer, then added to the landing_parent in the other tree.
        ProjectThing copy;
        try {
            // new ids, taken from target_project
            copy = source_pt.deepClone(target_project, false);
        } catch (Exception ee) {
            Utils.logAll("Can't send: " + ee.getMessage());
            IJError.print(ee);
            return false;
        }
        if (null == landing_parent.getChildTemplate(copy.getTemplate().getType())) {
            // ensure a copy is there
            landing_parent.getTemplate().addChild(copy.getTemplate().shallowCopy());
        }
        if (!landing_parent.addChild(copy)) {
            Utils.log("Could NOT transfer the node!");
            return false;
        }
        // Get the list of Profile instances in the source Project, in the same order
        // that they will be in the target project:
        final List<Profile> srcProfiles = new ArrayList<Profile>();
        for (final ProjectThing profile_pt : source_pt.findChildrenOfTypeR(Profile.class)) {
            srcProfiles.add((Profile) profile_pt.getObject());
        }
        final List<ProjectThing> copies = copy.findChildrenOfTypeR(Displayable.class);
        final List<Profile> newProfiles = new ArrayList<Profile>();
        // Utils.log2("copies size: " + copies.size());
        final List<Displayable> vdata = new ArrayList<Displayable>();
        final List<ZDisplayable> zd = new ArrayList<ZDisplayable>();
        for (final ProjectThing t : copies) {
            final Displayable d = (Displayable) t.getObject();
            // all should be, this is just future-proof code.
            if (d instanceof VectorData)
                vdata.add(d);
            if (d instanceof ZDisplayable) {
                zd.add((ZDisplayable) d);
            } else {
                // profile: always special
                newProfiles.add((Profile) d);
            }
        }
        // Fix Profile instances: exploit that the order as been conserved when copying.
        int profileIndex = 0;
        for (final Profile newProfile : newProfiles) {
            // Corresponding Profile:
            final Profile srcProfile = srcProfiles.get(profileIndex++);
            // Corresponding layer: layers have the same IDs by definition of what a sibling Project is.
            final Layer newLayer = target_project.getRootLayerSet().getLayer(srcProfile.getLayer().getId());
            newLayer.add(newProfile);
            // Corresponding links
            for (final Displayable srcLinkedProfile : srcProfile.getLinked(Profile.class)) {
                newProfile.link(newProfiles.get(srcProfiles.indexOf(srcLinkedProfile)));
            }
        }
        // add them all in one shot
        target_project.getRootLayerSet().addAll(zd);
        // could have changed
        target_project.getTemplateTree().rebuild();
        // When trying to rebuild just the landing_parent, it doesn't always work. Needs checking TODO
        target_project.getProjectTree().rebuild();
        // Open up the path to the landing parent node
        final TreePath tp = new TreePath(DNDTree.findNode(landing_parent, target_project.getProjectTree()).getPath());
        Utils.invokeLater(new Runnable() {

            public void run() {
                target_project.getProjectTree().scrollPathToVisible(tp);
                target_project.getProjectTree().setSelectionPath(tp);
            }
        });
        if (1 == transfer_mode) {
            // Collect original vdata
            if (null == original_vdata) {
                original_vdata = new ArrayList<Displayable>();
                for (final ProjectThing child : source_pt.findChildrenOfTypeR(Displayable.class)) {
                    final Displayable d = (Displayable) child.getObject();
                    if (d instanceof VectorData) {
                        original_vdata.add(d);
                        lids_to_operate.addAll(d.getLayerIds());
                    }
                }
            }
            // Utils.log2("original vdata:", original_vdata);
            // Utils.log2("vdata:", vdata);
            // Transform with images
            AlignTask.transformVectorData(AlignTask.createTransformPropertiesTable(original_vdata, vdata, lids_to_operate), vdata, target_project.getRootLayerSet());
        }
        return true;
    } catch (Exception e) {
        IJError.print(e);
    }
    return false;
}
Also used : Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) ArrayList(java.util.ArrayList) Layer(ini.trakem2.display.Layer) VectorData(ini.trakem2.display.VectorData) Profile(ini.trakem2.display.Profile) ZDisplayable(ini.trakem2.display.ZDisplayable) TreePath(javax.swing.tree.TreePath) HashSet(java.util.HashSet)

Example 52 with ProjectThing

use of ini.trakem2.tree.ProjectThing 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;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 53 with ProjectThing

use of ini.trakem2.tree.ProjectThing 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;
    }
}
Also used : HashMap(java.util.HashMap) TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) ProjectThing(ini.trakem2.tree.ProjectThing) SQLException(java.sql.SQLException)

Example 54 with ProjectThing

use of ini.trakem2.tree.ProjectThing 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;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) LayerSet(ini.trakem2.display.LayerSet) LayerSet(ini.trakem2.display.LayerSet) HashMap(java.util.HashMap) Layer(ini.trakem2.display.Layer) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint) ZDisplayable(ini.trakem2.display.ZDisplayable) DLabel(ini.trakem2.display.DLabel) ResultSet(java.sql.ResultSet) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch)

Aggregations

ProjectThing (ini.trakem2.tree.ProjectThing)27 DBObject (ini.trakem2.persistence.DBObject)17 HashMap (java.util.HashMap)17 ZDisplayable (ini.trakem2.display.ZDisplayable)16 Displayable (ini.trakem2.display.Displayable)15 ArrayList (java.util.ArrayList)12 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)11 HashSet (java.util.HashSet)10 TreePath (javax.swing.tree.TreePath)10 Project (ini.trakem2.Project)9 TemplateThing (ini.trakem2.tree.TemplateThing)8 Layer (ini.trakem2.display.Layer)7 LayerSet (ini.trakem2.display.LayerSet)7 Map (java.util.Map)7 LayerThing (ini.trakem2.tree.LayerThing)6 GenericDialog (ij.gui.GenericDialog)5 Profile (ini.trakem2.display.Profile)5 TreeMap (java.util.TreeMap)5 ImagePlus (ij.ImagePlus)4 Display (ini.trakem2.display.Display)3