KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > quickaccess > ActionProvider


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
12 package org.eclipse.ui.internal.quickaccess;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
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 /**
30  * @since 3.3
31  *
32  */

33 public class ActionProvider extends QuickAccessProvider {
34
35     private Map JavaDoc idToElement;
36
37     public String JavaDoc getId() {
38         return "org.eclipse.ui.actions"; //$NON-NLS-1$
39
}
40
41     public QuickAccessElement getElementForId(String JavaDoc id) {
42         getElements();
43         return (ActionElement) idToElement.get(id);
44     }
45
46     public QuickAccessElement[] getElements() {
47         if (idToElement == null) {
48             idToElement = new HashMap JavaDoc();
49             WorkbenchWindow window = (WorkbenchWindow) PlatformUI
50                     .getWorkbench().getActiveWorkbenchWindow();
51             if (window != null) {
52                 MenuManager menu = window.getMenuManager();
53                 Set JavaDoc result = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc getName() {
90         return QuickAccessMessages.QuickAccess_Menus;
91     }
92 }
93
Popular Tags