1 11 package org.eclipse.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IResourceChangeEvent; 22 import org.eclipse.core.resources.IResourceChangeListener; 23 import org.eclipse.core.resources.IResourceDelta; 24 import org.eclipse.core.resources.IResourceRuleFactory; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 27 import org.eclipse.core.resources.mapping.ResourceChangeValidator; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.NullProgressMonitor; 31 import org.eclipse.core.runtime.jobs.ISchedulingRule; 32 import org.eclipse.core.runtime.jobs.MultiRule; 33 import org.eclipse.jface.dialogs.IDialogConstants; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.osgi.util.NLS; 36 import org.eclipse.swt.custom.BusyIndicator; 37 import org.eclipse.swt.widgets.Shell; 38 import org.eclipse.ui.IEditorPart; 39 import org.eclipse.ui.IWorkbenchPage; 40 import org.eclipse.ui.IWorkbenchWindow; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.dialogs.ListSelectionDialog; 43 import org.eclipse.ui.ide.IDE; 44 import org.eclipse.ui.ide.ResourceUtil; 45 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 46 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 47 import org.eclipse.ui.model.AdaptableList; 48 import org.eclipse.ui.model.WorkbenchContentProvider; 49 import org.eclipse.ui.model.WorkbenchPartLabelProvider; 50 51 57 public class CloseResourceAction extends WorkspaceAction implements 58 IResourceChangeListener { 59 62 public static final String ID = PlatformUI.PLUGIN_ID 63 + ".CloseResourceAction"; private String [] modelProviderIds; 65 66 71 public CloseResourceAction(Shell shell) { 72 super(shell, IDEWorkbenchMessages.CloseResourceAction_text); 73 setId(ID); 74 setToolTipText(IDEWorkbenchMessages.CloseResourceAction_toolTip); 75 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 76 IIDEHelpContextIds.CLOSE_RESOURCE_ACTION); 77 } 78 79 83 protected CloseResourceAction(Shell shell, String text) { 84 super(shell, text); 85 } 86 87 93 List getDirtyEditors(List projects) { 94 List dirtyEditors = new ArrayList (0); 95 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 96 .getWorkbenchWindows(); 97 for (int i = 0; i < windows.length; i++) { 98 IWorkbenchPage[] pages = windows[i].getPages(); 99 for (int j = 0; j < pages.length; j++) { 100 IWorkbenchPage page = pages[j]; 101 IEditorPart[] editors = page.getDirtyEditors(); 102 for (int k = 0; k < editors.length; k++) { 103 IEditorPart editor = editors[k]; 104 IFile inputFile = ResourceUtil.getFile(editor.getEditorInput()); 105 if (inputFile != null) { 106 if (projects.contains(inputFile.getProject())) { 107 dirtyEditors.add(editor); 108 } 109 } 110 } 111 } 112 } 113 return dirtyEditors; 114 } 115 116 123 List getEditorsToSave(List dirtyEditors) { 124 if (dirtyEditors.isEmpty()) { 125 return new ArrayList (0); 126 } 127 128 List saveEditors = new ArrayList (0); 131 List dirtyInputs = new ArrayList (0); 132 Iterator iter = dirtyEditors.iterator(); 133 while (iter.hasNext()) { 134 IEditorPart editor = (IEditorPart) iter.next(); 135 IFile inputFile = ResourceUtil.getFile(editor.getEditorInput()); 136 if (inputFile != null) { 137 if (!dirtyInputs.contains(inputFile)) { 140 dirtyInputs.add(inputFile); 141 saveEditors.add(editor); 142 } 143 } 144 } 145 AdaptableList input = new AdaptableList(saveEditors); 146 ListSelectionDialog dlg = new ListSelectionDialog(getShell(), input, 147 new WorkbenchContentProvider(), 148 new WorkbenchPartLabelProvider(), IDEWorkbenchMessages.EditorManager_saveResourcesMessage); 149 150 dlg.setInitialSelections(saveEditors.toArray(new Object [saveEditors 151 .size()])); 152 dlg.setTitle(IDEWorkbenchMessages.EditorManager_saveResourcesTitle); 153 int result = dlg.open(); 154 155 if (result == IDialogConstants.CANCEL_ID) { 156 return null; 157 } 158 return Arrays.asList(dlg.getResult()); 159 } 160 161 164 protected String getOperationMessage() { 165 return IDEWorkbenchMessages.CloseResourceAction_operationMessage; 166 } 167 168 171 protected String getProblemsMessage() { 172 return IDEWorkbenchMessages.CloseResourceAction_problemMessage; 173 } 174 175 178 protected String getProblemsTitle() { 179 return IDEWorkbenchMessages.CloseResourceAction_title; 180 } 181 182 protected void invokeOperation(IResource resource, IProgressMonitor monitor) 183 throws CoreException { 184 ((IProject) resource).close(monitor); 185 } 186 187 192 public void run() { 193 if (!saveDirtyEditors()) { 194 return; 195 } 196 if (!validateClose()) { 197 return; 198 } 199 ISchedulingRule rule = null; 202 IResourceRuleFactory factory = ResourcesPlugin.getWorkspace().getRuleFactory(); 203 Iterator resources = getSelectedResources().iterator(); 204 while (resources.hasNext()) { 205 IProject project = (IProject) resources.next(); 206 rule = MultiRule.combine(rule, factory.modifyRule(project)); 207 } 208 runInBackground(rule); 209 } 210 211 215 boolean saveDirtyEditors() { 216 List projects = getSelectedResources(); 218 if (projects == null || projects.isEmpty()) { 219 return false; 221 } 222 223 final List dirtyEditors = getDirtyEditors(projects); 226 227 final List saveEditors = getEditorsToSave(dirtyEditors); 229 if (saveEditors == null) { 230 return false; 232 } 233 234 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 236 public void run() { 237 Iterator iter = dirtyEditors.iterator(); 238 while (iter.hasNext()) { 239 IEditorPart editor = (IEditorPart) iter.next(); 240 IWorkbenchPage page = editor.getEditorSite().getPage(); 241 if (saveEditors.contains(editor)) { 242 editor.doSave(new NullProgressMonitor()); 246 } 247 page.closeEditor(editor, false); 248 } 249 } 250 }); 251 252 return true; 253 } 254 255 258 protected boolean shouldPerformResourcePruning() { 259 return false; 260 } 261 262 267 protected boolean updateSelection(IStructuredSelection s) { 268 if (!selectionIsOfType(IResource.PROJECT)) { 270 return false; 271 } 272 273 Iterator resources = getSelectedResources().iterator(); 274 while (resources.hasNext()) { 275 IProject currentResource = (IProject) resources.next(); 276 if (currentResource.isOpen()) { 277 return true; 278 } 279 } 280 return false; 281 } 282 283 287 public void resourceChanged(IResourceChangeEvent event) { 288 List sel = getSelectedResources(); 290 if (selectionIsOfType(IResource.PROJECT)) { 292 IResourceDelta delta = event.getDelta(); 293 if (delta != null) { 294 IResourceDelta[] projDeltas = delta 295 .getAffectedChildren(IResourceDelta.CHANGED); 296 for (int i = 0; i < projDeltas.length; ++i) { 297 IResourceDelta projDelta = projDeltas[i]; 298 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) { 299 if (sel.contains(projDelta.getResource())) { 300 selectionChanged(getStructuredSelection()); 301 return; 302 } 303 } 304 } 305 } 306 } 307 } 308 309 317 public String [] getModelProviderIds() { 318 return modelProviderIds; 319 } 320 321 330 public void setModelProviderIds(String [] modelProviderIds) { 331 this.modelProviderIds = modelProviderIds; 332 } 333 334 339 private boolean validateClose() { 340 IResourceChangeDescriptionFactory factory = ResourceChangeValidator.getValidator().createDeltaFactory(); 341 List resources = getActionResources(); 342 for (Iterator iter = resources.iterator(); iter.hasNext();) { 343 IResource resource = (IResource) iter.next(); 344 if (resource instanceof IProject) { 345 IProject project = (IProject) resource; 346 factory.close(project); 347 } 348 } 349 String message; 350 if (resources.size() == 1) { 351 message = NLS.bind(IDEWorkbenchMessages.CloseResourceAction_warningForOne, ((IResource)resources.get(0)).getName()); 352 } else { 353 message = IDEWorkbenchMessages.CloseResourceAction_warningForMultiple; 354 } 355 return IDE.promptToConfirm(getShell(), IDEWorkbenchMessages.CloseResourceAction_confirm, message, factory.getDelta(), getModelProviderIds(), false ); 356 } 357 } 358 | Popular Tags |