use of aQute.bnd.service.Actionable in project bndtools by bndtools.
the class RepositoriesView method createContextMenu.
void createContextMenu() {
MenuManager mgr = new MenuManager();
Menu menu = mgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
mgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
getSite().registerContextMenu(mgr, viewer);
mgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
try {
manager.removeAll();
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
if (!selection.isEmpty()) {
final Object firstElement = selection.getFirstElement();
if (firstElement instanceof Actionable) {
final RepositoryPlugin rp = getRepositoryPlugin(firstElement);
//
// Use the Actionable interface to fill the menu
// Should extend this to allow other menu entries
// from the view, but currently there are none
//
final Actionable act = (Actionable) firstElement;
Map<String, Runnable> actions = act.actions();
if (actions != null) {
for (final Entry<String, Runnable> e : actions.entrySet()) {
String label = e.getKey();
boolean enabled = true;
boolean checked = false;
String description = null;
Matcher m = LABEL_PATTERN.matcher(label);
if (m.matches()) {
if (m.group(1) != null)
enabled = false;
if (m.group(2) != null)
checked = true;
label = m.group(3);
description = m.group(4);
}
Action a = new Action(label.replace("&", "&&")) {
@Override
public void run() {
Job backgroundJob = new Job("Repository Action '" + getText() + "'") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
e.getValue().run();
if (rp != null && rp instanceof Refreshable)
Central.refreshPlugin((Refreshable) rp);
} catch (final Exception e) {
IStatus status = new Status(IStatus.ERROR, Plugin.PLUGIN_ID, "Error executing: " + getName(), e);
Plugin.getDefault().getLog().log(status);
}
monitor.done();
return Status.OK_STATUS;
}
};
backgroundJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
viewer.getTree().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
viewer.refresh();
}
});
}
}
});
backgroundJob.setUser(true);
backgroundJob.setPriority(Job.SHORT);
backgroundJob.schedule();
}
};
a.setEnabled(enabled);
if (description != null)
a.setDescription(description);
a.setChecked(checked);
manager.add(a);
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
use of aQute.bnd.service.Actionable in project bnd by bndtools.
the class bnd method _action.
@Description("Execute an action on a repo, or if no name is give, list the actions")
public void _action(ActionOptions opts) throws Exception {
Project project = getProject(opts.project());
if (project == null) {
error("Not in a project directory");
return;
}
Glob filter = opts.filter();
if (filter == null)
filter = new Glob("*");
List<Actionable> actionables = project.getPlugins(Actionable.class);
if (actionables.isEmpty()) {
error("No actionables in [%s]", project.getPlugins());
return;
}
for (Actionable o : actionables) {
if (filter.matcher(o.title()).matches()) {
logger.debug("actionable {} - {}", o, o.title());
Map<String, Runnable> map = o.actions();
if (map != null) {
if (opts._arguments().isEmpty()) {
out.printf("# %s%n", o.title());
if (opts.tooltip() && o.tooltip() != null) {
out.printf("%s%n", o.tooltip());
}
out.printf("## actions%n");
for (String entry : map.keySet()) {
out.printf(" %s%n", entry);
}
} else {
for (String entry : opts._arguments()) {
Runnable r = map.get(entry);
if (r != null) {
r.run();
}
}
}
}
}
}
getInfo(project);
}
Aggregations