KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > NewActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.internal.navigator.resources.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.action.Separator;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.actions.ActionFactory;
20 import org.eclipse.ui.actions.NewExampleAction;
21 import org.eclipse.ui.actions.NewProjectAction;
22 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages;
23 import org.eclipse.ui.navigator.CommonActionProvider;
24 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
25 import org.eclipse.ui.navigator.ICommonMenuConstants;
26 import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
27 import org.eclipse.ui.navigator.WizardActionGroup;
28 import org.eclipse.ui.wizards.IWizardCategory;
29 import org.eclipse.ui.wizards.IWizardRegistry;
30
31 /**
32  * Provides the new (artifact creation) menu options for a context menu.
33  *
34  * <p>
35  * The added submenu has the following structure
36  * </p>
37  *
38  * <ul>
39  * <li>a new generic project wizard shortcut action, </li>
40  * <li>a separator, </li>
41  * <li>a set of context senstive wizard shortcuts (as defined by
42  * <b>org.eclipse.ui.navigator.commonWizard</b>), </li>
43  * <li>another separator, </li>
44  * <li>a generic examples wizard shortcut action, and finally </li>
45  * <li>a generic "Other" new wizard shortcut action</li>
46  * </ul>
47  *
48  * @since 3.2
49  *
50  */

51 public class NewActionProvider extends CommonActionProvider {
52
53     private static final String JavaDoc FULL_EXAMPLES_WIZARD_CATEGORY = "org.eclipse.ui.Examples"; //$NON-NLS-1$
54

55     private static final String JavaDoc NEW_MENU_NAME = "common.new.menu";//$NON-NLS-1$
56

57     private IAction showDlgAction;
58
59     private IAction newProjectAction;
60
61     private IAction newExampleAction;
62
63     private WizardActionGroup newWizardActionGroup;
64
65     private boolean contribute = false;
66
67     public void init(ICommonActionExtensionSite anExtensionSite) {
68
69         if (anExtensionSite.getViewSite() instanceof ICommonViewerWorkbenchSite) {
70             IWorkbenchWindow window = ((ICommonViewerWorkbenchSite) anExtensionSite.getViewSite()).getWorkbenchWindow();
71             showDlgAction = ActionFactory.NEW.create(window);
72             newProjectAction = new NewProjectAction(window);
73             newExampleAction = new NewExampleAction(window);
74
75             newWizardActionGroup = new WizardActionGroup(window, PlatformUI.getWorkbench().getNewWizardRegistry(), WizardActionGroup.TYPE_NEW, anExtensionSite.getContentService());
76
77             contribute = true;
78         }
79     }
80
81     /**
82      * Adds a submenu to the given menu with the name "group.new" see
83      * {@link ICommonMenuConstants#GROUP_NEW}). The submenu contains the following structure:
84      *
85      * <ul>
86      * <li>a new generic project wizard shortcut action, </li>
87      * <li>a separator, </li>
88      * <li>a set of context senstive wizard shortcuts (as defined by
89      * <b>org.eclipse.ui.navigator.commonWizard</b>), </li>
90      * <li>another separator, </li>
91      * <li>a generic examples wizard shortcut action, and finally </li>
92      * <li>a generic "Other" new wizard shortcut action</li>
93      * </ul>
94      */

95     public void fillContextMenu(IMenuManager menu) {
96         IMenuManager submenu = new MenuManager(
97                 WorkbenchNavigatorMessages.NewActionProvider_NewMenu_label,
98                 NEW_MENU_NAME);
99         if(!contribute) {
100             return;
101         }
102         // Add new project wizard shortcut
103
submenu.add(newProjectAction);
104         submenu.add(new Separator());
105
106         // fill the menu from the commonWizard contributions
107
newWizardActionGroup.setContext(getContext());
108         newWizardActionGroup.fillContextMenu(submenu);
109
110         submenu.add(new Separator(ICommonMenuConstants.GROUP_ADDITIONS));
111
112         // if there are examples, then add them to the end of the menu
113
if (hasExamples()) {
114             submenu.add(new Separator());
115             submenu.add(newExampleAction);
116         }
117
118         // Add other ..
119
submenu.add(new Separator());
120         submenu.add(showDlgAction);
121
122         // append the submenu after the GROUP_NEW group.
123
menu.insertAfter(ICommonMenuConstants.GROUP_NEW, submenu);
124     }
125
126     /**
127      * Return whether or not any examples are in the current install.
128      *
129      * @return True if there exists a full examples wizard category.
130      */

131     private boolean hasExamples() {
132         IWizardRegistry newRegistry = PlatformUI.getWorkbench().getNewWizardRegistry();
133         IWizardCategory category = newRegistry.findCategory(FULL_EXAMPLES_WIZARD_CATEGORY);
134         return category != null;
135
136     }
137
138 }
139
Popular Tags