Search in sources :

Example 11 with TemplateThing

use of ini.trakem2.tree.TemplateThing 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 12 with TemplateThing

use of ini.trakem2.tree.TemplateThing 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)

Example 13 with TemplateThing

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

the class Project method createNewProject.

private static Project createNewProject(Loader loader, boolean ask_for_template, TemplateThing template_root, boolean clone_ids) {
    Project project = new Project(loader);
    // Utils.log2("ask_for_template: " + ask_for_template);
    if (ask_for_template)
        template_root = project.loader.askForXMLTemplate(project);
    if (null == template_root) {
        template_root = new TemplateThing("anything");
    } else if (clone_ids) {
        // the given template_root belongs to another project from which we are cloning
        template_root = template_root.clone(project, true);
    }
    // else, use the given template_root as is.
    // create tree
    project.template_tree = new TemplateTree(project, template_root);
    project.root_tt = template_root;
    // collect unique TemplateThing instances
    synchronized (project.ht_unique_tt) {
        project.ht_unique_tt.clear();
        project.ht_unique_tt.putAll(template_root.getUniqueTypes(new HashMap<String, TemplateThing>()));
    }
    // add all TemplateThing objects to the database, recursively
    if (!clone_ids)
        template_root.addToDatabase(project);
    // else already done when cloning the root_tt
    // create a non-database bound template for the project Thing
    TemplateThing project_template = new TemplateThing("project");
    project.ht_unique_tt.put("project", project_template);
    project_template.addChild(template_root);
    // create the project Thing, to be root of the whole project thing tree
    try {
        project.root_pt = new ProjectThing(project_template, project, project);
    } catch (Exception e) {
        IJError.print(e);
    }
    // create the user objects tree
    project.project_tree = new ProjectTree(project, project.root_pt);
    // create the layer's tree
    project.createLayerTemplates();
    // initialized with default values, and null parent to signal 'root'
    project.layer_set = new LayerSet(project, "Top Level", 0, 0, null, 2048, 2048);
    try {
        project.root_lt = new LayerThing(Project.layer_set_template, project, project.layer_set);
        project.layer_tree = new LayerTree(project, project.root_lt);
    } catch (Exception e) {
        project.remove();
        IJError.print(e);
    }
    // create the project control window, containing the trees in a double JSplitPane
    // beware that this call is asynchronous, dispatched by the SwingUtilities.invokeLater to avoid havok with Swing components.
    ControlWindow.add(project, project.template_tree, project.project_tree, project.layer_tree);
    // register
    al_open_projects.add(project);
    return project;
}
Also used : ProjectTree(ini.trakem2.tree.ProjectTree) LayerTree(ini.trakem2.tree.LayerTree) HashMap(java.util.HashMap) LayerSet(ini.trakem2.display.LayerSet) LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) TemplateTree(ini.trakem2.tree.TemplateTree) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 14 with TemplateThing

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

the class Project method createLayerTemplates.

private void createLayerTemplates() {
    if (null == layer_template) {
        layer_template = new TemplateThing("layer");
        layer_set_template = new TemplateThing("layer_set");
        layer_set_template.addChild(layer_template);
        // adding a new instance to keep parent/child relationships clean
        layer_template.addChild(layer_set_template);
    // No need, there won't ever be a loop so far WARNING may change in the future.
    }
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing)

Example 15 with TemplateThing

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

the class Project method newFSProject.

public static Project newFSProject(String arg, TemplateThing template_root, String storage_folder, boolean autocreate_one_layer) {
    if (Utils.wrongImageJVersion())
        return null;
    FSLoader loader = null;
    try {
        String dir_project = storage_folder;
        if (null == dir_project || !new File(dir_project).isDirectory()) {
            DirectoryChooser dc = new DirectoryChooser("Select storage folder");
            dir_project = dc.getDirectory();
            // user cancelled dialog
            if (null == dir_project)
                return null;
            if (!Loader.canReadAndWriteTo(dir_project)) {
                Utils.showMessage("Can't read/write to the selected storage folder.\nPlease check folder permissions.");
                return null;
            }
            if (IJ.isWindows())
                dir_project = dir_project.replace('\\', '/');
        }
        loader = new FSLoader(dir_project);
        Project project = createNewProject(loader, !("blank".equals(arg) || "amira".equals(arg)), template_root);
        // help the helpless users:
        if (autocreate_one_layer && null != project && ControlWindow.isGUIEnabled()) {
            Utils.log2("Creating automatic Display.");
            // add a default layer
            Layer layer = new Layer(project, 0, 1, project.layer_set);
            project.layer_set.add(layer);
            project.layer_tree.addLayer(project.layer_set, layer);
            layer.recreateBuckets();
            Display.createDisplay(project, layer);
        }
        try {
            // waiting cheaply for asynchronous swing calls
            Thread.sleep(200);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        if ("amira".equals(arg) || "stack".equals(arg)) {
            // forks into a task thread
            loader.importStack(project.layer_set.getLayer(0), null, true);
        }
        project.restartAutosaving();
        return project;
    } catch (Exception e) {
        IJError.print(e);
        if (null != loader)
            loader.destroy();
    }
    return null;
}
Also used : FSLoader(ini.trakem2.persistence.FSLoader) JarFile(java.util.jar.JarFile) File(java.io.File) Layer(ini.trakem2.display.Layer) DirectoryChooser(ij.io.DirectoryChooser)

Aggregations

TemplateThing (ini.trakem2.tree.TemplateThing)17 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Displayable (ini.trakem2.display.Displayable)9 ZDisplayable (ini.trakem2.display.ZDisplayable)9 ProjectThing (ini.trakem2.tree.ProjectThing)9 TreePath (javax.swing.tree.TreePath)9 LayerThing (ini.trakem2.tree.LayerThing)8 Project (ini.trakem2.Project)7 Layer (ini.trakem2.display.Layer)7 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 LayerSet (ini.trakem2.display.LayerSet)6 DBObject (ini.trakem2.persistence.DBObject)6 ResultSet (java.sql.ResultSet)6 GenericDialog (ij.gui.GenericDialog)5 FSLoader (ini.trakem2.persistence.FSLoader)4 LayerTree (ini.trakem2.tree.LayerTree)4 ProjectTree (ini.trakem2.tree.ProjectTree)4 TemplateTree (ini.trakem2.tree.TemplateTree)4 SQLException (java.sql.SQLException)4