Search in sources :

Example 1 with Actionable

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);
            }
        }
    });
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) IStatus(org.eclipse.core.runtime.IStatus) Matcher(java.util.regex.Matcher) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) Actionable(aQute.bnd.service.Actionable) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositoryEntry(bndtools.model.repo.RepositoryEntry) Entry(java.util.Map.Entry) GroupMarker(org.eclipse.jface.action.GroupMarker) Menu(org.eclipse.swt.widgets.Menu) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Job(org.eclipse.core.runtime.jobs.Job) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Refreshable(aQute.bnd.service.Refreshable) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IMenuListener(org.eclipse.jface.action.IMenuListener) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 2 with Actionable

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);
}
Also used : Project(aQute.bnd.build.Project) Actionable(aQute.bnd.service.Actionable) Glob(aQute.libg.glob.Glob) Description(aQute.lib.getopt.Description)

Aggregations

Actionable (aQute.bnd.service.Actionable)2 Project (aQute.bnd.build.Project)1 Refreshable (aQute.bnd.service.Refreshable)1 RemoteRepositoryPlugin (aQute.bnd.service.RemoteRepositoryPlugin)1 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)1 Description (aQute.lib.getopt.Description)1 Glob (aQute.libg.glob.Glob)1 RepositoryEntry (bndtools.model.repo.RepositoryEntry)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Matcher (java.util.regex.Matcher)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1