PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/ui/actions/OpenProjectAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 240 lines | 167 code | 27 blank | 46 comment | 34 complexity | becc8cb8f4d3edc45bebffb91580969d MD5 | raw file
  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. import java.lang.reflect.InvocationTargetException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import org.eclipse.core.runtime.CoreException;
  16. import org.eclipse.core.runtime.IAdaptable;
  17. import org.eclipse.core.runtime.IProgressMonitor;
  18. import org.eclipse.core.runtime.IStatus;
  19. import org.eclipse.core.runtime.MultiStatus;
  20. import org.eclipse.core.runtime.SubProgressMonitor;
  21. import org.eclipse.core.resources.IProject;
  22. import org.eclipse.core.resources.IResourceChangeEvent;
  23. import org.eclipse.core.resources.IResourceChangeListener;
  24. import org.eclipse.core.resources.IResourceDelta;
  25. import org.eclipse.core.resources.IWorkspaceRunnable;
  26. import org.eclipse.core.resources.ResourcesPlugin;
  27. import org.eclipse.jface.viewers.ArrayContentProvider;
  28. import org.eclipse.jface.viewers.ISelection;
  29. import org.eclipse.jface.viewers.IStructuredSelection;
  30. import org.eclipse.jface.viewers.StructuredSelection;
  31. import org.eclipse.jface.window.Window;
  32. import org.eclipse.ui.IWorkbenchSite;
  33. import org.eclipse.ui.IWorkingSet;
  34. import org.eclipse.ui.PlatformUI;
  35. import org.eclipse.ui.actions.OpenResourceAction;
  36. import org.eclipse.ui.dialogs.ListSelectionDialog;
  37. import org.eclipse.jdt.core.IJavaProject;
  38. import org.eclipse.jdt.ui.JavaElementLabelProvider;
  39. import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
  40. import org.eclipse.jdt.internal.ui.JavaPlugin;
  41. import org.eclipse.jdt.internal.ui.actions.ActionMessages;
  42. import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
  43. import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
  44. /**
  45. * Action to open a closed project. Action either opens the closed projects
  46. * provided by the structured selection or presents a dialog from which the
  47. * user can select the projects to be opened.
  48. *
  49. * <p>
  50. * This class may be instantiated; it is not intended to be subclassed.
  51. * </p>
  52. *
  53. * @since 2.0
  54. *
  55. * @noextend This class is not intended to be subclassed by clients.
  56. */
  57. public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener {
  58. private int CLOSED_PROJECTS_SELECTED= 1;
  59. private int OTHER_ELEMENTS_SELECTED= 2;
  60. private OpenResourceAction fWorkbenchAction;
  61. /**
  62. * Creates a new <code>OpenProjectAction</code>. The action requires
  63. * that the selection provided by the site's selection provider is of type <code>
  64. * org.eclipse.jface.viewers.IStructuredSelection</code>.
  65. *
  66. * @param site the site providing context information for this action
  67. */
  68. public OpenProjectAction(IWorkbenchSite site) {
  69. super(site);
  70. fWorkbenchAction= new OpenResourceAction(site);
  71. setText(fWorkbenchAction.getText());
  72. setToolTipText(fWorkbenchAction.getToolTipText());
  73. PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_PROJECT_ACTION);
  74. setEnabled(hasClosedProjectsInWorkspace());
  75. }
  76. /*
  77. * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
  78. */
  79. public void resourceChanged(IResourceChangeEvent event) {
  80. IResourceDelta delta = event.getDelta();
  81. if (delta != null) {
  82. IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
  83. for (int i = 0; i < projDeltas.length; ++i) {
  84. IResourceDelta projDelta = projDeltas[i];
  85. if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
  86. setEnabled(hasClosedProjectsInWorkspace());
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. //---- normal selection -------------------------------------
  93. /* (non-Javadoc)
  94. * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
  95. */
  96. @Override
  97. public void selectionChanged(ISelection selection) {
  98. }
  99. /* (non-Javadoc)
  100. * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
  101. */
  102. @Override
  103. public void run(ISelection selection) {
  104. internalRun(null);
  105. }
  106. private int evaluateSelection(IStructuredSelection selection, List<Object> allClosedProjects) {
  107. Object[] array= selection.toArray();
  108. int selectionStatus = 0;
  109. for (int i= 0; i < array.length; i++) {
  110. Object curr= array[i];
  111. if (isClosedProject(curr)) {
  112. if (allClosedProjects != null)
  113. allClosedProjects.add(curr);
  114. selectionStatus |= CLOSED_PROJECTS_SELECTED;
  115. } else {
  116. if (curr instanceof IWorkingSet) {
  117. IAdaptable[] elements= ((IWorkingSet) curr).getElements();
  118. for (int k= 0; k < elements.length; k++) {
  119. Object elem= elements[k];
  120. if (isClosedProject(elem)) {
  121. if (allClosedProjects != null)
  122. allClosedProjects.add(elem);
  123. selectionStatus |= CLOSED_PROJECTS_SELECTED;
  124. }
  125. }
  126. }
  127. selectionStatus |= OTHER_ELEMENTS_SELECTED;
  128. }
  129. }
  130. return selectionStatus;
  131. }
  132. private static boolean isClosedProject(Object element) {
  133. if (element instanceof IJavaProject) {
  134. IProject project= ((IJavaProject) element).getProject();
  135. return !project.isOpen();
  136. }
  137. // assume all closed project are rendered as IProject
  138. return element instanceof IProject && !((IProject) element).isOpen();
  139. }
  140. //---- structured selection ---------------------------------------
  141. /* (non-Javadoc)
  142. * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
  143. */
  144. @Override
  145. public void run(IStructuredSelection selection) {
  146. List<Object> allClosedProjects= new ArrayList<Object>();
  147. int selectionStatus= evaluateSelection(selection, allClosedProjects);
  148. if ((selectionStatus & CLOSED_PROJECTS_SELECTED) != 0) { // selection contains closed projects
  149. fWorkbenchAction.selectionChanged(new StructuredSelection(allClosedProjects));
  150. fWorkbenchAction.run();
  151. } else {
  152. internalRun(allClosedProjects);
  153. }
  154. }
  155. private void internalRun(List<?> initialSelection) {
  156. ListSelectionDialog dialog= new ListSelectionDialog(getShell(), getClosedProjectsInWorkspace(), new ArrayContentProvider(), new JavaElementLabelProvider(), ActionMessages.OpenProjectAction_dialog_message);
  157. dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title);
  158. if (initialSelection != null && !initialSelection.isEmpty()) {
  159. dialog.setInitialElementSelections(initialSelection);
  160. }
  161. int result= dialog.open();
  162. if (result != Window.OK)
  163. return;
  164. final Object[] projects= dialog.getResult();
  165. IWorkspaceRunnable runnable= createRunnable(projects);
  166. try {
  167. PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
  168. } catch (InvocationTargetException e) {
  169. ExceptionHandler.handle(e, getShell(), ActionMessages.OpenProjectAction_dialog_title, ActionMessages.OpenProjectAction_error_message);
  170. } catch (InterruptedException e) {
  171. // user cancelled
  172. }
  173. }
  174. private IWorkspaceRunnable createRunnable(final Object[] projects) {
  175. return new IWorkspaceRunnable() {
  176. public void run(IProgressMonitor monitor) throws CoreException {
  177. monitor.beginTask("", projects.length); //$NON-NLS-1$
  178. MultiStatus errorStatus= null;
  179. for (int i = 0; i < projects.length; i++) {
  180. IProject project= (IProject)projects[i];
  181. try {
  182. project.open(new SubProgressMonitor(monitor, 1));
  183. } catch (CoreException e) {
  184. if (errorStatus == null)
  185. errorStatus = new MultiStatus(JavaPlugin.getPluginId(), IStatus.ERROR, ActionMessages.OpenProjectAction_error_message, null);
  186. errorStatus.add(e.getStatus());
  187. }
  188. }
  189. monitor.done();
  190. if (errorStatus != null)
  191. throw new CoreException(errorStatus);
  192. }
  193. };
  194. }
  195. private Object[] getClosedProjectsInWorkspace() {
  196. IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
  197. List<IProject> result= new ArrayList<IProject>(5);
  198. for (int i = 0; i < projects.length; i++) {
  199. IProject project= projects[i];
  200. if (!project.isOpen())
  201. result.add(project);
  202. }
  203. return result.toArray();
  204. }
  205. private boolean hasClosedProjectsInWorkspace() {
  206. IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
  207. for (int i = 0; i < projects.length; i++) {
  208. if (!projects[i].isOpen())
  209. return true;
  210. }
  211. return false;
  212. }
  213. }