1 11 package org.eclipse.debug.internal.ui.commands.actions; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 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 41 public class DebugCommandService implements IDebugContextListener { 42 43 46 private Map fCommandUpdates = new HashMap (); 47 48 51 private IWorkbenchWindow fWindow = null; 52 53 56 private IDebugContextService fContextService = null; 57 58 61 private static Map fgServices = new HashMap (); 62 63 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 115 public void postUpdateCommand(Class commandType, Action action) { 116 synchronized (fCommandUpdates) { 117 List actions = (List ) fCommandUpdates.get(commandType); 118 if (actions == null) { 119 actions = new ArrayList (); 120 fCommandUpdates.put(commandType, actions); 121 } 122 actions.add(action); 123 } 124 } 125 126 132 public void updateCommand(Class commandType, IAction action) { 133 ISelection context = fContextService.getActiveContext(); 134 if (context instanceof IStructuredSelection && !context.isEmpty()) { 135 Object [] 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 commands = null; 144 synchronized (fCommandUpdates) { 145 commands = fCommandUpdates; 146 fCommandUpdates = new HashMap (commands.size()); 147 } 148 if (context instanceof IStructuredSelection && !context.isEmpty()) { 149 Object [] elements = ((IStructuredSelection)context).toArray(); 150 Iterator iterator = commands.entrySet().iterator(); 151 while (iterator.hasNext()) { 152 Entry entry = (Entry) iterator.next(); 153 Class commandType = (Class )entry.getKey(); 154 List actions = (List ) entry.getValue(); 155 updateCommand(commandType, elements, (IAction[]) actions.toArray(new IAction[actions.size()])); 156 } 157 } else { 158 Iterator iterator = commands.values().iterator(); 159 while (iterator.hasNext()) { 160 List actionList = (List ) iterator.next(); 161 Iterator actions = actionList.iterator(); 162 while (actions.hasNext()) { 163 ((IAction)actions.next()).setEnabled(false); 164 } 165 } 166 } 167 commands.clear(); 168 } 169 170 177 private void updateCommand(Class handlerType, Object [] elements, IAction[] actions) { 178 if (elements.length == 1) { 179 Object 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 map = collate(elements, handlerType); 189 if (map != null) { 190 ActionsUpdater updater = new ActionsUpdater(actions, map.size()); 191 Iterator entries = map.entrySet().iterator(); 192 while (entries.hasNext()) { 193 Entry entry = (Entry) entries.next(); 194 IDebugCommandHandler handler = (IDebugCommandHandler) entry.getKey(); 195 List list = (List ) entry.getValue(); 196 UpdateHandlerRequest request = new UpdateHandlerRequest(list.toArray(), updater); 197 handler.canExecute(request); 198 } 199 return; 200 } 201 } 202 for (int i = 0; i < actions.length; i++) { 204 actions[i].setEnabled(false); 205 } 206 } 207 208 215 public boolean executeCommand(Class handlerType, Object [] elements, ICommandParticipant participant) { 216 if (elements.length == 1) { 217 Object 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 map = collate(elements, handlerType); 227 if (map != null) { 228 boolean enabled = true; 229 Iterator entries = map.entrySet().iterator(); 230 while (entries.hasNext()) { 231 Entry entry = (Entry) entries.next(); 232 IDebugCommandHandler handler = (IDebugCommandHandler) entry.getKey(); 233 List list = (List ) entry.getValue(); 234 ExecuteActionRequest request = new ExecuteActionRequest(list.toArray()); 235 request.setCommandParticipant(participant); 236 enabled = enabled & handler.execute(request); 238 } 239 return enabled; 240 } 241 } 242 return false; 244 } 245 246 public void debugContextChanged(DebugContextEvent event) { 247 postUpdate(event.getContext()); 248 } 249 250 257 private Map collate(Object [] elements, Class handlerType) { 258 Map map = new HashMap (); 259 for (int i = 0; i < elements.length; i++) { 260 Object element = elements[i]; 261 IDebugCommandHandler handler = getHandler(element, handlerType); 262 if (handler == null) { 263 return null; 264 } else { 265 List list = (List ) map.get(handler); 266 if (list == null) { 267 list = new ArrayList (); 268 map.put(handler, list); 269 } 270 list.add(element); 271 } 272 } 273 return map; 274 } 275 276 private IDebugCommandHandler getHandler(Object element, Class 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 handler = (IDebugCommandHandler) new CommandAdapterFactory().getAdapter(element, handlerType); 286 } 287 } 288 return handler; 289 } 290 291 } 292 | Popular Tags |