Search in sources :

Example 1 with IHandlerActivation

use of org.eclipse.ui.handlers.IHandlerActivation in project bndtools by bndtools.

the class BndEditor method setupActions.

private void setupActions() {
    String fileName = getFileAndProject(getEditorInput()).getFirst();
    if (fileName.endsWith(LaunchConstants.EXT_BNDRUN)) {
        final IHandlerService handlerSvc = (IHandlerService) getEditorSite().getService(IHandlerService.class);
        final AbstractHandler handler = new AbstractHandler() {

            @Override
            public Object execute(ExecutionEvent event) throws ExecutionException {
                resolveRunBundles(new NullProgressMonitor(), false);
                return null;
            }
        };
        final IHandlerActivation activation = handlerSvc.activateHandler("bndtools.runEditor.resolve", handler);
        this.resolveJobListener = new JobChangeAdapter() {

            @Override
            public void running(IJobChangeEvent event) {
                if (event.getJob() instanceof ResolveJob)
                    Display.getDefault().asyncExec(() -> handlerSvc.deactivateHandler(activation));
            }

            @Override
            public void done(IJobChangeEvent event) {
                if (event.getJob() instanceof ResolveJob)
                    Display.getDefault().asyncExec(() -> handlerSvc.activateHandler(activation));
            }
        };
        Job.getJobManager().addJobChangeListener(resolveJobListener);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) ResolveJob(org.bndtools.core.resolve.ResolveJob) AbstractHandler(org.eclipse.core.commands.AbstractHandler)

Example 2 with IHandlerActivation

use of org.eclipse.ui.handlers.IHandlerActivation in project xtext-eclipse by eclipse.

the class CodetemplatesEmbeddedEditorActions method createFocusAndDisposeListeners.

@Override
protected void createFocusAndDisposeListeners() {
    final List<IHandlerActivation> handlerActivations = Lists.newArrayListWithExpectedSize(3);
    final IHandlerService handlerService = workbench.getAdapter(IHandlerService.class);
    final IContextService contextService = workbench.getAdapter(IContextService.class);
    Shell shell = viewer.getTextWidget().getShell();
    final ActiveShellExpression expression = new ActiveShellExpression(shell);
    AtomicReference<IContextActivation> contextActivationHolder = new AtomicReference<>();
    shell.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            handlerService.deactivateHandlers(handlerActivations);
        }
    });
    viewer.getTextWidget().addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            IContextActivation contextActivation = contextActivationHolder.get();
            if (contextActivation != null) {
                contextService.deactivateContext(contextActivation);
            }
            handlerService.deactivateHandlers(handlerActivations);
            handlerActivations.clear();
        }

        @Override
        public void focusGained(FocusEvent e) {
            final IContextActivation contextActivation = contextService.activateContext(EMBEDDED_TEXT_EDITOR_SCOPE, expression);
            contextActivationHolder.set(contextActivation);
            for (final IAction action : allActions.values()) {
                handlerActivations.add(handlerService.activateHandler(action.getActionDefinitionId(), new ActionHandler(action), expression, true));
            }
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IAction(org.eclipse.jface.action.IAction) AtomicReference(java.util.concurrent.atomic.AtomicReference) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) Shell(org.eclipse.swt.widgets.Shell) IHandlerService(org.eclipse.ui.handlers.IHandlerService) IContextActivation(org.eclipse.ui.contexts.IContextActivation) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) IContextService(org.eclipse.ui.contexts.IContextService) ActiveShellExpression(org.eclipse.ui.ActiveShellExpression) FocusListener(org.eclipse.swt.events.FocusListener) ActionHandler(org.eclipse.jface.commands.ActionHandler)

Example 3 with IHandlerActivation

use of org.eclipse.ui.handlers.IHandlerActivation in project tmdm-studio-se by Talend.

the class XMLSourceViewer method activateHandlers.

/**
 * Activate all handlers
 */
protected void activateHandlers() {
    // if handler service is null, return
    if (handlerService == null) {
        return;
    }
    // activate handlers if it is not active
    Iterator<String> i = handlers.keySet().iterator();
    while (i.hasNext()) {
        String id = i.next();
        IHandler handler = handlers.get(id);
        IHandlerActivation activation = handlerActivations.get(handler);
        if (activation == null) {
            activation = handlerService.activateHandler(id, handler);
            handlerActivations.put(handler, activation);
        }
    }
}
Also used : IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) IHandler(org.eclipse.core.commands.IHandler)

Example 4 with IHandlerActivation

use of org.eclipse.ui.handlers.IHandlerActivation in project xtext-eclipse by eclipse.

the class EmbeddedEditorActions method createFocusAndDisposeListeners.

