Search in sources :

Example 81 with Layer

use of ini.trakem2.display.Layer 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;
}
Also used : Ball(ini.trakem2.display.Ball) Connector(ini.trakem2.display.Connector) Node(ini.trakem2.display.Node) Profile(ini.trakem2.display.Profile) AreaTree(ini.trakem2.display.AreaTree) ReconstructArea(ini.trakem2.utils.ReconstructArea) Dissector(ini.trakem2.display.Dissector) LayerSet(ini.trakem2.display.LayerSet) LayerThing(ini.trakem2.tree.LayerThing) Color(java.awt.Color) Pipe(ini.trakem2.display.Pipe) AreaList(ini.trakem2.display.AreaList) Layer(ini.trakem2.display.Layer) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) Stack(ini.trakem2.display.Stack) Treeline(ini.trakem2.display.Treeline) DLabel(ini.trakem2.display.DLabel) Polyline(ini.trakem2.display.Polyline) Tag(ini.trakem2.display.Tag) Taggable(ini.trakem2.display.Taggable) Patch(ini.trakem2.display.Patch)

Example 82 with Layer

use of ini.trakem2.display.Layer in project TrakEM2 by trakem2.

the class TMLHandler method startElement.

public void startElement(String namespace_URI, String local_name, String qualified_name, Attributes attributes) throws SAXException {
    if (null == loader)
        return;
    // Utils.log2("startElement: " + qualified_name);
    this.counter++;
    if (0 == counter % 100) {
        // davi-experimenting: don't talk so much when you have > 600,000 patches to load!
        Utils.showStatus("Loading " + counter, false);
    }
    try {
        // failsafe:
        qualified_name = qualified_name.toLowerCase();
        final HashMap<String, String> ht_attributes = new HashMap<String, String>();
        for (int i = attributes.getLength() - 1; i > -1; i--) {
            ht_attributes.put(attributes.getQName(i).toLowerCase(), attributes.getValue(i));
        }
        // get the id, which whenever possible it's the id of the encapsulating Thing object. The encapsulated object id is the oid
        // The type is specified by the qualified_name
        Thing thing = null;
        if (0 == qualified_name.indexOf("t2_")) {
            if (qualified_name.equals("t2_display")) {
                // store for later, until the layers exist
                if (open_displays)
                    al_displays.add(ht_attributes);
            } else {
                // a Layer, LayerSet or Displayable object
                thing = makeLayerThing(qualified_name, ht_attributes);
                if (null != thing) {
                    if (null == root_lt && thing.getObject() instanceof LayerSet) {
                        root_lt = (LayerThing) thing;
                    }
                }
            }
        } else if (qualified_name.equals("project")) {
            if (null != this.root_pt) {
                Utils.log("WARNING: more than one project definitions.");
                return;
            }
            // Create the project
            this.project = new Project(Long.parseLong(ht_attributes.remove("id")), ht_attributes.remove("title"));
            // temp, but will be the same anyway
            this.project.setTempLoader(this.loader);
            this.project.parseXMLOptions(ht_attributes);
            // register id
            this.project.addToDatabase();
            String title = ht_attributes.get("title");
            if (null != title)
                this.project.setTitle(title);
            // Add all unique TemplateThing types to the project
            for (Iterator<TemplateThing> it = root_tt.getUniqueTypes(new HashMap<String, TemplateThing>()).values().iterator(); it.hasNext(); ) {
                this.project.addUniqueType(it.next());
            }
            this.project.addUniqueType(this.project_tt);
            this.root_pt = new ProjectThing(this.project_tt, this.project, this.project);
            // Add a project pointer to all template things
            this.root_tt.addToDatabase(this.project);
            thing = root_pt;
        } else if (qualified_name.startsWith("ict_transform") || qualified_name.startsWith("iict_transform")) {
            makeCoordinateTransform(qualified_name, ht_attributes);
        } else if (!qualified_name.equals("trakem2")) {
            // Any abstract object
            thing = makeProjectThing(qualified_name, ht_attributes);
        }
        if (null != thing) {
            // get the previously open thing and add this new_thing to it as a child
            int size = al_open.size();
            if (size > 0) {
                Thing parent = al_open.get(size - 1);
                parent.addChild(thing);
            // Utils.log2("Adding child " + thing + " to parent " + parent);
            }
            // add the new thing as open
            al_open.add(thing);
        }
    } catch (Exception e) {
        IJError.print(e);
        skip = true;
    }
}
Also used : Project(ini.trakem2.Project) HashMap(java.util.HashMap) LayerSet(ini.trakem2.display.LayerSet) Iterator(java.util.Iterator) LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) ProjectThing(ini.trakem2.tree.ProjectThing) Thing(ini.trakem2.tree.Thing) ProjectThing(ini.trakem2.tree.ProjectThing) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException)

