1 11 12 package org.eclipse.ui.internal.quickaccess; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.jface.action.ActionContributionItem; 20 import org.eclipse.jface.action.IContributionItem; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.SubContributionItem; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 26 import org.eclipse.ui.internal.WorkbenchImages; 27 import org.eclipse.ui.internal.WorkbenchWindow; 28 29 33 public class ActionProvider extends QuickAccessProvider { 34 35 private Map idToElement; 36 37 public String getId() { 38 return "org.eclipse.ui.actions"; } 40 41 public QuickAccessElement getElementForId(String id) { 42 getElements(); 43 return (ActionElement) idToElement.get(id); 44 } 45 46 public QuickAccessElement[] getElements() { 47 if (idToElement == null) { 48 idToElement = new HashMap (); 49 WorkbenchWindow window = (WorkbenchWindow) PlatformUI 50 .getWorkbench().getActiveWorkbenchWindow(); 51 if (window != null) { 52 MenuManager menu = window.getMenuManager(); 53 Set result = new HashSet (); 54 collectContributions(menu, result); 55 ActionContributionItem[] actions = (ActionContributionItem[]) result 56 .toArray(new ActionContributionItem[result.size()]); 57 for (int i = 0; i < actions.length; i++) { 58 ActionElement actionElement = new ActionElement(actions[i], 59 this); 60 idToElement.put(actionElement.getId(), actionElement); 61 } 62 } 63 } 64 return (ActionElement[]) idToElement.values().toArray( 65 new ActionElement[idToElement.values().size()]); 66 } 67 68 private void collectContributions(MenuManager menu, Set result) { 69 IContributionItem[] items = menu.getItems(); 70 for (int i = 0; i < items.length; i++) { 71 IContributionItem item = items[i]; 72 if (item instanceof SubContributionItem) { 73 item = ((SubContributionItem) item).getInnerItem(); 74 } 75 if (item instanceof MenuManager) { 76 collectContributions((MenuManager) item, result); 77 } else if (item instanceof ActionContributionItem 78 && item.isEnabled()) { 79 result.add(item); 80 } 81 } 82 } 83 84 public ImageDescriptor getImageDescriptor() { 85 return WorkbenchImages 86 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE); 87 } 88 89 public String getName() { 90 return QuickAccessMessages.QuickAccess_Menus; 91 } 92 } 93 | Popular Tags |