/******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package net.sourceforge.phpdt.ui.actions; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.swt.widgets.Shell; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchSite; import org.eclipse.ui.actions.ActionGroup; import org.eclipse.ui.actions.CloseResourceAction; import org.eclipse.ui.ide.IDEActionFactory; import net.sourceforge.phpdt.ui.IContextMenuConstants; /** * Adds actions to open and close a project to the global menu bar. * *
* This class may be instantiated; it is not intended to be subclassed. *
* * @since 2.0 */ public class ProjectActionGroup extends ActionGroup { private IWorkbenchSite fSite; private OpenProjectAction fOpenAction; private CloseResourceAction fCloseAction; /** * Creates a newProjectActionGroup
. The group requires
* that the selection provided by the site's selection provider is of type
* org.eclipse.jface.viewers.IStructuredSelection
.
*
* @param part the view part that owns this action group
*/
public ProjectActionGroup(IViewPart part) {
fSite = part.getSite();
Shell shell= fSite.getShell();
ISelectionProvider provider= fSite.getSelectionProvider();
ISelection selection= provider.getSelection();
fCloseAction= new CloseResourceAction(shell);
fCloseAction.setActionDefinitionId("org.eclipse.ui.project.closeProject"); //$NON-NLS-1$
fOpenAction= new OpenProjectAction(fSite);
fOpenAction.setActionDefinitionId("org.eclipse.ui.project.openProject"); //$NON-NLS-1$
if (selection instanceof IStructuredSelection) {
IStructuredSelection s= (IStructuredSelection)selection;
fOpenAction.selectionChanged(s);
fCloseAction.selectionChanged(s);
}
provider.addSelectionChangedListener(fOpenAction);
provider.addSelectionChangedListener(fCloseAction);
IWorkspace workspace= ResourcesPlugin.getWorkspace();
workspace.addResourceChangeListener(fOpenAction);
workspace.addResourceChangeListener(fCloseAction);
}
/* (non-Javadoc)
* Method declared in ActionGroup
*/
public void fillActionBars(IActionBars actionBars) {
super.fillActionBars(actionBars);
actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction);
actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fOpenAction);
}
/* (non-Javadoc)
* Method declared in ActionGroup
*/
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
if (fOpenAction.isEnabled())
menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fOpenAction);
if (fCloseAction.isEnabled())
menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseAction);
}
/*
* @see ActionGroup#dispose()
*/
public void dispose() {
ISelectionProvider provider= fSite.getSelectionProvider();
provider.removeSelectionChangedListener(fOpenAction);
provider.removeSelectionChangedListener(fCloseAction);
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.removeResourceChangeListener(fOpenAction);
workspace.removeResourceChangeListener(fCloseAction);
super.dispose();
}
}