KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > commands > actions > DebugCommandService


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.commands.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Map.Entry;
19
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.PlatformObject;
22 import org.eclipse.debug.core.commands.IDebugCommandHandler;
23 import org.eclipse.debug.internal.core.commands.CommandAdapterFactory;
24 import org.eclipse.debug.ui.DebugUITools;
25 import org.eclipse.debug.ui.contexts.DebugContextEvent;
26 import org.eclipse.debug.ui.contexts.IDebugContextListener;
27 import org.eclipse.debug.ui.contexts.IDebugContextService;
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IWindowListener;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.PlatformUI;
35
36 /**
37  * Updates commands for a window. Coalesces update requests by command type.
38  *
39  * @since 3.3
40  */

41 public class DebugCommandService implements IDebugContextListener {
42     
43     /**
44      * Maps command types to actions to update
45      */

46     private Map JavaDoc fCommandUpdates = new HashMap JavaDoc();
47     
48     /**
49      * Window this service is for.
50      */

51     private IWorkbenchWindow fWindow = null;
52     
53     /**
54      * The context service for this command service.
55      */

56     private IDebugContextService fContextService = null;
57     
58     /**
59      * Service per window
60      */

61     private static Map JavaDoc fgServices = new HashMap JavaDoc();
62         
63     /**
64      * Returns the service for a window.
65      *
66      * @param window
67      * @return service
68      */

69     public synchronized static DebugCommandService getService(IWorkbenchWindow window) {
70         DebugCommandService service = (DebugCommandService) fgServices.get(window);
71         if (service == null) {
72             service = new DebugCommandService(window);
73             fgServices.put(window, service);
74         }
75         return service;
76     }
77     
78     public DebugCommandService(IWorkbenchWindow window) {
79         fWindow = window;
80         fContextService = DebugUITools.getDebugContextManager().getContextService(window);
81         fContextService.addPostDebugContextListener(this);
82         PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
83         
84             public void windowOpened(IWorkbenchWindow w) {
85             }
86         
87             public void windowDeactivated(IWorkbenchWindow w) {
88             }
89         
90             public void windowClosed(IWorkbenchWindow w) {
91                 if (fWindow == w) {
92                     dispose();
93                 }
94             }
95         
96             public void windowActivated(IWorkbenchWindow w) {
97             }
98         
99         });
100     }
101     
102     private void dispose() {
103         fContextService.removeDebugContextListener(this);
104         fgServices.remove(fWindow);
105         fCommandUpdates.clear();
106         fWindow = null;
107     }
108     
109     /**
110      * Updates the given command type after the next context change.
111      *
112      * @param commandType
113      * @param monitor
114      */

115     public void postUpdateCommand(Class JavaDoc commandType, Action action) {
116         synchronized (fCommandUpdates) {
117             List JavaDoc actions = (List JavaDoc) fCommandUpdates.get(commandType);
118             if (actions == null) {
119                 actions = new ArrayList JavaDoc();
120                 fCommandUpdates.put(commandType, actions);
121             }
122             actions.add(action);
123         }
124     }
125     
126     /**
127      * Updates the given command type based on the active context.
128      *
129      * @param commandType
130      * @param requestMonitor
131      */

132     public void updateCommand(Class JavaDoc commandType, IAction action) {
133         ISelection context = fContextService.getActiveContext();
134         if (context instanceof IStructuredSelection && !context.isEmpty()) {
135             Object JavaDoc[] elements = ((IStructuredSelection)context).toArray();
136             updateCommand(commandType, elements, new IAction[]{action});
137         } else {
138             action.setEnabled(false);
139         }
140     }
141     
142     private void postUpdate(ISelection context) {
143         Map JavaDoc commands = null;
144         synchronized (fCommandUpdates) {
145             commands = fCommandUpdates;
146             fCommandUpdates = new HashMap JavaDoc(commands.size());
147         }
148         if (context instanceof IStructuredSelection && !context.isEmpty()) {
149             Object JavaDoc[] elements = ((IStructuredSelection)context).toArray();
150             Iterator JavaDoc iterator = commands.entrySet().iterator();
151             while (iterator.hasNext()) {
152                 Entry entry = (Entry) iterator.next();
153                 Class JavaDoc commandType = (Class JavaDoc)entry.getKey();
154                 List JavaDoc actions = (List JavaDoc) entry.getValue();
155                 updateCommand(commandType, elements, (IAction[]) actions.toArray(new IAction[actions.size()]));
156             }
157         } else {
158             Iterator JavaDoc iterator = commands.values().iterator();
159             while (iterator.hasNext()) {
160                 List JavaDoc actionList = (List JavaDoc) iterator.next();
161                 Iterator JavaDoc actions = actionList.iterator();
162                 while (actions.hasNext()) {
163                     ((IAction)actions.next()).setEnabled(false);
164                 }
165             }
166         }
167         commands.clear();
168     }
169     
170     /**
171      * Updates the given command type for the specified elements.
172      *
173      * @param commandType command class to update
174      * @param elements elements to update for
175      * @param monitor status monitor
176      */

