Search in sources :

Example 1 with ActiveShellExpression

use of org.eclipse.ui.ActiveShellExpression 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 2 with ActiveShellExpression

use of org.eclipse.ui.ActiveShellExpression in project eclipse-integration-commons by spring-projects.

the class QuickSearchDialog method createViewMenu.

private void createViewMenu(Composite parent) {
    toolBar = new ToolBar(parent, SWT.FLAT);
    toolItem = new ToolItem(toolBar, SWT.PUSH, 0);
    GridData data = new GridData();
    data.horizontalAlignment = GridData.END;
    toolBar.setLayoutData(data);
    toolBar.addMouseListener(new MouseAdapter() {

        public void mouseDown(MouseEvent e) {
            showViewMenu();
        }
    });
    toolItem.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU));
    toolItem.setToolTipText(WorkbenchMessages.FilteredItemsSelectionDialog_menu);
    toolItem.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            showViewMenu();
        }
    });
    menuManager = new MenuManager();
    fillViewMenu(menuManager);
    IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
    IHandler handler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            showViewMenu();
            return null;
        }
    };
    showViewHandler = service.activateHandler(IWorkbenchCommandConstants.WINDOW_SHOW_VIEW_MENU, handler, new ActiveShellExpression(getShell()));
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) AbstractHandler(org.eclipse.core.commands.AbstractHandler) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ToolBar(org.eclipse.swt.widgets.ToolBar) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IHandler(org.eclipse.core.commands.IHandler) ActiveShellExpression(org.eclipse.ui.ActiveShellExpression) ToolItem(org.eclipse.swt.widgets.ToolItem)

Example 3 with ActiveShellExpression

use of org.eclipse.ui.ActiveShellExpression 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)

Example 4 with ActiveShellExpression

use of org.eclipse.ui.ActiveShellExpression 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)

Aggregations

ActiveShellExpression (org.eclipse.ui.ActiveShellExpression)4 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 IHandlerActivation (org.eclipse.ui.handlers.IHandlerActivation)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 AbstractHandler (org.eclipse.core.commands.AbstractHandler)1 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)1 IHandler (org.eclipse.core.commands.IHandler)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1