Example 83 with Layer

use of ini.trakem2.display.Layer 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;
}
Also used : ArrayList(java.util.ArrayList) Layer(ini.trakem2.display.Layer)

Example 84 with Layer

use of ini.trakem2.display.Layer in project TrakEM2 by trakem2.

the class LayerThing method getPopupItems.

public JMenuItem[] getPopupItems(ActionListener listener) {
    JMenuItem item;
    ArrayList<JMenuItem> al_items = new ArrayList<JMenuItem>();
    JMenu menu = new JMenu("Add...");
    ArrayList<TemplateThing> tc = template.getChildren();
    if (null != tc) {
        for (Iterator<TemplateThing> it = tc.iterator(); it.hasNext(); ) {
            // changing underscores for spaces, for the 'layer_set' type to read nice
            item = new JMenuItem("new " + it.next().getType().replace('_', ' '));
            item.addActionListener(listener);
            menu.add(item);
        }
        if (template.getType().replaceAll("_", " ").equals("layer set")) {
            item = new JMenuItem("many new layers...");
            item.addActionListener(listener);
            menu.add(item);
        }
    }
    if (0 != menu.getItemCount()) {
        al_items.add(menu);
    }
    // Add a "Show" for all except the root LayerSet
    if (null != parent) {
        item = new JMenuItem("Show");
        item.addActionListener(listener);
        al_items.add(item);
    }
    if (template.getType().equals("layer")) {
        // adjust z and thickness
        item = new JMenuItem("Adjust...");
        item.addActionListener(listener);
        al_items.add(item);
    }
    item = new JMenuItem("Rename...");
    item.addActionListener(listener);
    al_items.add(item);
    if (template.getType().replaceAll("_", " ").equals("layer set")) {
        if (null != parent) {
            item = new JMenuItem("Show centered in Display");
            item.addActionListener(listener);
            al_items.add(item);
        }
        item = new JMenuItem("Resize LayerSet...");
        item.addActionListener(listener);
        al_items.add(item);
        LayerSet layer_set = (LayerSet) object;
        final boolean empty = 0 == layer_set.getLayers().size();
        item = new JMenuItem("Autoresize LayerSet");
        item.addActionListener(listener);
        al_items.add(item);
        if (empty)
            item.setEnabled(false);
        item = new JMenuItem("Translate layers in Z...");
        item.addActionListener(listener);
        al_items.add(item);
        if (empty)
            item.setEnabled(false);
        item = new JMenuItem("Reverse layer Z coords...");
        item.addActionListener(listener);
        al_items.add(item);
        if (empty)
            item.setEnabled(false);
        item = new JMenuItem("Reset layer Z and thickness");
        item.addActionListener(listener);
        al_items.add(item);
        if (empty)
            item.setEnabled(false);
        item = new JMenuItem("Search...");
        item.addActionListener(listener);
        al_items.add(item);
        if (empty)
            item.setEnabled(false);
    }
    item = new JMenuItem("Import stack...");
    // contains layers already, wouldn't know where to put it!
    if (template.getType().replaceAll("_", " ").equals("layer set") && 0 != ((LayerSet) object).getLayers().size())
        item.setEnabled(false);
    item.addActionListener(listener);
    al_items.add(item);
    if (template.getType().equals("layer")) {
        item = new JMenuItem("Import grid...");
        item.addActionListener(listener);
        al_items.add(item);
        item = new JMenuItem("Import sequence as grid...");
        item.addActionListener(listener);
        al_items.add(item);
        item = new JMenuItem("Import from text file...");
        item.addActionListener(listener);
        al_items.add(item);
    }
    // add a delete to all except the root LayerSet
    if (null != parent) {
        al_items.add(new JMenuItem(""));
        item = new JMenuItem("Delete...");
        item.addActionListener(listener);
        al_items.add(item);
    }
    JMenuItem[] items = new JMenuItem[al_items.size()];
    al_items.toArray(items);
    return items;
}
Also used : LayerSet(ini.trakem2.display.LayerSet) ArrayList(java.util.ArrayList) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Example 85 with Layer

