001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import java.util.Iterator;
013: import java.util.List;
014:
015: import org.eclipse.core.resources.IResource;
016:
017: import org.eclipse.jface.action.IMenuManager;
018: import org.eclipse.jface.action.MenuManager;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021:
022: import org.eclipse.ui.IWorkbenchSite;
023: import org.eclipse.ui.IWorkingSet;
024: import org.eclipse.ui.actions.ActionGroup;
025: import org.eclipse.ui.actions.NewWizardMenu;
026:
027: import org.eclipse.jdt.core.IJavaElement;
028:
029: import org.eclipse.jdt.ui.IContextMenuConstants;
030:
031: import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater;
032: import org.eclipse.jdt.internal.ui.workingsets.OthersWorkingSetUpdater;
033:
034: /**
035: * Action group that adds the 'new' menu to a context menu.
036: * <p>
037: * This class may be instantiated; it is not intended to be subclassed.
038: * </p>
039: *
040: * @since 2.1
041: */
042: public class NewWizardsActionGroup extends ActionGroup {
043:
044: private IWorkbenchSite fSite;
045:
046: /**
047: * Creates a new <code>NewWizardsActionGroup</code>. The group requires
048: * that the selection provided by the part's selection provider is of type <code>
049: * org.eclipse.jface.viewers.IStructuredSelection</code>.
050: *
051: * @param site the view part that owns this action group
052: */
053: public NewWizardsActionGroup(IWorkbenchSite site) {
054: fSite = site;
055: }
056:
057: /* (non-Javadoc)
058: * Method declared in ActionGroup
059: */
060: public void fillContextMenu(IMenuManager menu) {
061: super .fillContextMenu(menu);
062:
063: ISelection selection = getContext().getSelection();
064: if (selection instanceof IStructuredSelection) {
065: if (canEnable((IStructuredSelection) selection)) {
066: MenuManager newMenu = new MenuManager(
067: ActionMessages.NewWizardsActionGroup_new);
068: menu.appendToGroup(IContextMenuConstants.GROUP_NEW,
069: newMenu);
070: newMenu.add(new NewWizardMenu(fSite
071: .getWorkbenchWindow()));
072: }
073: }
074:
075: }
076:
077: private boolean canEnable(IStructuredSelection sel) {
078: if (sel.size() == 0)
079: return true;
080:
081: List list = sel.toList();
082: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
083: if (!isNewTarget(iterator.next()))
084: return false;
085: }
086:
087: return true;
088: }
089:
090: private boolean isNewTarget(Object element) {
091: if (element == null)
092: return true;
093: if (element instanceof IResource) {
094: return true;
095: }
096: if (element instanceof IJavaElement) {
097: int type = ((IJavaElement) element).getElementType();
098: return type == IJavaElement.JAVA_PROJECT
099: || 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 workingSetId = ((IWorkingSet) element).getId();
106: return JavaWorkingSetUpdater.ID.equals(workingSetId)
107: || "org.eclipse.ui.resourceWorkingSetPage".equals(workingSetId) || //$NON-NLS-1$
108: OthersWorkingSetUpdater.ID.equals(workingSetId);
109: }
110: return false;
111: }
112:
113: }
|