| /******************************************************************************* |
| * Copyright (c) 2000, 2017 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.internal.ui.actions; |
| |
| import java.util.Iterator; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IWorkspace; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.dltk.ui.IContextMenuConstants; |
| 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.actions.CloseUnrelatedProjectsAction; |
| import org.eclipse.ui.ide.IDEActionFactory; |
| |
| /** |
| * Adds actions to open and close a project to the global menu bar. |
| * |
| * <p> |
| * This class may be instantiated; it is not intended to be subclassed. |
| * </p> |
| * |
| * |
| */ |
| public class ProjectActionGroup extends ActionGroup { |
| |
| private IWorkbenchSite fSite; |
| |
| private OpenProjectAction fOpenAction; |
| private CloseResourceAction fCloseAction; |
| private CloseResourceAction fCloseUnrelatedAction; |
| |
| /** |
| * Creates a new <code>ProjectActionGroup</code>. The group requires that |
| * the selection provided by the site's selection provider is of type <code> |
| * org.eclipse.jface.viewers.IStructuredSelection</code>. |
| * |
| * @param part |
| * the view part that owns this action group |
| */ |
| public ProjectActionGroup(IViewPart part) { |
| fSite = part.getSite(); |
| ISelectionProvider provider = fSite.getSelectionProvider(); |
| ISelection selection = provider.getSelection(); |
| |
| fCloseAction = new CloseResourceAction(fSite); |
| fCloseAction |
| .setActionDefinitionId("org.eclipse.ui.project.closeProject"); //$NON-NLS-1$ |
| |
| fCloseUnrelatedAction = new CloseUnrelatedProjectsAction(fSite); |
| fCloseUnrelatedAction.setActionDefinitionId( |
| "org.eclipse.ui.project.closeUnrelatedProjects"); //$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); |
| fCloseUnrelatedAction.selectionChanged(s); |
| } |
| provider.addSelectionChangedListener(fOpenAction); |
| provider.addSelectionChangedListener(fCloseAction); |
| provider.addSelectionChangedListener(fCloseUnrelatedAction); |
| IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| workspace.addResourceChangeListener(fOpenAction); |
| workspace.addResourceChangeListener(fCloseAction); |
| workspace.addResourceChangeListener(fCloseUnrelatedAction); |
| } |
| |
| @Override |
| public void fillActionBars(IActionBars actionBars) { |
| super.fillActionBars(actionBars); |
| actionBars.setGlobalActionHandler( |
| IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction); |
| actionBars.setGlobalActionHandler( |
| IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), |
| fCloseUnrelatedAction); |
| actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), |
| fOpenAction); |
| } |
| |
| @Override |
| 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); |
| if (fCloseUnrelatedAction.isEnabled() && areOnlyProjectsSelected( |
| fCloseUnrelatedAction.getStructuredSelection())) |
| menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, |
| fCloseUnrelatedAction); |
| } |
| |
| private boolean areOnlyProjectsSelected(IStructuredSelection selection) { |
| if (selection.isEmpty()) |
| return false; |
| |
| Iterator iter = selection.iterator(); |
| while (iter.hasNext()) { |
| Object obj = iter.next(); |
| if (obj instanceof IAdaptable) { |
| if (((IAdaptable) obj).getAdapter(IProject.class) == null) |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| /** |
| * Returns the open project action contained in this project action group. |
| * |
| * @return returns the open project action |
| * |
| * @since 3.3 |
| */ |
| public OpenProjectAction getOpenProjectAction() { |
| return fOpenAction; |
| } |
| |
| @Override |
| public void dispose() { |
| ISelectionProvider provider = fSite.getSelectionProvider(); |
| provider.removeSelectionChangedListener(fOpenAction); |
| provider.removeSelectionChangedListener(fCloseAction); |
| provider.removeSelectionChangedListener(fCloseUnrelatedAction); |
| IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| workspace.removeResourceChangeListener(fOpenAction); |
| workspace.removeResourceChangeListener(fCloseAction); |
| workspace.removeResourceChangeListener(fCloseUnrelatedAction); |
| super.dispose(); |
| } |
| } |