1 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.ui.JavaElementLabelProvider; 44 45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 48 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 50 51 62 public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener { 63 64 private int CLOSED_PROJECTS_SELECTED= 1; 65 private int OTHER_ELEMENTS_SELECTED= 2; 66 67 private OpenResourceAction fWorkbenchAction; 68 69 76 public OpenProjectAction(IWorkbenchSite site) { 77 super(site); 78 fWorkbenchAction= new OpenResourceAction(site.getShell()); 79 setText(fWorkbenchAction.getText()); 80 setToolTipText(fWorkbenchAction.getToolTipText()); 81 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_PROJECT_ACTION); 82 setEnabled(hasClosedProjectsInWorkspace()); 83 } 84 85 88 public void resourceChanged(IResourceChangeEvent event) { 89 IResourceDelta delta = event.getDelta(); 90 if (delta != null) { 91 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED); 92 for (int i = 0; i < projDeltas.length; ++i) { 93 IResourceDelta projDelta = projDeltas[i]; 94 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) { 95 setEnabled(hasClosedProjectsInWorkspace()); 96 return; 97 } 98 } 99 } 100 } 101 102 104 107 public void selectionChanged(ISelection selection) { 108 } 109 110 113 public void run(ISelection selection) { 114 internalRun(null); 115 } 116 117 private int evaluateSelection(IStructuredSelection selection, List allClosedProjects) { 118 Object [] array= selection.toArray(); 119 int selectionStatus = 0; 120 for (int i= 0; i < array.length; i++) { 121 Object curr= array[i]; 122 if (isClosedProject(curr)) { 123 if (allClosedProjects != null) 124 allClosedProjects.add(curr); 125 selectionStatus |= CLOSED_PROJECTS_SELECTED; 126 } else { 127 if (curr instanceof IWorkingSet) { 128 IAdaptable[] elements= ((IWorkingSet) curr).getElements(); 129 for (int k= 0; k < elements.length; k++) { 130 Object elem= elements[k]; 131 if (isClosedProject(elem)) { 132 if (allClosedProjects != null) 133 allClosedProjects.add(elem); 134 selectionStatus |= CLOSED_PROJECTS_SELECTED; 135 } 136 } 137 } 138 selectionStatus |= OTHER_ELEMENTS_SELECTED; 139 } 140 141 } 142 return selectionStatus; 143 } 144 145 private static boolean isClosedProject(Object element) { 146 return element instanceof IProject && !((IProject) element).isOpen(); 148 } 149 150 151 153 156 public void run(IStructuredSelection selection) { 157 ArrayList allClosedProjects= new ArrayList (); 158 int selectionStatus= evaluateSelection(selection, allClosedProjects); 159 if (selectionStatus == CLOSED_PROJECTS_SELECTED) { fWorkbenchAction.selectionChanged(new StructuredSelection(allClosedProjects)); 161 fWorkbenchAction.run(); 162 } else { 163 internalRun(allClosedProjects); 164 } 165 } 166 167 private void internalRun(List initialSelection) { 168 ListSelectionDialog dialog= new ListSelectionDialog(getShell(), getClosedProjectsInWorkspace(), new ArrayContentProvider(), new JavaElementLabelProvider(), ActionMessages.OpenProjectAction_dialog_message); 169 dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title); 170 if (initialSelection != null && !initialSelection.isEmpty()) { 171 dialog.setInitialElementSelections(initialSelection); 172 } 173 int result= dialog.open(); 174 if (result != Window.OK) 175 return; 176 final Object [] projects= dialog.getResult(); 177 IWorkspaceRunnable runnable= createRunnable(projects); 178 try { 179 PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable)); 180 } catch (InvocationTargetException e) { 181 ExceptionHandler.handle(e, getShell(), ActionMessages.OpenProjectAction_dialog_title, ActionMessages.OpenProjectAction_error_message); 182 } catch (InterruptedException e) { 183 } 185 } 186 187 private IWorkspaceRunnable createRunnable(final Object [] projects) { 188 return new IWorkspaceRunnable() { 189 public void run(IProgressMonitor monitor) throws CoreException { 190 monitor.beginTask("", projects.length); MultiStatus errorStatus= null; 192 for (int i = 0; i < projects.length; i++) { 193 IProject project= (IProject)projects[i]; 194 try { 195 project.open(new SubProgressMonitor(monitor, 1)); 196 } catch (CoreException e) { 197 if (errorStatus == null) 198 errorStatus = new MultiStatus(JavaPlugin.getPluginId(), IStatus.ERROR, ActionMessages.OpenProjectAction_error_message, null); 199 errorStatus.add(e.getStatus()); 200 } 201 } 202 monitor.done(); 203 if (errorStatus != null) 204 throw new CoreException(errorStatus); 205 } 206 }; 207 } 208 209 private Object [] getClosedProjectsInWorkspace() { 210 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 211 List result= new ArrayList (5); 212 for (int i = 0; i < projects.length; i++) { 213 IProject project= projects[i]; 214 if (!project.isOpen()) 215 result.add(project); 216 } 217 return result.toArray(); 218 } 219 220 private boolean hasClosedProjectsInWorkspace() { 221 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 222 for (int i = 0; i < projects.length; i++) { 223 if (!projects[i].isOpen()) 224 return true; 225 } 226 return false; 227 } 228 } 229 | Popular Tags |