]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/OpenProjectAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / OpenProjectAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
23
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;
30
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;
36
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;
42
43 import org.eclipse.jdt.core.IJavaProject;
44
45 import org.eclipse.jdt.ui.JavaElementLabelProvider;
46
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;
52
53 /**
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.
57  *
58  * <p>
59  * This class may be instantiated; it is not intended to be subclassed.
60  * </p>
61  *
62  * @since 2.0
63  *
64  * @noextend This class is not intended to be subclassed by clients.
65  */
66 public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener {
67
68         private int CLOSED_PROJECTS_SELECTED= 1;
69         private int OTHER_ELEMENTS_SELECTED= 2;
70
71         private OpenResourceAction fWorkbenchAction;
72
73         /**
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>.
77          *
78          * @param site the site providing context information for this action
79          */
80         public OpenProjectAction(IWorkbenchSite site) {
81                 super(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());
87         }
88
89         /*
90          * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
91          */
92         public void resourceChanged(IResourceChangeEvent event) {
93                 IResourceDelta delta = event.getDelta();
94                 if (delta != null) {
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());
100                                         return;
101                                 }
102                         }
103                 }
104         }
105
106         //---- normal selection -------------------------------------
107
108         /* (non-Javadoc)
109          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
110          */
111         @Override
112         public void selectionChanged(ISelection selection) {
113         }
114
115         /* (non-Javadoc)
116          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
117          */
118         @Override
119         public void run(ISelection selection) {
120                 internalRun(null);
121         }
122
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;
132                         } else {
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;
141                                                 }
142                                         }
143                                 }
144                                 selectionStatus |= OTHER_ELEMENTS_SELECTED;
145                         }
146
147                 }
148                 return selectionStatus;
149         }
150
151         private static boolean isClosedProject(Object element) {
152                 if (element instanceof IJavaProject) {
153                         IProject project= ((IJavaProject) element).getProject();
154                         return !project.isOpen();
155                 }
156
157                 // assume all closed project are rendered as IProject
158                 return element instanceof IProject && !((IProject) element).isOpen();
159         }
160
161
162         //---- structured selection ---------------------------------------
163
164         /* (non-Javadoc)
165          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
166          */
167         @Override
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();
174                 } else {
175                         internalRun(allClosedProjects);
176                 }
177         }
178
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);
184                 }
185                 int result= dialog.open();
186                 if (result != Window.OK)
187                         return;
188                 final Object[] projects= dialog.getResult();
189                 IWorkspaceRunnable runnable= createRunnable(projects);
190                 try {
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) {
195                         // user cancelled
196                 }
197         }
198
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];
206                                         try {
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());
212                                         }
213                                 }
214                                 monitor.done();
215                                 if (errorStatus != null)
216                                         throw new CoreException(errorStatus);
217                         }
218                 };
219         }
220
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())
227                                 result.add(project);
228                 }
229                 return result.toArray();
230         }
231
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())
236                                 return true;
237                 }
238                 return false;
239         }
240 }