KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > BaseNewWizardMenu


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.IRegistryChangeEvent;
22 import org.eclipse.core.runtime.IRegistryChangeListener;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.ActionContributionItem;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IContributionItem;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.activities.WorkbenchActivityHelper;
34 import org.eclipse.ui.internal.WorkbenchMessages;
35 import org.eclipse.ui.internal.WorkbenchPlugin;
36 import org.eclipse.ui.internal.WorkbenchWindow;
37 import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
38 import org.eclipse.ui.internal.util.Util;
39 import org.eclipse.ui.wizards.IWizardDescriptor;
40
41 /**
42  * A <code>BaseNewWizardMenu</code> is used to populate a menu manager with
43  * New Wizard actions for the current perspective's new wizard shortcuts,
44  * including an Other... action to open the new wizard dialog.
45  *
46  * @since 3.1
47  */

48 public class BaseNewWizardMenu extends CompoundContributionItem {
49     /*
50      * @issue Should be possible to implement this class entirely using public
51      * API. Cases to be fixed: WorkbenchWindow, NewWizardShortcutAction
52      * Suggestions:
53      * - add the ability to update the submenus of a window
54      */

55
56     private final Map JavaDoc actions = new HashMap JavaDoc(21);
57
58     private final IExtensionChangeHandler configListener = new IExtensionChangeHandler() {
59
60         /* (non-Javadoc)
61          * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
62          */

63         public void removeExtension(IExtension source, Object JavaDoc[] objects) {
64             for (int i = 0; i < objects.length; i++) {
65                 if (objects[i] instanceof NewWizardShortcutAction) {
66                     actions.values().remove(objects[i]);
67                 }
68             }
69         }
70
71         /* (non-Javadoc)
72          * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
73          */

74         public void addExtension(IExtensionTracker tracker, IExtension extension) {
75             // Do nothing
76
}
77     };
78
79     /**
80      * TODO: should this be done with an addition listener?
81      */

82     private final IRegistryChangeListener registryListener = new IRegistryChangeListener() {
83
84         public void registryChanged(IRegistryChangeEvent event) {
85             // reset the reader.
86
// TODO This is expensive. Can we be more selective?
87
if (getParent() != null) {
88                 getParent().markDirty();
89             }
90         }
91
92     };
93
94     private final IAction showDlgAction;
95
96     private IWorkbenchWindow workbenchWindow;
97
98     /**
99      * Creates a new wizard shortcut menu for the IDE.
100      *
101      * @param window
102      * the window containing the menu
103      * @param id
104      * the contribution item identifier, or <code>null</code>
105      */

106     public BaseNewWizardMenu(IWorkbenchWindow window, String JavaDoc id) {
107         super(id);
108         Assert.isNotNull(window);
109         this.workbenchWindow = window;
110         showDlgAction = ActionFactory.NEW.create(window);
111         registerListeners();
112         // indicate that a new wizards submenu has been created
113
((WorkbenchWindow) window)
114                 .addSubmenu(WorkbenchWindow.NEW_WIZARD_SUBMENU);
115     }
116
117     /**
118      * Adds the items to show to the given list.
119      *
120      * @param list the list to add items to
121      */

122     protected void addItems(List JavaDoc list) {
123         if (addShortcuts(list)) {
124             list.add(new Separator());
125         }
126         list.add(new ActionContributionItem(getShowDialogAction()));
127     }
128
129     /**
130      * Adds the new wizard shortcuts for the current perspective to the given list.
131      *
132      * @param list the list to add items to
133      * @return <code>true</code> if any items were added, <code>false</code> if none were added
134      */

135     protected boolean addShortcuts(List JavaDoc list) {
136         boolean added = false;
137         IWorkbenchPage page = workbenchWindow.getActivePage();
138         if (page != null) {
139             String JavaDoc[] wizardIds = page.getNewWizardShortcuts();
140             for (int i = 0; i < wizardIds.length; i++) {
141                 IAction action = getAction(wizardIds[i]);
142                 if (action != null) {
143                     if (!WorkbenchActivityHelper.filterItem(action)) {
144                         list.add(new ActionContributionItem(action));
145                         added = true;
146                     }
147                 }
148             }
149         }
150         return added;
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.eclipse.jface.action.IContributionItem#dispose()
157      */

158     public void dispose() {
159         if (workbenchWindow != null) {
160             super.dispose();
161             unregisterListeners();
162             workbenchWindow = null;
163         }
164     }
165
166     /*
167      * (non-Javadoc) Returns the action for the given wizard id, or null if not
168      * found.
169      */

170     private IAction getAction(String JavaDoc id) {
171         // Keep a cache, rather than creating a new action each time,
172
// so that image caching in ActionContributionItem works.
173
IAction action = (IAction) actions.get(id);
174         if (action == null) {
175             IWizardDescriptor wizardDesc = WorkbenchPlugin.getDefault()
176                     .getNewWizardRegistry().findWizard(id);
177             if (wizardDesc != null) {
178                 action = new NewWizardShortcutAction(workbenchWindow,
179                         wizardDesc);
180                 actions.put(id, action);
181                 IConfigurationElement element = (IConfigurationElement) Util
182                         .getAdapter(wizardDesc, IConfigurationElement.class);
183                 if (element != null) {
184                     workbenchWindow.getExtensionTracker().registerObject(
185                             element.getDeclaringExtension(), action,
186                             IExtensionTracker.REF_WEAK);
187                 }
188             }
189         }
190         return action;
191     }
192
193     /*
194      * (non-Javadoc)
195      *
196      * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
197      */

198     protected IContributionItem[] getContributionItems() {
199         ArrayList JavaDoc list = new ArrayList JavaDoc();
200         if (workbenchWindow != null && workbenchWindow.getActivePage() != null
201                 && workbenchWindow.getActivePage().getPerspective() != null) {
202             addItems(list);
203         } else {
204             String JavaDoc text = WorkbenchMessages.Workbench_noApplicableItems;
205             Action dummyAction = new Action(text) {
206                 // dummy inner class; no methods
207
};
208             dummyAction.setEnabled(false);
209             list.add(new ActionContributionItem(dummyAction));
210         }
211         return (IContributionItem[]) list.toArray(new IContributionItem[list.size()]);
212     }
213
214     /**
215      * Returns the "Other..." action, used to show the new wizards dialog.
216      *
217      * @return the action used to show the new wizards dialog
218      */

219     protected IAction getShowDialogAction() {
220         return showDlgAction;
221     }
222
223     /**
224      * Returns the window in which this menu appears.
225      *
226      * @return the window in which this menu appears
227      */

228     protected IWorkbenchWindow getWindow() {
229         return workbenchWindow;
230     }
231
232     /**
233      * Registers listeners.
234      *
235      * @since 3.1
236      */

237     private void registerListeners() {
238         Platform.getExtensionRegistry().addRegistryChangeListener(
239                 registryListener);
240         workbenchWindow.getExtensionTracker().registerHandler(
241                 configListener, null);
242     }
243
244     /**
245      * Returns whether the new wizards registry has a non-empty category with
246      * the given identifier.
247      *
248      * @param categoryId
249      * the identifier for the category
250      * @return <code>true</code> if there is a non-empty category with the
251      * given identifier, <code>false</code> otherwise
252      */

253     protected boolean registryHasCategory(String JavaDoc categoryId) {
254         return WorkbenchPlugin.getDefault().getNewWizardRegistry()
255                 .findCategory(categoryId) != null;
256     }
257
258     /**
259      * Unregisters listeners.
260      *
261      * @since 3.1
262      */

263     private void unregisterListeners() {
264         Platform.getExtensionRegistry().removeRegistryChangeListener(
265                 registryListener);
266         workbenchWindow.getExtensionTracker().unregisterHandler(configListener);
267     }
268 }
269
Popular Tags