Custom texts for the copy command
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / ActionUtils.java
blob8a91257eb01ef2479f6239577cbfc1ce8a72b4e8
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;
30 /**
31 * Action-related utilities.
33 public final class ActionUtils {
35 private ActionUtils() {
36 // Utility class shall not be instantiated.
39 /**
40 * Create an {@link IAction} taking the text, id, and action definition id
41 * from the given {@link ActionFactory}.
43 * @param factory
44 * from which the new {@link IAction} shall be derived
45 * @param action
46 * to execute
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()) {
55 @Override
56 public void run() {
57 action.run();
60 result.setActionDefinitionId(factory.getCommandId());
61 result.setId(factory.getId());
62 template.dispose();
63 return result;
66 /**
67 * Create an {@link IAction} taking the text, id, and action definition id
68 * from the given {@link ActionFactory}.
70 * @param factory
71 * from which the new {@link IAction} shall be derived
72 * @param action
73 * to execute
74 * @param enabled
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()) {
84 @Override
85 public void run() {
86 action.run();
89 @Override
90 public boolean isEnabled() {
91 return enabled.getAsBoolean();
94 result.setActionDefinitionId(factory.getCommandId());
95 result.setId(factory.getId());
96 template.dispose();
97 return result;
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.
106 * @param control
107 * to hook up
108 * @param actions
109 * to be registered while the control has the focus
110 * @param service
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(
123 control.getShell());
124 control.addFocusListener(new FocusListener() {
126 @Override
127 public void focusLost(FocusEvent e) {
128 if (!handlerActivations.isEmpty()) {
129 service.deactivateHandlers(handlerActivations);
130 handlerActivations.clear();
134 @Override
135 public void focusGained(FocusEvent e) {
136 if (!handlerActivations.isEmpty()) {
137 // Looks like sometimes we get two focusGained events.
138 return;
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.
158 * @param control
159 * to hook up
160 * @param service
161 * to register the actions with
162 * @param actions
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.
176 * @param control
177 * to hook up
178 * @param actions
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.
193 * @param control
194 * to hook up
195 * @param actions
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));