KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.action.ActionContributionItem;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IContributionItem;
20 import org.eclipse.jface.action.IContributionManager;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
25 import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
26 import org.eclipse.ui.internal.registry.WizardsRegistryReader;
27 import org.eclipse.ui.wizards.IWizardDescriptor;
28
29 /**
30  * A <code>NewWizardMenu</code> augments <code>BaseNewWizardMenu</code> with IDE-specific
31  * actions: New Project... (always shown) and New Example... (shown only if there are example wizards installed).
32  */

33 public class NewWizardMenu extends BaseNewWizardMenu {
34
35     private final IAction newExampleAction;
36     private final IAction newProjectAction;
37
38     private boolean enabled = true;
39
40     /**
41      * Creates a new wizard shortcut menu for the IDE.
42      *
43      * @param window
44      * the window containing the menu
45      */

46     public NewWizardMenu(IWorkbenchWindow window) {
47         this(window, null);
48         
49     }
50     
51     /**
52      * Creates a new wizard shortcut menu for the IDE.
53      *
54      * @param window
55      * the window containing the menu
56      * @param id
57      * the identifier for this contribution item
58      */

59     public NewWizardMenu(IWorkbenchWindow window, String JavaDoc id) {
60         super(window, id);
61         newExampleAction = new NewExampleAction(window);
62         newProjectAction = new NewProjectAction(window);
63     }
64
65     /**
66      * Create a new wizard shortcut menu.
67      * <p>
68      * If the menu will appear on a semi-permanent basis, for instance within
69      * a toolbar or menubar, the value passed for <code>register</code> should be true.
70      * If set, the menu will listen to perspective activation and update itself
71      * to suit. In this case clients are expected to call <code>deregister</code>
72      * when the menu is no longer needed. This will unhook any perspective
73      * listeners.
74      * </p>
75      *
76      * @param innerMgr the location for the shortcut menu contents
77      * @param window the window containing the menu
78      * @param register if <code>true</code> the menu listens to perspective changes in
79      * the window
80      * @deprecated use NewWizardMenu(IWorkbenchWindow) instead
81      */

82     public NewWizardMenu(IMenuManager innerMgr, IWorkbenchWindow window,
83             boolean register) {
84         this(window, null);
85         fillMenu(innerMgr);
86         // Must be done after constructor to ensure field initialization.
87
}
88     
89     /* (non-Javadoc)
90      * Fills the menu with New Wizards.
91      */

92     private void fillMenu(IContributionManager innerMgr) {
93         // Remove all.
94
innerMgr.removeAll();
95
96         IContributionItem[] items = getContributionItems();
97         for (int i = 0; i < items.length; i++) {
98             innerMgr.add(items[i]);
99         }
100     }
101
102     /**
103      * Removes all listeners from the containing workbench window.
104      * <p>
105      * This method should only be called if the shortcut menu is created with
106      * <code>register = true</code>.
107      * </p>
108      *
109      * @deprecated has no effect
110      */

111     public void deregisterListeners() {
112         // do nothing
113
}
114
115     /**
116      * Return whether or not any examples are in the current install.
117      *
118      * @return boolean
119      */

120     private boolean hasExamples() {
121         return registryHasCategory(WizardsRegistryReader.FULL_EXAMPLES_WIZARD_CATEGORY);
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.ui.actions.BaseNewWizardMenu#addItems(org.eclipse.jface.action.IContributionManager)
126      */

127     protected void addItems(List JavaDoc list) {
128         ArrayList JavaDoc shortCuts= new ArrayList JavaDoc();
129         addShortcuts(shortCuts);
130         
131         for (Iterator JavaDoc iterator= shortCuts.iterator(); iterator.hasNext();) {
132             Object JavaDoc curr= iterator.next();
133             if (curr instanceof ActionContributionItem && isNewProjectWizardAction(((ActionContributionItem) curr).getAction())) {
134                 iterator.remove();
135                 list.add(curr);
136             }
137         }
138         list.add(new ActionContributionItem(newProjectAction));
139         list.add(new Separator());
140         if (!shortCuts.isEmpty()) {
141             list.addAll(shortCuts);
142             list.add(new Separator());
143         }
144         if (hasExamples()) {
145             list.add(new ActionContributionItem(newExampleAction));
146             list.add(new Separator());
147         }
148         list.add(new ActionContributionItem(getShowDialogAction()));
149     }
150
151     private boolean isNewProjectWizardAction(IAction action) {
152         if (action instanceof NewWizardShortcutAction) {
153             IWizardDescriptor wizardDescriptor= ((NewWizardShortcutAction) action).getWizardDescriptor();
154             String JavaDoc [] tags = wizardDescriptor.getTags();
155             for (int i = 0; i < tags.length; i++) {
156                 if (WorkbenchWizardElement.TAG_PROJECT.equals(tags[i])) {
157                     return true;
158                 }
159             }
160         }
161         return false;
162     }
163     
164     /* (non-Javadoc)
165      * Method declared on IContributionItem.
166      */

167     public boolean isEnabled() {
168         return enabled;
169     }
170
171     /**
172      * Sets the enabled state of the receiver.
173      *
174      * @param enabledValue if <code>true</code> the menu is enabled; else
175      * it is disabled
176      */

177     public void setEnabled(boolean enabledValue) {
178         this.enabled = enabledValue;
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.ui.actions.BaseNewWizardMenu#getContributionItems()
183      */

184     protected IContributionItem[] getContributionItems() {
185         if (isEnabled()) {
186             return super.getContributionItems();
187         }
188         return new IContributionItem[0];
189     }
190 }
191
Popular Tags