use of ini.trakem2.display.Layer in project TrakEM2 by trakem2.

the class LayerTree method addLayer.

/**
 * Used by the Loader.importStack and the "many new layers" command.
 */
public void addLayer(LayerSet layer_set, Layer layer) {
    if (null == layer_set || null == layer)
        return;
    try {
        // find the node that contains the LayerSet
        DefaultMutableTreeNode root_node = (DefaultMutableTreeNode) this.getModel().getRoot();
        LayerThing root_lt = (LayerThing) root_node.getUserObject();
        Thing thing = null;
        if (root_lt.getObject().equals(layer_set))
            thing = root_lt;
        else
            thing = root_lt.findChild(layer_set);
        DefaultMutableTreeNode parent_node = DNDTree.findNode(thing, this);
        if (null == parent_node) {
            Utils.log("LayerTree: LayerSet not found.");
            return;
        }
        LayerThing parent_thing = (LayerThing) parent_node.getUserObject();
        double z = layer.getZ();
        // find the node whose 'z' is larger than z, and add the Layer before that.
        int n = parent_node.getChildCount();
        int i = 0;
        for (; i < n; i++) {
            DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) parent_node.getChildAt(i);
            LayerThing child_thing = (LayerThing) child_node.getUserObject();
            if (!child_thing.getType().equals("layer")) {
                continue;
            }
            double iz = ((Layer) child_thing.getObject()).getZ();
            if (iz < z) {
                continue;
            }
            // else, add the layer here, after the 'i' layer which has a larger z
            break;
        }
        TemplateThing tt = parent_thing.getChildTemplate("layer");
        if (null == tt) {
            Utils.log("LayerTree: Null template Thing!");
            return;
        }
        LayerThing new_thing = new LayerThing(tt, layer.getProject(), layer);
        // Add the new_thing to the tree
        if (null != new_thing) {
            parent_thing.addChild(new_thing);
            DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_thing);
            // TODO when changing the Z of a layer, the insertion is proper but an empty space is left //Utils.log("LayerTree: inserting at: " + i);
            ((DefaultTreeModel) this.getModel()).insertNodeInto(new_node, parent_node, i);
            TreePath treePath = new TreePath(new_node.getPath());
            this.scrollPathToVisible(treePath);
            this.setSelectionPath(treePath);
        }
    } catch (Exception e) {
        IJError.print(e);
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreePath(javax.swing.tree.TreePath) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) Layer(ini.trakem2.display.Layer)

Aggregations

Layer (ini.trakem2.display.Layer)61 ArrayList (java.util.ArrayList)52 Patch (ini.trakem2.display.Patch)43 Rectangle (java.awt.Rectangle)34 HashSet (java.util.HashSet)27 ImagePlus (ij.ImagePlus)26 Displayable (ini.trakem2.display.Displayable)25 AffineTransform (java.awt.geom.AffineTransform)23 GenericDialog (ij.gui.GenericDialog)22 Worker (ini.trakem2.utils.Worker)22 Point (mpicbg.models.Point)22 HashMap (java.util.HashMap)21 Point (java.awt.Point)19 Area (java.awt.geom.Area)19 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)18 File (java.io.File)16 Future (java.util.concurrent.Future)16 LayerSet (ini.trakem2.display.LayerSet)15 ExecutorService (java.util.concurrent.ExecutorService)14 Collection (java.util.Collection)13