1 /*******************************************************************************
2 * Copyright (C) 2016 Thomas Wolf <thomas.wolf@paranor.ch>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.egit
.ui
.internal
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.function
.BooleanSupplier
;
16 import org
.eclipse
.jface
.action
.Action
;
17 import org
.eclipse
.jface
.action
.IAction
;
18 import org
.eclipse
.jface
.commands
.ActionHandler
;
19 import org
.eclipse
.swt
.events
.FocusEvent
;
20 import org
.eclipse
.swt
.events
.FocusListener
;
21 import org
.eclipse
.swt
.widgets
.Control
;
22 import org
.eclipse
.ui
.ActiveShellExpression
;
23 import org
.eclipse
.ui
.PlatformUI
;
24 import org
.eclipse
.ui
.actions
.ActionFactory
;
25 import org
.eclipse
.ui
.actions
.ActionFactory
.IWorkbenchAction
;
26 import org
.eclipse
.ui
.handlers
.IHandlerActivation
;
27 import org
.eclipse
.ui
.handlers
.IHandlerService
;
28 import org
.eclipse
.ui
.texteditor
.IUpdate
;
31 * Action-related utilities.
33 public final class ActionUtils
{
35 private ActionUtils() {
36 // Utility class shall not be instantiated.
40 * Create an {@link IAction} taking the text, id, and action definition id
41 * from the given {@link ActionFactory}.
44 * from which the new {@link IAction} shall be derived
47 * @return the new {@link IAction}
49 public static IAction
createGlobalAction(ActionFactory factory
,
50 final Runnable action
) {
51 IWorkbenchAction template
= factory
52 .create(PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
53 IAction result
= new Action(template
.getText()) {
60 result
.setActionDefinitionId(factory
.getCommandId());
61 result
.setId(factory
.getId());
67 * Create an {@link IAction} taking the text, id, and action definition id
68 * from the given {@link ActionFactory}.
71 * from which the new {@link IAction} shall be derived
75 * to obtain the action's enablement
76 * @return the new {@link IAction}
78 public static IAction
createGlobalAction(ActionFactory factory
,
79 final Runnable action
, final BooleanSupplier enabled
) {
80 IWorkbenchAction template
= factory
81 .create(PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
82 IAction result
= new Action(template
.getText()) {
90 public boolean isEnabled() {
91 return enabled
.getAsBoolean();
94 result
.setActionDefinitionId(factory
.getCommandId());
95 result
.setId(factory
.getId());
101 * Hooks up the {@link Control} such that the given {@link IAction}s are
102 * registered with the given {@link IHandlerService} while the control has
103 * the focus. Ensures that actions are properly de-registered when the
104 * control is disposed.
109 * to be registered while the control has the focus
111 * to register the actions with
113 public static void setGlobalActions(Control control
,
114 Collection
<?
extends IAction
> actions
, IHandlerService service
) {
115 Collection
<IHandlerActivation
> handlerActivations
= new ArrayList
<>();
116 control
.addDisposeListener(event
-> {
117 if (!handlerActivations
.isEmpty()) {
118 service
.deactivateHandlers(handlerActivations
);
119 handlerActivations
.clear();
122 final ActiveShellExpression expression
= new ActiveShellExpression(
124 control
.addFocusListener(new FocusListener() {
127 public void focusLost(FocusEvent e
) {
128 if (!handlerActivations
.isEmpty()) {
129 service
.deactivateHandlers(handlerActivations
);
130 handlerActivations
.clear();
135 public void focusGained(FocusEvent e
) {
136 if (!handlerActivations
.isEmpty()) {
137 // Looks like sometimes we get two focusGained events.
140 for (final IAction action
: actions
) {
141 handlerActivations
.add(service
.activateHandler(
142 action
.getActionDefinitionId(),
143 new ActionHandler(action
), expression
, false));
144 if (action
instanceof IUpdate
) {
145 ((IUpdate
) action
).update();
153 * Hooks up the {@link Control} such that the given {@link IAction}s are
154 * registered with the given {@link IHandlerService} while the control has
155 * the focus. Ensures that actions are properly de-registered when the
156 * control is disposed.
161 * to register the actions with
163 * to be registered while the control has the focus
165 public static void setGlobalActions(Control control
,
166 IHandlerService service
, IAction
... actions
) {
167 setGlobalActions(control
, Arrays
.asList(actions
), service
);
171 * Hooks up the {@link Control} such that the given {@link IAction}s are
172 * registered with the workbench-global {@link IHandlerService} while the
173 * control has the focus. Ensures that actions are properly de-registered
174 * when the control is disposed.
179 * to be registered while the control has the focus
181 public static void setGlobalActions(Control control
,
182 Collection
<?
extends IAction
> actions
) {
183 setGlobalActions(control
, actions
, CommonUtils
184 .getService(PlatformUI
.getWorkbench(), IHandlerService
.class));
188 * Hooks up the {@link Control} such that the given {@link IAction}s are
189 * registered with the workbench-global {@link IHandlerService} while the
190 * control has the focus. Ensures that actions are properly de-registered
191 * when the control is disposed.
196 * to be registered while the control has the focus
198 public static void setGlobalActions(Control control
, IAction
... actions
) {
199 setGlobalActions(control
, Arrays
.asList(actions
));