Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ac0ecd2b5a008f27d1405dff242085c54e800f1 (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
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.commands.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.commands.IDebugCommandHandler;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.contexts.IDebugContextService;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Updates commands for a window. Coalesces update requests by command type.
 *
 * @since 3.3
 */
public class DebugCommandService implements IDebugContextListener {

	/**
	 * Maps command types to actions to update
	 */
	private Map<Class<?>, List<IEnabledTarget>> fCommandUpdates = new HashMap<>();

	/**
	 * Window this service is for.
	 */
	private IWorkbenchWindow fWindow = null;

	/**
	 * The context service for this command service.
	 */
	private IDebugContextService fContextService = null;

	/**
	 * Service per window
	 */
	private static Map<IWorkbenchWindow, DebugCommandService> fgServices = new HashMap<>();

	/**
	 * Returns the service for a window.
	 *
	 * @param window the window
	 * @return service
	 */
	public synchronized static DebugCommandService getService(IWorkbenchWindow window) {
		DebugCommandService service = fgServices.get(window);
		if (service == null) {
			service = new DebugCommandService(window);
			fgServices.put(window, service);
		}
		return service;
	}

	public DebugCommandService(IWorkbenchWindow window) {
		fWindow = window;
		fContextService = DebugUITools.getDebugContextManager().getContextService(window);
		fContextService.addPostDebugContextListener(this);
		PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {

			@Override
			public void windowOpened(IWorkbenchWindow w) {
			}

			@Override
			public void windowDeactivated(IWorkbenchWindow w) {
			}

			@Override
			public void windowClosed(IWorkbenchWindow w) {
				if (fWindow == w) {
					dispose();
				}
			}

			@Override
			public void windowActivated(IWorkbenchWindow w) {
			}

		});
	}

	private void dispose() {
		fContextService.removeDebugContextListener(this);
		fgServices.remove(fWindow);
		fCommandUpdates.clear();
		fWindow = null;
	}

	/**
	 * Updates the given command type after the next context change.
	 *
	 * @param commandType the command class
	 * @param action the action to add to the update list
	 */
	public void postUpdateCommand(Class<?> commandType, IEnabledTarget action) {
		synchronized (fCommandUpdates) {
			Job.getJobManager().cancel(commandType);
			List<IEnabledTarget> actions = fCommandUpdates.get(commandType);
			if (actions == null) {
				actions = new ArrayList<>();
				fCommandUpdates.put(commandType, actions);
			}
			actions.add(action);
		}
	}

	/**
	 * Updates the given command type based on the active context.
	 *
	 * @param commandType the command class
	 * @param action the action to update
	 */
	public void updateCommand(Class<?> commandType, IEnabledTarget action) {
		ISelection context = fContextService.getActiveContext();
		if (context instanceof IStructuredSelection && !context.isEmpty()) {
			Object[] elements = ((IStructuredSelection)context).toArray();
			updateCommand(commandType, elements, new IEnabledTarget[]{action});
		} else {
			action.setEnabled(false);
		}
	}

	private void postUpdate(ISelection context) {
		Map<Class<?>, List<IEnabledTarget>> commands = null;
		synchronized (fCommandUpdates) {
			commands = fCommandUpdates;
			fCommandUpdates = new HashMap<>(commands.size());
		}
		if (context instanceof IStructuredSelection && !context.isEmpty()) {
			Object[] elements = ((IStructuredSelection)context).toArray();
			for (Entry<Class<?>, List<IEnabledTarget>> entry : commands.entrySet()) {
				List<IEnabledTarget> actions = entry.getValue();
				updateCommand(entry.getKey(), elements, actions.toArray(new IEnabledTarget[actions.size()]));
			}
		} else {
			for (List<IEnabledTarget> actionList : commands.values()) {
				for (IEnabledTarget target : actionList) {
					target.setEnabled(false);
				}
			}
		}
		commands.clear();
	}

	/**
	 * Updates the given command type for the specified elements.
	 * @param handlerType the handle type class
	 * @param elements elements to update for
	 * @param actions the actions to update
	 */
	private void updateCommand(Class<?> handlerType, Object[] elements, IEnabledTarget[] actions) {
		if (elements.length == 1) {
			// usual case - one element
			Object element = elements[0];
			IDebugCommandHandler handler = getHandler(element, handlerType);
			if (handler != null) {
				UpdateActionsRequest request = new UpdateActionsRequest(elements, actions);
				handler.canExecute(request);
				return;
			}
		} else {
			Map<IDebugCommandHandler, List<Object>> map = collate(elements, handlerType);
			if (map != null) {
				ActionsUpdater updater = new ActionsUpdater(actions, map.size());
				for (Entry<IDebugCommandHandler, List<Object>> entry : map.entrySet()) {
					UpdateHandlerRequest request = new UpdateHandlerRequest(entry.getValue().toArray(), updater);
					entry.getKey().canExecute(request);
				}
				return;
			}
		}
		// ABORT - no command processors
		for (int i = 0; i < actions.length; i++) {
			actions[i].setEnabled(false);
		}
	}

	/**
	 * Updates the given command type for the specified elements.
	 * @param handlerType the handler type class
	 * @param elements elements to update for
	 * @param participant the participant
	 * @return if the command stays enabled while the command executes
	 */
	public boolean executeCommand(Class<?> handlerType, Object[] elements, ICommandParticipant participant) {
		if (elements.length == 1) {
			// usual case - one element
			Object element = elements[0];
			IDebugCommandHandler handler = getHandler(element, handlerType);
			if (handler != null) {
				ExecuteActionRequest request = new ExecuteActionRequest(elements);
				request.setCommandParticipant(participant);
				return handler.execute(request);
			}
		} else {
			Map<IDebugCommandHandler, List<Object>> map = collate(elements, handlerType);
			if (map != null) {
				boolean enabled = true;
				for (Entry<IDebugCommandHandler, List<Object>> entry : map.entrySet()) {
					ExecuteActionRequest request = new ExecuteActionRequest(entry.getValue().toArray());
					request.setCommandParticipant(participant);
					// specifically use & so handler is executed
					enabled = enabled & entry.getKey().execute(request);
				}
				return enabled;
			}
		}
		// ABORT - no command processors
		return false;
	}

	@Override
	public void debugContextChanged(DebugContextEvent event) {
		postUpdate(event.getContext());
	}

	/**
	 * Returns a map of command handlers to associated elements, or <code>null</code> if
	 * one is missing.
	 *
	 * @param elements the elements
	 * @param handlerType the handler type class
	 * @return map of command handlers to associated elements or <code>null</code>
	 */
	private Map<IDebugCommandHandler, List<Object>> collate(Object[] elements, Class<?> handlerType) {
		Map<IDebugCommandHandler, List<Object>> map = new HashMap<>();
 		for (int i = 0; i < elements.length; i++) {
 			Object element = elements[i];
 			IDebugCommandHandler handler = getHandler(element, handlerType);
			if (handler == null) {
				return null;
			} else {
				List<Object> list = map.get(handler);
				if (list == null) {
					list = new ArrayList<>();
					map.put(handler, list);
	 				}
				list.add(element);
	 			}
	 		}
		return map;
	}

	private IDebugCommandHandler getHandler(Object element, Class<?> handlerType) {
		return (IDebugCommandHandler)DebugPlugin.getAdapter(element, handlerType);
	}

}

Back to the top