KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > NewWizardsActionGroup


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.jdt.internal.ui.actions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.IWorkingSet;
25 import org.eclipse.ui.actions.ActionGroup;
26 import org.eclipse.ui.actions.NewWizardMenu;
27
28 import org.eclipse.jdt.core.IJavaElement;
29
30 import org.eclipse.jdt.ui.IContextMenuConstants;
31
32 import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater;
33 import org.eclipse.jdt.internal.ui.workingsets.OthersWorkingSetUpdater;
34
35
36 /**
37  * Action group that adds the 'new' menu to a context menu.
38  * <p>
39  * This class may be instantiated; it is not intended to be subclassed.
40  * </p>
41  *
42  * @since 2.1
43  */

44 public class NewWizardsActionGroup extends ActionGroup {
45
46     private IWorkbenchSite fSite;
47     
48     /**
49      * Creates a new <code>NewWizardsActionGroup</code>. The group requires
50      * that the selection provided by the part's selection provider is of type <code>
51      * org.eclipse.jface.viewers.IStructuredSelection</code>.
52      *
53      * @param site the view part that owns this action group
54      */

55     public NewWizardsActionGroup(IWorkbenchSite site) {
56         fSite= site;
57     }
58     
59
60     /* (non-Javadoc)
61      * Method declared in ActionGroup
62      */

63     public void fillContextMenu(IMenuManager menu) {
64         super.fillContextMenu(menu);
65         
66         ISelection selection= getContext().getSelection();
67         if (selection instanceof IStructuredSelection) {
68             if (canEnable((IStructuredSelection)selection)) {
69                 MenuManager newMenu = new MenuManager(ActionMessages.NewWizardsActionGroup_new);
70                 menu.appendToGroup(IContextMenuConstants.GROUP_NEW, newMenu);
71                 newMenu.add(new NewWizardMenu(fSite.getWorkbenchWindow()));
72             }
73         }
74         
75     }
76     
77     private boolean canEnable(IStructuredSelection sel) {
78         if (sel.size() == 0)
79             return true;
80         
81         List JavaDoc list= sel.toList();
82         for (Iterator JavaDoc iterator= list.iterator(); iterator.hasNext();) {
83             if (!isNewTarget(iterator.next()))
84                 return false;
85         }
86
87         return true;
88     }
89     
90     private boolean isNewTarget(Object JavaDoc element) {
91         if (element == null)
92             return true;
93         if (element instanceof IResource) {
94             return true;
95         }
96         if (element instanceof IJavaElement) {
97             int type= ((IJavaElement)element).getElementType();
98             return type == IJavaElement.JAVA_PROJECT ||
99                 type == IJavaElement.PACKAGE_FRAGMENT_ROOT ||
100                 type == IJavaElement.PACKAGE_FRAGMENT ||
101                 type == IJavaElement.COMPILATION_UNIT ||
102                 type == IJavaElement.TYPE;
103         }
104         if (element instanceof IWorkingSet) {
105             String JavaDoc workingSetId= ((IWorkingSet)element).getId();
106             return
107                 JavaWorkingSetUpdater.ID.equals(workingSetId) ||
108                 "org.eclipse.ui.resourceWorkingSetPage".equals(workingSetId) || //$NON-NLS-1$
109
OthersWorkingSetUpdater.ID.equals(workingSetId);
110         }
111         return false;
112     }
113
114 }
115
Popular Tags