Search in sources :

Example 51 with Project

use of ini.trakem2.Project in project TrakEM2 by trakem2.

the class Merger method createTable.

private static JTable createTable(final HashSet<ZDisplayable> hs, final String column_title, final Project p1, final Project p2) {
    final TwoColumnModel tcm = new TwoColumnModel(hs, column_title);
    final JTable table = new JTable(tcm);
    table.setDefaultRenderer(table.getColumnClass(0), new DefaultTableCellRenderer() {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (1 == column && tcm.sent[row]) {
                c.setBackground(Color.green);
                c.setForeground(Color.white);
            } else if (isSelected) {
                c.setForeground(table.getSelectionForeground());
                c.setBackground(table.getSelectionBackground());
            } else {
                c.setBackground(Color.white);
                c.setForeground(Color.black);
            }
            return c;
        }
    });
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent me) {
            final JTable src = (JTable) me.getSource();
            final TwoColumnModel model = (TwoColumnModel) src.getModel();
            final int row = src.rowAtPoint(me.getPoint()), col = src.columnAtPoint(me.getPoint());
            if (2 == me.getClickCount()) {
                Object ob = model.getValueAt(row, col);
                if (ob instanceof ZDisplayable) {
                    ZDisplayable zd = (ZDisplayable) ob;
                    Display df = Display.getOrCreateFront(zd.getProject());
                    // also select
                    df.show(zd.getFirstLayer(), zd, true, false);
                }
            } else if (me.isPopupTrigger()) {
                JPopupMenu popup = new JPopupMenu();
                final JMenuItem send = new JMenuItem("Send selection");
                popup.add(send);
                send.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        ArrayList<ZDisplayable> col = new ArrayList<ZDisplayable>();
                        for (final int i : src.getSelectedRows()) {
                            col.add((ZDisplayable) model.getValueAt(i, 0));
                        }
                        if (col.isEmpty())
                            return;
                        // the other
                        Project target = col.get(0).getProject() == p1 ? p2 : p1;
                        LayerSet ls = target.getRootLayerSet();
                        ArrayList<ZDisplayable> copies = new ArrayList<ZDisplayable>();
                        for (ZDisplayable zd : col) {
                            copies.add((ZDisplayable) zd.clone(target, false));
                            model.sent[row] = true;
                        }
                        // 1. To the LayerSet:
                        ls.addAll(copies);
                        // 2. To the ProjectTree:
                        target.getProjectTree().insertSegmentations(copies);
                        // Update:
                        model.fireTableDataChanged();
                    }
                });
                popup.show(table, me.getX(), me.getY());
            }
        }
    });
    return table;
}
Also used : MouseEvent(java.awt.event.MouseEvent) LayerSet(ini.trakem2.display.LayerSet) ActionEvent(java.awt.event.ActionEvent) MouseAdapter(java.awt.event.MouseAdapter) ArrayList(java.util.ArrayList) JPopupMenu(javax.swing.JPopupMenu) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) ZDisplayable(ini.trakem2.display.ZDisplayable) Project(ini.trakem2.Project) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) Component(java.awt.Component) JMenuItem(javax.swing.JMenuItem) Display(ini.trakem2.display.Display)

Example 52 with Project

use of ini.trakem2.Project in project TrakEM2 by trakem2.

the class Merger method compare.

/**
 * Take two projects and find out what is different among them,
 *  independent of id.
 */