177     private void updateCommand(Class JavaDoc handlerType, Object JavaDoc[] elements, IAction[] actions) {
178         if (elements.length == 1) {
179             // usual case - one element
180
Object JavaDoc element = elements[0];
181             IDebugCommandHandler handler = getHandler(element, handlerType);
182             if (handler != null) {
183                 UpdateActionsRequest request = new UpdateActionsRequest(elements, actions);
184                 handler.canExecute(request);
185                 return;
186             }
187         } else {
188             Map JavaDoc map = collate(elements, handlerType);
189             if (map != null) {
190                 ActionsUpdater updater = new ActionsUpdater(actions, map.size());
191                 Iterator JavaDoc entries = map.entrySet().iterator();
192                 while (entries.hasNext()) {
193                     Entry entry = (Entry) entries.next();
194                     IDebugCommandHandler handler = (IDebugCommandHandler) entry.getKey();
195                     List JavaDoc list = (List JavaDoc) entry.getValue();
196                     UpdateHandlerRequest request = new UpdateHandlerRequest(list.toArray(), updater);
197                     handler.canExecute(request);
198                 }
199                 return;
200             }
201         }
202         // ABORT - no command processors
203
for (int i = 0; i < actions.length; i++) {
204             actions[i].setEnabled(false);
205         }
206     }
207     
208     /**
209      * Updates the given command type for the specified elements.
210      *
211      * @param commandType command class to update
212      * @param elements elements to update for
213      * @param monitor status monitor
214      */

215     public boolean executeCommand(Class JavaDoc handlerType, Object JavaDoc[] elements, ICommandParticipant participant) {
216         if (elements.length == 1) {
217             // usual case - one element
218
Object JavaDoc element = elements[0];
219             IDebugCommandHandler handler = getHandler(element, handlerType);
220             if (handler != null) {
221                 ExecuteActionRequest request = new ExecuteActionRequest(elements);
222                 request.setCommandParticipant(participant);
223                 return handler.execute(request);
224             }
225         } else {
226             Map JavaDoc map = collate(elements, handlerType);
227             if (map != null) {
228                 boolean enabled = true;
229                 Iterator JavaDoc entries = map.entrySet().iterator();
230                 while (entries.hasNext()) {
231                     Entry entry = (Entry) entries.next();
232                     IDebugCommandHandler handler = (IDebugCommandHandler) entry.getKey();
233                     List JavaDoc list = (List JavaDoc) entry.getValue();
234                     ExecuteActionRequest request = new ExecuteActionRequest(list.toArray());
235                     request.setCommandParticipant(participant);
236                     // specifically use & so handler is executed
237
enabled = enabled & handler.execute(request);
238                 }
239                 return enabled;
240             }
241         }
242         // ABORT - no command processors
243
return false;
244     }
245
246     public void debugContextChanged(DebugContextEvent event) {
247         postUpdate(event.getContext());
248     }
249     
250     /**
251      * Returns a map of command handlers to associated elements, or <code>null</code> if
252      * one is missing.
253      *
254      * @param elements
255      * @return map of command handlers to associated elements or <code>null</code>
256      */

257     private Map JavaDoc collate(Object JavaDoc[] elements, Class JavaDoc handlerType) {
258         Map JavaDoc map = new HashMap JavaDoc();
259         for (int i = 0; i < elements.length; i++) {
260             Object JavaDoc element = elements[i];
261             IDebugCommandHandler handler = getHandler(element, handlerType);
262             if (handler == null) {
263                 return null;
264             } else {
265                 List JavaDoc list = (List JavaDoc) map.get(handler);
266                 if (list == null) {
267                     list = new ArrayList JavaDoc();
268                     map.put(handler, list);
269                     }
270                 list.add(element);
271                 }
272             }
273         return map;
274     }
275     
276     private IDebugCommandHandler getHandler(Object JavaDoc element, Class JavaDoc handlerType) {
277         IDebugCommandHandler handler = null;
278         if (handlerType.isInstance(element)) {
279             handler = (IDebugCommandHandler)element;
280         } else if (element instanceof IAdaptable) {
281             handler = (IDebugCommandHandler)((IAdaptable)element).getAdapter(handlerType);
282             if (handler == null && !(element instanceof PlatformObject)) {
283                 // for objects that don't properly subclass PlatformObject to inherit default
284
// adapters, just delegate to the adapter factory
285
handler = (IDebugCommandHandler) new CommandAdapterFactory().getAdapter(element, handlerType);
286             }
287         }
288         return handler;
289     }
290
291 }
292
Popular Tags