01: /*******************************************************************************
02: * Copyright (c) 2006, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.quickaccess;
11:
12: import java.util.HashMap;
13: import java.util.HashSet;
14: import java.util.Map;
15: import java.util.Set;
16:
17: import org.eclipse.jface.action.ActionContributionItem;
18: import org.eclipse.jface.action.IContributionItem;
19: import org.eclipse.jface.action.MenuManager;
20: import org.eclipse.jface.action.SubContributionItem;
21: import org.eclipse.jface.resource.ImageDescriptor;
22: import org.eclipse.ui.PlatformUI;
23: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
24: import org.eclipse.ui.internal.WorkbenchImages;
25: import org.eclipse.ui.internal.WorkbenchWindow;
26:
27: /**
28: * @since 3.3
29: *
30: */
31: public class ActionProvider extends QuickAccessProvider {
32:
33: private Map idToElement;
34:
35: public String getId() {
36: return "org.eclipse.ui.actions"; //$NON-NLS-1$
37: }
38:
39: public QuickAccessElement getElementForId(String id) {
40: getElements();
41: return (ActionElement) idToElement.get(id);
42: }
43:
44: public QuickAccessElement[] getElements() {
45: if (idToElement == null) {
46: idToElement = new HashMap();
47: WorkbenchWindow window = (WorkbenchWindow) PlatformUI
48: .getWorkbench().getActiveWorkbenchWindow();
49: if (window != null) {
50: MenuManager menu = window.getMenuManager();
51: Set result = new HashSet();
52: collectContributions(menu, result);
53: ActionContributionItem[] actions = (ActionContributionItem[]) result
54: .toArray(new ActionContributionItem[result
55: .size()]);
56: for (int i = 0; i < actions.length; i++) {
57: ActionElement actionElement = new ActionElement(
58: actions[i], this );
59: idToElement.put(actionElement.getId(),
60: 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: }
|