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;
}
Aggregations