public static final void compare(final Project p1, final Project p2) {
    Utils.log("Be warned: only Treeline, AreaTree and Connector are considered at the moment.");
    final LayerSet ls1 = p1.getRootLayerSet(), ls2 = p2.getRootLayerSet();
    final Collection<ZDisplayable> zds1 = ls1.getZDisplayables(), zds2 = ls2.getZDisplayables();
    final HashSet<Class<?>> accepted = new HashSet<Class<?>>();
    accepted.add(Treeline.class);
    accepted.add(AreaTree.class);
    accepted.add(Connector.class);
    final HashMap<Displayable, List<Change>> matched = new HashMap<Displayable, List<Change>>();
    final HashSet<ZDisplayable> empty1 = new HashSet<ZDisplayable>(), empty2 = new HashSet<ZDisplayable>();
    final HashSet<ZDisplayable> unmatched1 = new HashSet<ZDisplayable>(), unmatched2 = new HashSet<ZDisplayable>(zds2);
    // Remove instances of classes not accepted
    for (final Iterator<ZDisplayable> it = unmatched2.iterator(); it.hasNext(); ) {
        ZDisplayable zd = it.next();
        if (!accepted.contains(zd.getClass())) {
            it.remove();
            continue;
        }
        if (zd.isDeletable()) {
            it.remove();
            empty2.add(zd);
        }
    }
    zds2.removeAll(empty2);
    final AtomicInteger counter = new AtomicInteger(0);
    // or at least one or more that are similar in that they have some nodes in common.
    try {
        ini.trakem2.parallel.Process.unbound(zds1, new TaskFactory<ZDisplayable, Object>() {

            @Override
            public Object process(final ZDisplayable zd1) {
                Utils.showProgress(counter.getAndIncrement() / (float) zds1.size());
                if (!accepted.contains(zd1.getClass())) {
                    Utils.log("Ignoring: [A] " + zd1);
                    return null;
                }
                if (zd1.isDeletable()) {
                    synchronized (empty1) {
                        empty1.add(zd1);
                    }
                    return null;
                }
                final List<Change> cs = new ArrayList<Change>();
                for (final ZDisplayable zd2 : zds2) {
                    // Same class?
                    if (zd1.getClass() != zd2.getClass())
                        continue;
                    if (zd1 instanceof Tree<?> && zd2 instanceof Tree<?>) {
                        Change c = compareTrees(zd1, zd2);
                        if (c.hasSimilarNodes()) {
                            cs.add(c);
                            if (1 == cs.size()) {
                                synchronized (matched) {
                                    matched.put(zd1, cs);
                                }
                            }
                            synchronized (unmatched2) {
                                unmatched2.remove(zd2);
                            }
                        }
                        // debug
                        if (zd1.getId() == zd2.getId()) {
                            Utils.log("zd1 #" + zd1.getId() + " is similar to #" + zd2.getId() + ": " + c.hasSimilarNodes());
                        }
                    }
                }
                if (cs.isEmpty()) {
                    synchronized (unmatched1) {
                        unmatched1.add(zd1);
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        IJError.print(e);
    }
    // reset
    Utils.showProgress(1);
    Utils.log("matched.size(): " + matched.size());
    makeGUI(p1, p2, empty1, empty2, matched, unmatched1, unmatched2);
}
Also used : ZDisplayable(ini.trakem2.display.ZDisplayable) Displayable(ini.trakem2.display.Displayable) LayerSet(ini.trakem2.display.LayerSet) HashMap(java.util.HashMap) ZDisplayable(ini.trakem2.display.ZDisplayable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tree(ini.trakem2.display.Tree) AreaTree(ini.trakem2.display.AreaTree) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 53 with Project

use of ini.trakem2.Project in project TrakEM2 by trakem2.

the class ProjectTree method getPopupMenu.

/**
 * Get a custom, context-sensitive popup menu for the selected node.
 */
private JPopupMenu getPopupMenu(DefaultMutableTreeNode node) {
    Object ob = node.getUserObject();
    ProjectThing thing = null;
    if (ob instanceof ProjectThing) {
        thing = (ProjectThing) ob;
    } else {
        return null;
    }
    // context-sensitive popup
    JMenuItem[] items = thing.getPopupItems(this);
    if (0 == items.length)
        return null;
    JPopupMenu popup = new JPopupMenu();
    for (int i = 0; i < items.length; i++) {
        popup.add(items[i]);
    }
    JMenu node_menu = new JMenu("Node");
    JMenuItem item = new JMenuItem("Move up");
    item.addActionListener(this);
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0, true));
    node_menu.add(item);
    item = new JMenuItem("Move down");
    item.addActionListener(this);
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0, true));
    node_menu.add(item);
    item = new JMenuItem("Collapse nodes of children nodes");
    item.addActionListener(this);
    node_menu.add(item);
    popup.add(node_menu);
    JMenu send_menu = new JMenu("Send to");
    item = new JMenuItem("Sibling project");
    item.addActionListener(this);
    send_menu.add(item);
    popup.add(send_menu);
    return popup;
}
Also used : DBObject(ini.trakem2.persistence.DBObject) JMenuItem(javax.swing.JMenuItem) JPopupMenu(javax.swing.JPopupMenu) JMenu(javax.swing.JMenu)

Example 54 with Project

use of ini.trakem2.Project in project TrakEM2 by trakem2.

the class TemplateTree method mousePressed.

public void mousePressed(MouseEvent me) {
    Object source = me.getSource();
    if (!source.equals(this) || !project.isInputEnabled()) {
        return;
    }
    /*if (!(me.isPopupTrigger() || me.isControlDown() || MouseEvent.BUTTON2 == me.getButton() || 0 != (me.getModifiers() & Event.META_MASK))) { // the last block is from ij.gui.ImageCanvas, aparently to make the right-click work on windows?
			return;
		}*/
    if (!Utils.isPopupTrigger(me))
        return;
    int x = me.getX();
    int y = me.getY();
    // find the node and set it selected
    TreePath path = getPathForLocation(x, y);
    if (null == path)
        return;
    setSelectionPath(path);
    this.selected_node = (DefaultMutableTreeNode) path.getLastPathComponent();
    final TemplateThing tt = (TemplateThing) selected_node.getUserObject();
    String type = tt.getType();
    // 
    JPopupMenu popup = new JPopupMenu();
    JMenuItem item;
    if (!Project.isBasicType(type) && !tt.isNested()) {
        JMenu menu = new JMenu("Add new child");
        popup.add(menu);
        item = new JMenuItem("new...");
        item.addActionListener(this);
        menu.add(item);
        // Add also from other open projects
        if (ControlWindow.getProjects().size() > 1) {
            menu.addSeparator();
            JMenu other = new JMenu("From project...");
            menu.add(other);
            for (Iterator<Project> itp = ControlWindow.getProjects().iterator(); itp.hasNext(); ) {
                final Project pr = (Project) itp.next();
                if (root.getProject() == pr)
                    continue;
                item = new JMenuItem(pr.toString());
                other.add(item);
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent ae) {
                        GenericDialog gd = new GenericDialog(pr.toString());
                        gd.addMessage("Project: " + pr.toString());
                        final HashMap<String, TemplateThing> hm = pr.getTemplateTree().root.getUniqueTypes(new HashMap<String, TemplateThing>());
                        final String[] u_types = hm.keySet().toArray(new String[0]);
                        gd.addChoice("type:", u_types, u_types[0]);
                        gd.showDialog();
                        if (gd.wasCanceled())
                            return;
                        TemplateThing tt_chosen = hm.get(gd.getNextChoice());
                        // must solve conflicts!
                        // Recurse into children: if any type that is not a basic type exists in the target project, ban the operation.
                        ArrayList al = tt_chosen.collectAllChildren(new ArrayList());
                        for (Iterator ital = al.iterator(); ital.hasNext(); ) {
                            TemplateThing child = (TemplateThing) ital.next();
                            if (root.getProject().typeExists(child.getType()) && !pr.isBasicType(child.getType())) {
                                Utils.showMessage("Type conflict: cannot add type " + tt_chosen.getType());
                                return;
                            }
                        }
                        // Else add it, recursive into children
                        // Target is tt
                        addCopiesRecursively(tt, tt_chosen);
                        rebuild(selected_node, true);
                    }
                });
            }
        }
        menu.addSeparator();
        String[] ut = tt.getProject().getUniqueTypes();
        for (int i = 0; i < ut.length; i++) {
            item = new JMenuItem(ut[i]);
            item.addActionListener(this);
            menu.add(item);
        }
    }
    item = new JMenuItem("Delete...");
    item.addActionListener(this);
    popup.add(item);
    // disable deletion of root.
    if (null == selected_node.getParent())
        item.setEnabled(false);
    if (!Project.isBasicType(type)) {
        item = new JMenuItem("Rename...");
        item.addActionListener(this);
        popup.add(item);
    }
    popup.addSeparator();
    item = new JMenuItem("Export XML template...");
    item.addActionListener(this);
    popup.add(item);
    popup.show(this, x, y);
}
Also used : HashMap(java.util.HashMap) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) JPopupMenu(javax.swing.JPopupMenu) Project(ini.trakem2.Project) TreePath(javax.swing.tree.TreePath) ActionListener(java.awt.event.ActionListener) GenericDialog(ij.gui.GenericDialog) Iterator(java.util.Iterator) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Example 55 with Project

