Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2d2a5c3613cf05584a9fbba7670b5d5a27a3d8d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*******************************************************************************
 * Copyright (C) 2016 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.BooleanSupplier;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.commands.ActionHandler;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ActiveShellExpression;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.handlers.IHandlerActivation;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.texteditor.IUpdate;

/**
 * Action-related utilities.
 */
public final class ActionUtils {

	private ActionUtils() {
		// Utility class shall not be instantiated.
	}

	/**
	 * Create an {@link IAction} taking the text, id, and action definition id
	 * from the given {@link ActionFactory}.
	 *
	 * @param factory
	 *            from which the new {@link IAction} shall be derived
	 * @param action
	 *            to execute
	 * @return the new {@link IAction}
	 */
	public static IAction createGlobalAction(ActionFactory factory,
			final Runnable action) {
		IWorkbenchAction template = factory
				.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
		IAction result = new Action(template.getText()) {

			@Override
			public void run() {
				action.run();
			}
		};
		result.setActionDefinitionId(template.getActionDefinitionId());
		result.setId(template.getId());
		result.setImageDescriptor(template.getImageDescriptor());
		result.setDisabledImageDescriptor(
				template.getDisabledImageDescriptor());
		template.dispose();
		return result;
	}

	/**
	 * Create an {@link UpdateableAction} taking the text, id, and action
	 * definition id from the given {@link ActionFactory}. The using code of
	 * such an action is responsible for calling {@link IUpdate#update()
	 * update()} on the action when its enablement should be updated.
	 *
	 * @param factory
	 *            from which the new {@link IAction} shall be derived
	 * @param action
	 *            to execute
	 * @param enabled
	 *            to obtain the action's enablement
	 * @return the new {@link UpdateableAction}
	 */
	public static UpdateableAction createGlobalAction(ActionFactory factory,
			final Runnable action, final BooleanSupplier enabled) {
		IWorkbenchAction template = factory
				.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
		UpdateableAction result = new UpdateableAction(template.getText()) {

			@Override
			public void run() {
				action.run();
			}

			@Override
			public void update() {
				setEnabled(enabled.getAsBoolean());
			}
		};
		result.setActionDefinitionId(template.getActionDefinitionId());
		result.setId(template.getId());
		result.setImageDescriptor(template.getImageDescriptor());
		result.setDisabledImageDescriptor(
				template.getDisabledImageDescriptor());
		result.update();
		template.dispose();
		return result;
	}

	/**
	 * Creates a new text action using a given {@link ActionFactory} to use as a
	 * template to set the label, image, and action definition id.
	 *
	 * @param target
	 *            for the action
	 * @param factory
	 *            to configure the action
	 * @param operationCode
	 *            for the action
	 * @return the configured {@link UpdateableAction}
	 */
	public static UpdateableAction createTextAction(
			ITextOperationTarget target, ActionFactory factory,
			int operationCode) {
		if (operationCode == ITextOperationTarget.REDO) {
			// XXX: workaround for
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=206111
			return createGlobalAction(factory,
					() -> target.doOperation(operationCode), () -> true);
		}
		return createGlobalAction(factory,
				() -> target.doOperation(operationCode),
				() -> target.canDoOperation(operationCode));
	}

	private static UpdateableAction[] createStandardTextActions(
			ITextOperationTarget target, boolean editable) {
		UpdateableAction[] actions = new UpdateableAction[ITextOperationTarget.SELECT_ALL
				+ 1];
		if (editable) {
			actions[ITextOperationTarget.UNDO] = createTextAction(target,
					ActionFactory.UNDO, ITextOperationTarget.UNDO);
			actions[ITextOperationTarget.REDO] = createTextAction(target,
					ActionFactory.REDO, ITextOperationTarget.REDO);
			actions[ITextOperationTarget.CUT] = createTextAction(target,
					ActionFactory.CUT, ITextOperationTarget.CUT);
			actions[ITextOperationTarget.PASTE] = createTextAction(
					target, ActionFactory.PASTE, ITextOperationTarget.PASTE);
			actions[ITextOperationTarget.DELETE] = createTextAction(
					target, ActionFactory.DELETE, ITextOperationTarget.DELETE);
		}
		actions[ITextOperationTarget.COPY] = createTextAction(target,
				ActionFactory.COPY, ITextOperationTarget.COPY);
		actions[ITextOperationTarget.SELECT_ALL] = createTextAction(
				target, ActionFactory.SELECT_ALL,
				ITextOperationTarget.SELECT_ALL);
		return actions;
	}

	/**
	 * Create the standard text actions, fill them into a {@MenuManager} and
	 * return them as an array indexed by the {@link ITextOperationTarget}
	 * operation codes. For an editable target, creates undo, redo | cut, copy,
	 * paste | delete, select all; otherwise just copy, select all.
	 *
	 * @param target
	 *            for the actions to operate on
	 * @param editable
	 *            whether the target is editable
	 * @param manager
	 *            to fill in; may be {@code null} if the actions shall not be
	 *            added to a {@link MenuManager}
	 * @return the actions; may contain {@code null} values (index 0 will always
	 *         be {@code null})
	 */
	public static UpdateableAction[] fillStandardTextActions(
			ITextOperationTarget target, boolean editable,
			MenuManager manager) {
		UpdateableAction[] actions = createStandardTextActions(target,
				editable);
		if (manager != null) {
			if (editable) {
				manager.add(actions[ITextOperationTarget.UNDO]);
				manager.add(actions[ITextOperationTarget.REDO]);
				manager.add(new Separator());
				manager.add(actions[ITextOperationTarget.CUT]);
			}
			manager.add(actions[ITextOperationTarget.COPY]);
			if (editable) {
				manager.add(actions[ITextOperationTarget.PASTE]);
				manager.add(new Separator());
				manager.add(actions[ITextOperationTarget.DELETE]);
			}
			manager.add(actions[ITextOperationTarget.SELECT_ALL]);
		}
		return actions;
	}

	/**
	 * 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; {@code null}
	 *            items are skipped.
	 * @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 (IAction action : actions) {
					if (action != null) {
						handlerActivations.add(service.activateHandler(
								action.getActionDefinitionId(),
								new ActionHandler(action), expression, false));
						if (action instanceof IUpdate) {
							((IUpdate) action).update();
						}
					}
				}
			}
		});
	}

	/**
	 * 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 service
	 *            to register the actions with
	 * @param actions
	 *            to be registered while the control has the focus
	 */
	public static void setGlobalActions(Control control,
			IHandlerService service, IAction... actions) {
		setGlobalActions(control, Arrays.asList(actions), service);
	}

	/**
	 * Hooks up the {@link Control} such that the given {@link IAction}s are
	 * registered with the workbench-global {@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
	 */
	public static void setGlobalActions(Control control,
			Collection<? extends IAction> actions) {
		setGlobalActions(control, actions,
				PlatformUI.getWorkbench().getService(IHandlerService.class));
	}

	/**
	 * Hooks up the {@link Control} such that the given {@link IAction}s are
	 * registered with the workbench-global {@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
	 */
	public static void setGlobalActions(Control control, IAction... actions) {
		setGlobalActions(control, Arrays.asList(actions));
	}

	/**
	 * An {@link Action} that is updateable via {@link IUpdate}.
	 */
	public static abstract class UpdateableAction extends Action
			implements IUpdate {

		/**
		 * Creates a new {@link UpdateableAction} with the given text.
		 *
		 * @param text
		 */
		public UpdateableAction(String text) {
			super(text);
		}
	}
}

Back to the top