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.ui.actions;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IAdaptable;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IWorkspace;
020: import org.eclipse.core.resources.ResourcesPlugin;
021:
022: import org.eclipse.swt.widgets.Shell;
023:
024: import org.eclipse.jface.action.IMenuManager;
025: import org.eclipse.jface.viewers.ISelection;
026: import org.eclipse.jface.viewers.ISelectionChangedListener;
027: import org.eclipse.jface.viewers.ISelectionProvider;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.SelectionChangedEvent;
030: import org.eclipse.jface.viewers.StructuredSelection;
031:
032: import org.eclipse.ui.IActionBars;
033: import org.eclipse.ui.IViewPart;
034: import org.eclipse.ui.IWorkbenchSite;
035: import org.eclipse.ui.IWorkingSet;
036: import org.eclipse.ui.actions.ActionGroup;
037: import org.eclipse.ui.actions.CloseResourceAction;
038: import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
039:
040: import org.eclipse.ui.ide.IDEActionFactory;
041:
042: import org.eclipse.jdt.core.IJavaProject;
043:
044: import org.eclipse.jdt.ui.IContextMenuConstants;
045:
046: /**
047: * Adds actions to open and close a project to the global menu bar.
048: *
049: * <p>
050: * This class may be instantiated; it is not intended to be subclassed.
051: * </p>
052: *
053: * @since 2.0
054: */
055: public class ProjectActionGroup extends ActionGroup {
056:
057: private IWorkbenchSite fSite;
058:
059: private OpenProjectAction fOpenAction;
060: private CloseResourceAction fCloseAction;
061: private CloseResourceAction fCloseUnrelatedAction;
062:
063: private ISelectionChangedListener fSelectionChangedListener;
064:
065: /**
066: * Creates a new <code>ProjectActionGroup</code>. The group requires
067: * that the selection provided by the site's selection provider is of type <code>
068: * org.eclipse.jface.viewers.IStructuredSelection</code>.
069: *
070: * @param part the view part that owns this action group
071: */
072: public ProjectActionGroup(IViewPart part) {
073: fSite = part.getSite();
074: Shell shell = fSite.getShell();
075: ISelectionProvider provider = fSite.getSelectionProvider();
076: ISelection selection = provider.getSelection();
077:
078: fCloseAction = new CloseResourceAction(shell);
079: fCloseAction
080: .setActionDefinitionId("org.eclipse.ui.project.closeProject"); //$NON-NLS-1$
081:
082: fCloseUnrelatedAction = new CloseUnrelatedProjectsAction(shell);
083: fCloseUnrelatedAction
084: .setActionDefinitionId("org.eclipse.ui.project.closeUnrelatedProjects"); //$NON-NLS-1$
085:
086: fOpenAction = new OpenProjectAction(fSite);
087: fOpenAction
088: .setActionDefinitionId("org.eclipse.ui.project.openProject"); //$NON-NLS-1$
089: if (selection instanceof IStructuredSelection) {
090: IStructuredSelection s = (IStructuredSelection) selection;
091: fOpenAction.selectionChanged(s);
092: fCloseAction.selectionChanged(s);
093: fCloseUnrelatedAction.selectionChanged(s);
094: }
095:
096: fSelectionChangedListener = new ISelectionChangedListener() {
097: public void selectionChanged(SelectionChangedEvent event) {
098: ISelection s = event.getSelection();
099: if (s instanceof IStructuredSelection) {
100: performSelectionChanged((IStructuredSelection) s);
101: }
102: }
103: };
104: provider.addSelectionChangedListener(fSelectionChangedListener);
105:
106: IWorkspace workspace = ResourcesPlugin.getWorkspace();
107: workspace.addResourceChangeListener(fOpenAction);
108: workspace.addResourceChangeListener(fCloseAction);
109: workspace.addResourceChangeListener(fCloseUnrelatedAction);
110: }
111:
112: protected void performSelectionChanged(
113: IStructuredSelection structuredSelection) {
114: Object[] array = structuredSelection.toArray();
115: ArrayList openProjects = new ArrayList();
116: int selectionStatus = evaluateSelection(array, openProjects);
117: StructuredSelection sel = new StructuredSelection(openProjects);
118:
119: fOpenAction
120: .setEnabled(selectionStatus == CLOSED_PROJECTS_SELECTED
121: || (selectionStatus == 0 && hasClosedProjectsInWorkspace()));
122: fCloseAction.selectionChanged(sel);
123: fCloseUnrelatedAction.selectionChanged(sel);
124: }
125:
126: private int CLOSED_PROJECTS_SELECTED = 1;
127: private int NON_PROJECT_SELECTED = 2;
128:
129: private int evaluateSelection(Object[] array, List allOpenProjects) {
130: int status = 0;
131: for (int i = 0; i < array.length; i++) {
132: Object curr = array[i];
133: if (curr instanceof IJavaProject) {
134: curr = ((IJavaProject) curr).getProject();
135: }
136: if (curr instanceof IProject) {
137: IProject project = (IProject) curr;
138: if (project.isOpen()) {
139: allOpenProjects.add(project);
140: } else {
141: status |= CLOSED_PROJECTS_SELECTED;
142: }
143: } else {
144: if (curr instanceof IWorkingSet) {
145: int res = evaluateSelection(((IWorkingSet) curr)
146: .getElements(), allOpenProjects);
147: status |= res;
148: } else {
149: status |= NON_PROJECT_SELECTED;
150: }
151: }
152: }
153: return status;
154: }
155:
156: private boolean hasClosedProjectsInWorkspace() {
157: IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
158: .getProjects();
159: for (int i = 0; i < projects.length; i++) {
160: if (!projects[i].isOpen())
161: return true;
162: }
163: return false;
164: }
165:
166: /* (non-Javadoc)
167: * Method declared in ActionGroup
168: */
169: public void fillActionBars(IActionBars actionBars) {
170: super .fillActionBars(actionBars);
171: actionBars.setGlobalActionHandler(
172: IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction);
173: actionBars.setGlobalActionHandler(
174: IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(),
175: fCloseUnrelatedAction);
176: actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT
177: .getId(), fOpenAction);
178: }
179:
180: /* (non-Javadoc)
181: * Method declared in ActionGroup
182: */
183: public void fillContextMenu(IMenuManager menu) {
184: super .fillContextMenu(menu);
185: if (fOpenAction.isEnabled())
186: menu.appendToGroup(IContextMenuConstants.GROUP_BUILD,
187: fOpenAction);
188: if (fCloseAction.isEnabled())
189: menu.appendToGroup(IContextMenuConstants.GROUP_BUILD,
190: fCloseAction);
191: if (fCloseUnrelatedAction.isEnabled()
192: && areOnlyProjectsSelected(fCloseUnrelatedAction
193: .getStructuredSelection()))
194: menu.appendToGroup(IContextMenuConstants.GROUP_BUILD,
195: fCloseUnrelatedAction);
196: }
197:
198: /**
199: * Returns the open project action contained in this project action group.
200: *
201: * @return returns the open project action
202: *
203: * @since 3.3
204: */
205: public OpenProjectAction getOpenProjectAction() {
206: return fOpenAction;
207: }
208:
209: private boolean areOnlyProjectsSelected(
210: IStructuredSelection selection) {
211: if (selection.isEmpty())
212: return false;
213:
214: Iterator iter = selection.iterator();
215: while (iter.hasNext()) {
216: Object obj = iter.next();
217: if (obj instanceof IAdaptable) {
218: if (((IAdaptable) obj).getAdapter(IProject.class) == null)
219: return false;
220: }
221: }
222: return true;
223: }
224:
225: /*
226: * @see ActionGroup#dispose()
227: */
228: public void dispose() {
229: ISelectionProvider provider = fSite.getSelectionProvider();
230: provider
231: .removeSelectionChangedListener(fSelectionChangedListener);
232:
233: IWorkspace workspace = ResourcesPlugin.getWorkspace();
234: workspace.removeResourceChangeListener(fOpenAction);
235: workspace.removeResourceChangeListener(fCloseAction);
236: workspace.removeResourceChangeListener(fCloseUnrelatedAction);
237: super.dispose();
238: }
239: }
|