use of ini.trakem2.Project in project TrakEM2 by trakem2.

the class TemplateTree method renameType.

/**
 * Rename a TemplateThing type from @param old_name to @param new_name.
 *  If such a new_name already exists, the renaming will not occur and returns false.
 */
public boolean renameType(final String old_name, String new_name) {
    // to lower case!
    new_name = new_name.toLowerCase();
    Project project = root.getProject();
    if (new_name.equals(old_name)) {
        return true;
    } else if (project.typeExists(new_name)) {
        Utils.logAll("Type '" + new_name + "' exists already!");
        return false;
    }
    // process name change in all TemplateThing instances that have it
    ArrayList<TemplateThing> al = root.collectAllChildren(new ArrayList<TemplateThing>());
    al.add(root);
    for (final TemplateThing tet : al) {
        // Utils.log("\tchecking " + tet.getType() + " " + tet.getId());
        if (tet.getType().equals(old_name))
            tet.rename(new_name);
    }
    // and update the ProjectThing objects in the tree and its dependant Displayable objects in the open Displays
    project.getRootProjectThing().updateType(new_name, old_name);
    // tell the project about it
    project.updateTypeName(old_name, new_name);
    // repaint both trees (will update the type names)
    updateUILater();
    project.getProjectTree().updateUILater();
    return true;
}
Also used : Project(ini.trakem2.Project)

Aggregations

ArrayList (java.util.ArrayList)33 Project (ini.trakem2.Project)26 HashMap (java.util.HashMap)25 Layer (ini.trakem2.display.Layer)21 Displayable (ini.trakem2.display.Displayable)19 Patch (ini.trakem2.display.Patch)18 File (java.io.File)18 HashSet (java.util.HashSet)18 ZDisplayable (ini.trakem2.display.ZDisplayable)17 ImagePlus (ij.ImagePlus)16 ProjectThing (ini.trakem2.tree.ProjectThing)16 Worker (ini.trakem2.utils.Worker)16 TemplateThing (ini.trakem2.tree.TemplateThing)15 Map (java.util.Map)15 LayerSet (ini.trakem2.display.LayerSet)14 ResultSet (java.sql.ResultSet)13 DBObject (ini.trakem2.persistence.DBObject)12 AffineTransform (java.awt.geom.AffineTransform)11 TreeMap (java.util.TreeMap)11 FSLoader (ini.trakem2.persistence.FSLoader)10