Source code: org/eclipse/ui/internal/PopupMenuExtender.java
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Dan Rubel (dan_rubel@instantiations.com) - accessor to get menu id
11 *******************************************************************************/
12 package org.eclipse.ui.internal;
13
14
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.Set;
18
19 import org.eclipse.jface.action.IContributionItem;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.jface.action.SubMenuManager;
25 import org.eclipse.jface.viewers.ISelectionProvider;
26 import org.eclipse.ui.IWorkbenchActionConstants;
27 import org.eclipse.ui.IWorkbenchPart;
28
29 /**
30 * This class extends a single popup menu
31 */
32 public class PopupMenuExtender implements IMenuListener {
33
34 //for dynamic UI
35 public static final class PopupMenuExtenderManager {
36
37 private Set extenders = new HashSet();
38
39 public void clearCaches() {
40 for (Iterator i = extenders.iterator(); i.hasNext();) {
41 PopupMenuExtender extender = (PopupMenuExtender) i.next();
42 extender.readStaticActions();
43 }
44 }
45
46 private void addExtender(PopupMenuExtender extender) {
47 extenders.add(extender);
48 }
49
50 private void removeExtender(PopupMenuExtender extender) {
51 extenders.remove(extender);
52 }
53 }
54
55 private static final PopupMenuExtenderManager manager = new PopupMenuExtenderManager();
56
57 public static final PopupMenuExtenderManager getManager() {
58 return manager;
59 }
60
61 private String menuID;
62 private MenuManager menu;
63 private SubMenuManager menuWrapper;
64 private ISelectionProvider selProvider;
65 private IWorkbenchPart part;
66 private ViewerActionBuilder staticActionBuilder;
67
68 /**
69 * Construct a new menu extender.
70 */
71 public PopupMenuExtender(String id, MenuManager menu, ISelectionProvider prov, IWorkbenchPart part) {
72 super();
73 this.menuID = id;
74 this.menu = menu;
75 this.selProvider = prov;
76 this.part = part;
77 menu.addMenuListener(this);
78 if (!menu.getRemoveAllWhenShown()) {
79 menuWrapper = new SubMenuManager(menu);
80 menuWrapper.setVisible(true);
81 }
82 getManager().addExtender(this);
83 readStaticActions();
84 }
85 // getMenuId() added by Dan Rubel (dan_rubel@instantiations.com)
86 /**
87 * Return the menu identifier
88 */
89 public String getMenuId() {
90 return menuID;
91 }
92 /**
93 * Contributes items registered for the object type(s) in
94 * the current selection.
95 */
96 private void addObjectActions(IMenuManager mgr) {
97 if (selProvider != null) {
98 if (ObjectActionContributorManager.getManager().contributeObjectActions(part, mgr, selProvider)) {
99 mgr.add(new Separator());
100 }
101 }
102 }
103 /**
104 * Adds static items to the context menu.
105 */
106 private void addStaticActions(IMenuManager mgr) {
107 if (staticActionBuilder != null)
108 staticActionBuilder.contribute(mgr, null, true);
109 }
110 /**
111 * Notifies the listener that the menu is about to be shown.
112 */
113 public void menuAboutToShow(IMenuManager mgr) {
114 testForAdditions();
115 if (menuWrapper != null) {
116 mgr = menuWrapper;
117 menuWrapper.removeAll();
118 }
119 addObjectActions(mgr);
120 addStaticActions(mgr);
121 }
122 /**
123 * Read static items for the context menu.
124 */
125 private void readStaticActions() {
126 // If no menu id provided, then there is no contributions
127 // to add. Fix for bug #33140.
128 if (menuID != null && menuID.length() > 0) {
129 staticActionBuilder = new ViewerActionBuilder();
130 if (!staticActionBuilder.readViewerContributions(menuID, selProvider, part))
131 staticActionBuilder = null;
132 }
133 }
134 /**
135 * Checks for the existance of an MB_ADDITIONS group.
136 */
137 private void testForAdditions() {
138 IContributionItem item = menu.find(IWorkbenchActionConstants.MB_ADDITIONS);
139 if (item == null) {
140 WorkbenchPlugin.log("Context menu missing standard group 'org.eclipse.ui.IWorkbenchActionConstants.MB_ADDITIONS'. (menu id = " //$NON-NLS-1$
141 + (menuID == null ? "???" : menuID) //$NON-NLS-1$
142 +") part id = " //$NON-NLS-1$
143 + (part == null ? "???" : part.getSite().getId()) //$NON-NLS-1$
144 +")"); //$NON-NLS-1$
145 }
146 }
147 /**
148 * Dispose of the menu extender. Should only be called when the part
149 * is disposed.
150 */
151 public void dispose() {
152 if (staticActionBuilder != null) {
153 staticActionBuilder.dispose();
154 }
155 getManager().removeExtender(this);
156 }
157 }