1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.SubProgressMonitor;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResourceChangeEvent;
26 import org.eclipse.core.resources.IResourceChangeListener;
27 import org.eclipse.core.resources.IResourceDelta;
28 import org.eclipse.core.resources.IWorkspaceRunnable;
29 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.jface.viewers.ArrayContentProvider;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.window.Window;
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.IWorkingSet;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.actions.OpenResourceAction;
41 import org.eclipse.ui.dialogs.ListSelectionDialog;
43 import org.eclipse.jdt.core.IJavaProject;
45 import org.eclipse.jdt.ui.JavaElementLabelProvider;
47 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
50 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
54 * Action to open a closed project. Action either opens the closed projects
55 * provided by the structured selection or presents a dialog from which the
56 * user can select the projects to be opened.
59 * This class may be instantiated; it is not intended to be subclassed.
64 * @noextend This class is not intended to be subclassed by clients.
66 public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener {
68 private int CLOSED_PROJECTS_SELECTED= 1;
69 private int OTHER_ELEMENTS_SELECTED= 2;
71 private OpenResourceAction fWorkbenchAction;
74 * Creates a new <code>OpenProjectAction</code>. The action requires
75 * that the selection provided by the site's selection provider is of type <code>
76 * org.eclipse.jface.viewers.IStructuredSelection</code>.
78 * @param site the site providing context information for this action
80 public OpenProjectAction(IWorkbenchSite site) {
82 fWorkbenchAction= new OpenResourceAction(site);
83 setText(fWorkbenchAction.getText());
84 setToolTipText(fWorkbenchAction.getToolTipText());
85 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_PROJECT_ACTION);
86 setEnabled(hasClosedProjectsInWorkspace());
90 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
92 public void resourceChanged(IResourceChangeEvent event) {
93 IResourceDelta delta = event.getDelta();
95 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
96 for (int i = 0; i < projDeltas.length; ++i) {
97 IResourceDelta projDelta = projDeltas[i];
98 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
99 setEnabled(hasClosedProjectsInWorkspace());
106 //---- normal selection -------------------------------------
109 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
112 public void selectionChanged(ISelection selection) {
116 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
119 public void run(ISelection selection) {
123 private int evaluateSelection(IStructuredSelection selection, List<Object> allClosedProjects) {
124 Object[] array= selection.toArray();
125 int selectionStatus = 0;
126 for (int i= 0; i < array.length; i++) {
127 Object curr= array[i];
128 if (isClosedProject(curr)) {
129 if (allClosedProjects != null)
130 allClosedProjects.add(curr);
131 selectionStatus |= CLOSED_PROJECTS_SELECTED;
133 if (curr instanceof IWorkingSet) {
134 IAdaptable[] elements= ((IWorkingSet) curr).getElements();
135 for (int k= 0; k < elements.length; k++) {
136 Object elem= elements[k];
137 if (isClosedProject(elem)) {
138 if (allClosedProjects != null)
139 allClosedProjects.add(elem);
140 selectionStatus |= CLOSED_PROJECTS_SELECTED;
144 selectionStatus |= OTHER_ELEMENTS_SELECTED;
148 return selectionStatus;
151 private static boolean isClosedProject(Object element) {
152 if (element instanceof IJavaProject) {
153 IProject project= ((IJavaProject) element).getProject();
154 return !project.isOpen();
157 // assume all closed project are rendered as IProject
158 return element instanceof IProject && !((IProject) element).isOpen();
162 //---- structured selection ---------------------------------------
165 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
168 public void run(IStructuredSelection selection) {
169 List<Object> allClosedProjects= new ArrayList<Object>();
170 int selectionStatus= evaluateSelection(selection, allClosedProjects);
171 if ((selectionStatus & CLOSED_PROJECTS_SELECTED) != 0) { // selection contains closed projects
172 fWorkbenchAction.selectionChanged(new StructuredSelection(allClosedProjects));
173 fWorkbenchAction.run();
175 internalRun(allClosedProjects);
179 private void internalRun(List<?> initialSelection) {
180 ListSelectionDialog dialog= new ListSelectionDialog(getShell(), getClosedProjectsInWorkspace(), new ArrayContentProvider(), new JavaElementLabelProvider(), ActionMessages.OpenProjectAction_dialog_message);
181 dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title);
182 if (initialSelection != null && !initialSelection.isEmpty()) {
183 dialog.setInitialElementSelections(initialSelection);
185 int result= dialog.open();
186 if (result != Window.OK)
188 final Object[] projects= dialog.getResult();
189 IWorkspaceRunnable runnable= createRunnable(projects);
191 PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
192 } catch (InvocationTargetException e) {
193 ExceptionHandler.handle(e, getShell(), ActionMessages.OpenProjectAction_dialog_title, ActionMessages.OpenProjectAction_error_message);
194 } catch (InterruptedException e) {
199 private IWorkspaceRunnable createRunnable(final Object[] projects) {
200 return new IWorkspaceRunnable() {
201 public void run(IProgressMonitor monitor) throws CoreException {
202 monitor.beginTask("", projects.length); //$NON-NLS-1$
203 MultiStatus errorStatus= null;
204 for (int i = 0; i < projects.length; i++) {
205 IProject project= (IProject)projects[i];
207 project.open(new SubProgressMonitor(monitor, 1));
208 } catch (CoreException e) {
209 if (errorStatus == null)
210 errorStatus = new MultiStatus(JavaPlugin.getPluginId(), IStatus.ERROR, ActionMessages.OpenProjectAction_error_message, null);
211 errorStatus.add(e.getStatus());
215 if (errorStatus != null)
216 throw new CoreException(errorStatus);
221 private Object[] getClosedProjectsInWorkspace() {
222 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
223 List<IProject> result= new ArrayList<IProject>(5);
224 for (int i = 0; i < projects.length; i++) {
225 IProject project= projects[i];
226 if (!project.isOpen())
229 return result.toArray();
232 private boolean hasClosedProjectsInWorkspace() {
233 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
234 for (int i = 0; i < projects.length; i++) {
235 if (!projects[i].isOpen())