1 11 12 package org.eclipse.ui.internal.quickaccess; 13 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.eclipse.core.commands.Command; 20 import org.eclipse.core.commands.ParameterizedCommand; 21 import org.eclipse.core.commands.common.NotDefinedException; 22 import org.eclipse.core.expressions.IEvaluationContext; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.commands.ICommandService; 26 import org.eclipse.ui.handlers.IHandlerService; 27 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 28 import org.eclipse.ui.internal.WorkbenchImages; 29 import org.eclipse.ui.internal.handlers.HandlerService; 30 31 35 public class CommandProvider extends QuickAccessProvider { 36 37 private Map idToElement; 38 private IEvaluationContext contextSnapshot; 39 private HandlerService realHandlerService; 40 41 public CommandProvider() { 42 saveApplicationContext(); 44 getElements(); 45 } 46 47 public String getId() { 48 return "org.eclipse.ui.commands"; } 50 51 public QuickAccessElement getElementForId(String id) { 52 getElements(); 53 return (CommandElement) idToElement.get(id); 54 } 55 56 public QuickAccessElement[] getElements() { 57 if (idToElement == null) { 58 idToElement = new HashMap (); 59 ICommandService commandService = (ICommandService) PlatformUI 60 .getWorkbench().getService(ICommandService.class); 61 final Collection commandIds = commandService.getDefinedCommandIds(); 62 final Iterator commandIdItr = commandIds.iterator(); 63 while (commandIdItr.hasNext()) { 64 final String currentCommandId = (String ) commandIdItr.next(); 65 final Command command = commandService 66 .getCommand(currentCommandId); 67 if (command != null && command.isHandled() 68 && command.isEnabled()) { 69 try { 70 Collection combinations = ParameterizedCommand 71 .generateCombinations(command); 72 for (Iterator it = combinations.iterator(); it 73 .hasNext();) { 74 ParameterizedCommand pc = (ParameterizedCommand) it.next(); 75 String id = pc.serialize(); 76 idToElement.put(id, 77 new CommandElement(pc, id, this)); 78 } 79 } catch (final NotDefinedException e) { 80 } 82 } 83 } 84 } 85 return (QuickAccessElement[]) idToElement.values().toArray( 86 new QuickAccessElement[idToElement.values().size()]); 87 } 88 89 public ImageDescriptor getImageDescriptor() { 90 return WorkbenchImages 91 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE); 92 } 93 94 public String getName() { 95 return QuickAccessMessages.QuickAccess_Commands; 96 } 97 98 private void saveApplicationContext() { 99 realHandlerService = (HandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); 100 contextSnapshot = realHandlerService.getFullContextSnapshot(); 101 } 102 103 HandlerService getRealHandlerService() { 104 return realHandlerService; 105 } 106 107 IEvaluationContext getContextSnapshot() { 108 return contextSnapshot; 109 } 110 } 111 | Popular Tags |