protected void createFocusAndDisposeListeners() {
    final List<IHandlerActivation> handlerActivations = Lists.newArrayListWithExpectedSize(3);
    final IHandlerService handlerService = workbench.getAdapter(IHandlerService.class);
    final IContextService contextService = workbench.getAdapter(IContextService.class);
    Shell shell = viewer.getTextWidget().getShell();
    final ActiveShellExpression expression = new ActiveShellExpression(shell);
    final IContextActivation contextActivation = contextService.activateContext(EMBEDDED_TEXT_EDITOR_SCOPE, expression);
    shell.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            handlerService.deactivateHandlers(handlerActivations);
            contextService.deactivateContext(contextActivation);
        }
    });
    viewer.getTextWidget().addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            handlerService.deactivateHandlers(handlerActivations);
            handlerActivations.clear();
        }

        @Override
        public void focusGained(FocusEvent e) {
            for (final IAction action : allActions.values()) {
                handlerActivations.add(handlerService.activateHandler(action.getActionDefinitionId(), new ActionHandler(action), expression, true));
            }
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IAction(org.eclipse.jface.action.IAction) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) Shell(org.eclipse.swt.widgets.Shell) IHandlerService(org.eclipse.ui.handlers.IHandlerService) IContextActivation(org.eclipse.ui.contexts.IContextActivation) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) IContextService(org.eclipse.ui.contexts.IContextService) ActiveShellExpression(org.eclipse.ui.ActiveShellExpression) FocusListener(org.eclipse.swt.events.FocusListener) ActionHandler(org.eclipse.jface.commands.ActionHandler)

Example 5 with IHandlerActivation

use of org.eclipse.ui.handlers.IHandlerActivation in project egit by eclipse.

the class ActionUtils method setGlobalActions.

/**
 * Hooks up the {@link Control} such that the given {@link IAction}s are
 * registered with the given {@link IHandlerService} while the control has
 * the focus. Ensures that actions are properly de-registered when the
 * control is disposed.
 *
 * @param control
 *            to hook up
 * @param actions
 *            to be registered while the control has the focus
 * @param service
 *            to register the actions with
 */
public static void setGlobalActions(Control control, Collection<? extends IAction> actions, IHandlerService service) {
    Collection<IHandlerActivation> handlerActivations = new ArrayList<>();
    control.addDisposeListener(event -> {
        if (!handlerActivations.isEmpty()) {
            service.deactivateHandlers(handlerActivations);
            handlerActivations.clear();
        }
    });
    final ActiveShellExpression expression = new ActiveShellExpression(control.getShell());
    control.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            if (!handlerActivations.isEmpty()) {
                service.deactivateHandlers(handlerActivations);
                handlerActivations.clear();
            }
        }

        @Override
        public void focusGained(FocusEvent e) {
            if (!handlerActivations.isEmpty()) {
                // Looks like sometimes we get two focusGained events.
                return;
            }
            for (final IAction action : actions) {
                handlerActivations.add(service.activateHandler(action.getActionDefinitionId(), new ActionHandler(action), expression, false));
                if (action instanceof IUpdate) {
                    ((IUpdate) action).update();
                }
            }
        }
    });
}
Also used : IAction(org.eclipse.jface.action.IAction) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) ArrayList(java.util.ArrayList) ActiveShellExpression(org.eclipse.ui.ActiveShellExpression) FocusListener(org.eclipse.swt.events.FocusListener) FocusEvent(org.eclipse.swt.events.FocusEvent) ActionHandler(org.eclipse.jface.commands.ActionHandler) IUpdate(org.eclipse.ui.texteditor.IUpdate)

Aggregations

IHandlerActivation (org.eclipse.ui.handlers.IHandlerActivation)5 IAction (org.eclipse.jface.action.IAction)3 ActionHandler (org.eclipse.jface.commands.ActionHandler)3 FocusEvent (org.eclipse.swt.events.FocusEvent)3 FocusListener (org.eclipse.swt.events.FocusListener)3 ActiveShellExpression (org.eclipse.ui.ActiveShellExpression)3 IHandlerService (org.eclipse.ui.handlers.IHandlerService)3 DisposeEvent (org.eclipse.swt.events.DisposeEvent)2 DisposeListener (org.eclipse.swt.events.DisposeListener)2 Shell (org.eclipse.swt.widgets.Shell)2 IContextActivation (org.eclipse.ui.contexts.IContextActivation)2 IContextService (org.eclipse.ui.contexts.IContextService)2 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ResolveJob (org.bndtools.core.resolve.ResolveJob)1 AbstractHandler (org.eclipse.core.commands.AbstractHandler)1 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)1 IHandler (org.eclipse.core.commands.IHandler)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1