1 11 package org.eclipse.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.WorkspaceJob; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.MultiStatus; 24 import org.eclipse.core.runtime.OperationCanceledException; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.core.runtime.SubProgressMonitor; 27 import org.eclipse.core.runtime.jobs.ISchedulingRule; 28 import org.eclipse.core.runtime.jobs.Job; 29 import org.eclipse.jface.dialogs.ErrorDialog; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.operation.IRunnableWithProgress; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.osgi.util.NLS; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 37 import org.eclipse.ui.internal.ide.StatusUtil; 38 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; 39 40 69 public abstract class WorkspaceAction extends SelectionListenerAction { 70 73 private final Shell shell; 74 75 84 protected WorkspaceAction(Shell shell, String text) { 85 super(text); 86 if (shell == null) { 87 throw new IllegalArgumentException (); 88 } 89 this.shell = shell; 90 } 91 92 101 void displayError(String message) { 102 if (message == null) { 103 message = IDEWorkbenchMessages.WorkbenchAction_internalError; 104 } 105 MessageDialog.openError(shell, getProblemsTitle(), message); 106 } 107 108 121 final IStatus execute(List resources, IProgressMonitor monitor) { 122 MultiStatus errors = null; 123 if (shouldPerformResourcePruning()) { 125 resources = pruneResources(resources); 126 } 127 monitor.beginTask("", resources.size() * 1000); monitor.setTaskName(getOperationMessage()); 134 Iterator resourcesEnum = resources.iterator(); 135 try { 136 while (resourcesEnum.hasNext()) { 137 IResource resource = (IResource) resourcesEnum.next(); 138 try { 139 invokeOperation(resource, new SubProgressMonitor(monitor, 142 1000)); 143 } catch (CoreException e) { 144 errors = recordError(errors, e); 145 } 146 if (monitor.isCanceled()) { 147 throw new OperationCanceledException(); 148 } 149 } 150 return errors == null ? Status.OK_STATUS : errors; 151 } finally { 152 monitor.done(); 153 } 154 } 155 156 169 protected abstract String getOperationMessage(); 170 171 184 protected String getProblemsMessage() { 185 return IDEWorkbenchMessages.WorkbenchAction_problemsMessage; 186 } 187 188 200 protected String getProblemsTitle() { 201 return IDEWorkbenchMessages.WorkspaceAction_problemsTitle; 202 } 203 204 210 Shell getShell() { 211 return shell; 212 } 213 214 238 protected abstract void invokeOperation(IResource resource, 239 IProgressMonitor monitor) throws CoreException; 240 241 252 boolean isDescendent(List resources, IResource child) { 253 IResource parent = child.getParent(); 254 return parent != null 255 && (resources.contains(parent) || isDescendent(resources, 256 parent)); 257 } 258 259 269 List pruneResources(List resourceCollection) { 270 List prunedList = new ArrayList (resourceCollection); 271 Iterator elementsEnum = prunedList.iterator(); 272 while (elementsEnum.hasNext()) { 273 IResource currentResource = (IResource) elementsEnum.next(); 274 if (isDescendent(prunedList, currentResource)) { 275 elementsEnum.remove(); } 277 } 278 return prunedList; 279 } 280 281 288 MultiStatus recordError(MultiStatus errors, CoreException error) { 289 if (errors == null) { 290 errors = new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, 291 IStatus.ERROR, getProblemsMessage(), null); 292 } 293 errors.merge(error.getStatus()); 294 return errors; 295 } 296 297 308 public void run() { 309 IStatus[] errorStatus = new IStatus[1]; 310 try { 311 new ProgressMonitorJobsDialog(shell).run(true, true, 312 createOperation(errorStatus)); 313 } catch (InterruptedException e) { 314 return; 315 } catch (InvocationTargetException e) { 316 String msg = NLS.bind( 319 IDEWorkbenchMessages.WorkspaceAction_logTitle, getClass() 320 .getName(), e.getTargetException()); 321 IDEWorkbenchPlugin.log(msg, StatusUtil.newStatus(IStatus.ERROR, 322 msg, e.getTargetException())); 323 displayError(e.getTargetException().getMessage()); 324 } 325 if (errorStatus[0] != null && !errorStatus[0].isOK()) { 328 ErrorDialog.openError(shell, getProblemsTitle(), null, errorStatus[0]); 332 } 333 } 334 335 352 protected boolean shouldPerformResourcePruning() { 353 return true; 354 } 355 356 364 protected boolean updateSelection(IStructuredSelection selection) { 365 if (!super.updateSelection(selection) || selection.isEmpty()) { 366 return false; 367 } 368 for (Iterator i = getSelectedResources().iterator(); i.hasNext();) { 369 IResource r = (IResource) i.next(); 370 if (!r.isAccessible()) { 371 return false; 372 } 373 } 374 return true; 375 } 376 377 385 protected List getActionResources() { 386 return getSelectedResources(); 387 } 388 389 396 public void runInBackground(ISchedulingRule rule) { 397 runInBackground(rule, (Object []) null); 398 } 399 400 412 public void runInBackground(ISchedulingRule rule, Object jobFamily) { 413 if (jobFamily == null) { 414 runInBackground(rule, (Object []) null); 415 } else { 416 runInBackground(rule, new Object [] { jobFamily }); 417 } 418 } 419 420 432 public void runInBackground(ISchedulingRule rule, final Object [] jobFamilies) { 433 final List resources = new ArrayList (getActionResources()); 435 Job job = new WorkspaceJob(removeMnemonics(getText())) { 436 437 442 public boolean belongsTo(Object family) { 443 if (jobFamilies == null || family == null) { 444 return false; 445 } 446 for (int i = 0; i < jobFamilies.length; i++) { 447 if (family.equals(jobFamilies[i])) { 448 return true; 449 } 450 } 451 return false; 452 } 453 454 459 public IStatus runInWorkspace(IProgressMonitor monitor) { 460 return WorkspaceAction.this.execute(resources, monitor); 461 } 462 }; 463 if (rule != null) { 464 job.setRule(rule); 465 } 466 job.setUser(true); 467 job.schedule(); 468 } 469 470 488 protected IRunnableWithProgress createOperation(final IStatus[] errorStatus) { 489 return new WorkspaceModifyOperation() { 490 public void execute(IProgressMonitor monitor) { 491 errorStatus[0] = WorkspaceAction.this.execute( 492 getActionResources(), monitor); 493 } 494 }; 495 } 496 497 } 498 | Popular Tags |