Search in sources :

Example 1 with TPlugIn

use of ini.trakem2.plugin.TPlugIn in project TrakEM2 by trakem2.

the class Utils method addPlugIns.

/**
 * Returns null if none to add.
 */
public static final JMenu addPlugIns(final String menu, final Project project, final Callable<Displayable> active) {
    final Map<String, TPlugIn> plugins = project.getPlugins(menu);
    if (0 == plugins.size())
        return null;
    Displayable d = null;
    try {
        d = active.call();
    } catch (final Exception e) {
        IJError.print(e);
    }
    final JMenu m = new JMenu("Plugins");
    JMenuItem item;
    int count = 0;
    for (final Map.Entry<String, TPlugIn> e : plugins.entrySet()) {
        item = new JMenuItem(e.getKey());
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent ae) {
                Bureaucrat.createAndStart(new Worker.Task(e.getKey()) {

                    @Override
                    public void exec() {
                        try {
                            e.getValue().invoke(active.call());
                        } catch (final Exception e) {
                            IJError.print(e);
                        }
                    }
                }, project);
            }
        });
        item.setEnabled(e.getValue().applies(d));
        if (count < 9) {
            item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1 + count, Utils.getControlModifier(), true));
        }
        m.add(item);
        count++;
    }
    if (0 == m.getItemCount())
        return null;
    m.addSeparator();
    // Now all the "Setup " + name
    for (final Map.Entry<String, TPlugIn> e : plugins.entrySet()) {
        item = new JMenuItem("Setup " + e.getKey());
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent ae) {
                Bureaucrat.createAndStart(new Worker.Task(e.getKey()) {

                    @Override
                    public void exec() {
                        try {
                            e.getValue().setup(active.call());
                        } catch (final Exception e) {
                            IJError.print(e);
                        }
                    }
                }, project);
            }
        });
        m.add(item);
    }
    return m;
}
Also used : Displayable(ini.trakem2.display.Displayable) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) TPlugIn(ini.trakem2.plugin.TPlugIn) ActionListener(java.awt.event.ActionListener) JMenuItem(javax.swing.JMenuItem) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) JMenu(javax.swing.JMenu)

Aggregations

Displayable (ini.trakem2.display.Displayable)1 TPlugIn (ini.trakem2.plugin.TPlugIn)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1