1 14 package org.eclipse.ui.actions; 15 16 import java.util.List ; 17 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.core.runtime.jobs.Job; 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.window.Window; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.events.MouseAdapter; 34 import org.eclipse.swt.events.MouseEvent; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.events.SelectionListener; 38 import org.eclipse.swt.graphics.FontMetrics; 39 import org.eclipse.swt.graphics.GC; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.layout.GridLayout; 42 import org.eclipse.swt.widgets.Button; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Label; 46 import org.eclipse.swt.widgets.Shell; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.ide.undo.DeleteResourcesOperation; 49 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil; 50 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 51 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 52 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 53 import org.eclipse.ui.progress.WorkbenchJob; 54 55 61 public class DeleteResourceAction extends SelectionListenerAction { 62 63 static class DeleteProjectDialog extends MessageDialog { 64 65 private IResource[] projects; 66 67 private boolean deleteContent = false; 68 69 73 private boolean fIsTesting = false; 74 75 private Button radio1; 76 77 private Button radio2; 78 79 DeleteProjectDialog(Shell parentShell, IResource[] projects) { 80 super(parentShell, getTitle(projects), null, getMessage(projects), MessageDialog.QUESTION, new String [] { 84 IDialogConstants.YES_LABEL, 85 IDialogConstants.NO_LABEL }, 0); this.projects = projects; 88 } 89 90 static String getTitle(IResource[] projects) { 91 if (projects.length == 1) { 92 return IDEWorkbenchMessages.DeleteResourceAction_titleProject1; 93 } 94 return IDEWorkbenchMessages.DeleteResourceAction_titleProjectN; 95 } 96 97 static String getMessage(IResource[] projects) { 98 if (projects.length == 1) { 99 IProject project = (IProject) projects[0]; 100 return NLS 101 .bind( 102 IDEWorkbenchMessages.DeleteResourceAction_confirmProject1, 103 project.getName()); 104 } 105 return NLS.bind( 106 IDEWorkbenchMessages.DeleteResourceAction_confirmProjectN, 107 new Integer (projects.length)); 108 } 109 110 113 protected void configureShell(Shell newShell) { 114 super.configureShell(newShell); 115 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, 116 IIDEHelpContextIds.DELETE_PROJECT_DIALOG); 117 } 118 119 protected Control createCustomArea(Composite parent) { 120 Composite composite = new Composite(parent, SWT.NONE); 121 composite.setLayout(new GridLayout()); 122 radio1 = new Button(composite, SWT.RADIO); 123 radio1.addSelectionListener(selectionListener); 124 String text1; 125 if (projects.length == 1) { 126 IProject project = (IProject) projects[0]; 127 if (project == null || project.getLocation() == null) { 128 text1 = IDEWorkbenchMessages.DeleteResourceAction_deleteContentsN; 129 } else { 130 text1 = NLS 131 .bind( 132 IDEWorkbenchMessages.DeleteResourceAction_deleteContents1, 133 project.getLocation().toOSString()); 134 } 135 } else { 136 text1 = IDEWorkbenchMessages.DeleteResourceAction_deleteContentsN; 137 } 138 radio1.setText(text1); 139 radio1.setFont(parent.getFont()); 140 141 Label detailsLabel = new Label(composite, SWT.LEFT); 145 detailsLabel.setText(IDEWorkbenchMessages.DeleteResourceAction_deleteContentsDetails); 146 detailsLabel.setFont(parent.getFont()); 147 GC gc = new GC(detailsLabel); 149 gc.setFont(detailsLabel.getParent().getFont()); 150 FontMetrics fontMetrics = gc.getFontMetrics(); 151 gc.dispose(); 152 GridData data = new GridData(); 153 data.horizontalIndent = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.INDENT); 154 detailsLabel.setLayoutData(data); 155 detailsLabel.addMouseListener(new MouseAdapter() { 159 public void mouseUp(MouseEvent e) { 160 deleteContent = true; 161 radio1.setSelection(deleteContent); 162 radio2.setSelection(!deleteContent); 163 } 164 }); 165 new Label(composite, SWT.LEFT); 167 168 radio2 = new Button(composite, SWT.RADIO); 169 radio2.addSelectionListener(selectionListener); 170 String text2 = IDEWorkbenchMessages.DeleteResourceAction_doNotDeleteContents; 171 radio2.setText(text2); 172 radio2.setFont(parent.getFont()); 173 174 radio1.setSelection(deleteContent); 176 radio2.setSelection(!deleteContent); 177 178 return composite; 179 } 180 181 private SelectionListener selectionListener = new SelectionAdapter() { 182 public void widgetSelected(SelectionEvent e) { 183 Button button = (Button) e.widget; 184 if (button.getSelection()) { 185 deleteContent = (button == radio1); 186 } 187 } 188 }; 189 190 boolean getDeleteContent() { 191 return deleteContent; 192 } 193 194 199 public int open() { 200 if (fIsTesting) { 202 deleteContent = true; 203 return Window.OK; 204 } 205 return super.open(); 206 } 207 208 215 void setTestingMode(boolean t) { 216 fIsTesting = t; 217 } 218 } 219 220 223 public static final String ID = PlatformUI.PLUGIN_ID 224 + ".DeleteResourceAction"; 226 229 private Shell shell; 230 231 234 private boolean deleteContent = false; 235 236 240 protected boolean fTestingMode = false; 241 242 private String [] modelProviderIds; 243 244 250 public DeleteResourceAction(Shell shell) { 251 super(IDEWorkbenchMessages.DeleteResourceAction_text); 252 setToolTipText(IDEWorkbenchMessages.DeleteResourceAction_toolTip); 253 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 254 IIDEHelpContextIds.DELETE_RESOURCE_ACTION); 255 setId(ID); 256 if (shell == null) { 257 throw new IllegalArgumentException (); 258 } 259 this.shell = shell; 260 } 261 262 271 private boolean canDelete(IResource[] resources) { 272 if (!(containsOnlyProjects(resources) || containsOnlyNonProjects(resources))) { 275 return false; 276 } 277 278 if (resources.length == 0) { 279 return false; 280 } 281 for (int i = 0; i < resources.length; i++) { 283 IResource resource = resources[i]; 284 if (resource.isPhantom()) { 285 return false; 286 } 287 } 288 return true; 289 } 290 291 299 private boolean containsLinkedResource(IResource[] resources) { 300 for (int i = 0; i < resources.length; i++) { 301 IResource resource = resources[i]; 302 if (resource.isLinked()) { 303 return true; 304 } 305 } 306 return false; 307 } 308 309 317 private boolean containsOnlyNonProjects(IResource[] resources) { 318 int types = getSelectedResourceTypes(resources); 319 if (types == 0) { 321 return false; 322 } 323 return (types & IResource.PROJECT) == 0; 325 } 326 327 335 private boolean containsOnlyProjects(IResource[] resources) { 336 int types = getSelectedResourceTypes(resources); 337 return types == IResource.PROJECT; 339 } 340 341 349 private boolean confirmDelete(IResource[] resources) { 350 if (containsOnlyProjects(resources)) { 351 return confirmDeleteProjects(resources); 352 } 353 return confirmDeleteNonProjects(resources); 354 355 } 356 357 366 private boolean confirmDeleteNonProjects(IResource[] resources) { 367 String title; 368 String msg; 369 if (resources.length == 1) { 370 title = IDEWorkbenchMessages.DeleteResourceAction_title1; 371 IResource resource = resources[0]; 372 if (resource.isLinked()) { 373 msg = NLS 374 .bind( 375 IDEWorkbenchMessages.DeleteResourceAction_confirmLinkedResource1, 376 resource.getName()); 377 } else { 378 msg = NLS.bind( 379 IDEWorkbenchMessages.DeleteResourceAction_confirm1, 380 resource.getName()); 381 } 382 } else { 383 title = IDEWorkbenchMessages.DeleteResourceAction_titleN; 384 if (containsLinkedResource(resources)) { 385 msg = NLS 386 .bind( 387 IDEWorkbenchMessages.DeleteResourceAction_confirmLinkedResourceN, 388 new Integer (resources.length)); 389 } else { 390 msg = NLS.bind( 391 IDEWorkbenchMessages.DeleteResourceAction_confirmN, 392 new Integer (resources.length)); 393 } 394 } 395 return MessageDialog.openQuestion(shell, title, msg); 396 } 397 398 407 private boolean confirmDeleteProjects(IResource[] resources) { 408 DeleteProjectDialog dialog = new DeleteProjectDialog(shell, resources); 409 dialog.setTestingMode(fTestingMode); 410 int code = dialog.open(); 411 deleteContent = dialog.getDeleteContent(); 412 return code == 0; } 414 415 416 417 418 423 private IResource[] getSelectedResourcesArray() { 424 List selection = getSelectedResources(); 425 IResource[] resources = new IResource[selection.size()]; 426 selection.toArray(resources); 427 return resources; 428 } 429 430 436 private int getSelectedResourceTypes(IResource[] resources) { 437 int types = 0; 438 for (int i = 0; i < resources.length; i++) { 439 types |= resources[i].getType(); 440 } 441 return types; 442 } 443 444 447 public void run() { 448 final IResource[] resources = getSelectedResourcesArray(); 449 if (!confirmDelete(resources)) { 455 return; 456 } 457 458 Job deletionCheckJob = new Job(IDEWorkbenchMessages.DeleteResourceAction_checkJobName) { 459 460 465 protected IStatus run(IProgressMonitor monitor) { 466 if (resources.length == 0) 467 return Status.CANCEL_STATUS; 468 scheduleDeleteJob(resources); 469 return Status.OK_STATUS; 470 } 471 472 477 public boolean belongsTo(Object family) { 478 if (IDEWorkbenchMessages.DeleteResourceAction_jobName 479 .equals(family)) { 480 return true; 481 } 482 return super.belongsTo(family); 483 } 484 }; 485 486 deletionCheckJob.schedule(); 487 488 } 489 490 495 private void scheduleDeleteJob(final IResource[] resourcesToDelete) { 496 Job deleteJob = new Job( 499 IDEWorkbenchMessages.DeleteResourceAction_jobName) { 500 public IStatus run(final IProgressMonitor monitor) { 501 try { 502 final DeleteResourcesOperation op = 503 new DeleteResourcesOperation(resourcesToDelete, IDEWorkbenchMessages.DeleteResourceAction_operationLabel, deleteContent); 504 op.setModelProviderIds(getModelProviderIds()); 505 if (deleteContent && containsOnlyProjects(resourcesToDelete)) { 510 WorkbenchJob statusJob = new WorkbenchJob("Status checking"){ 517 public IStatus runInUIThread( 518 IProgressMonitor monitor) { 519 return op.computeExecutionStatus(monitor); 520 } 521 522 }; 523 524 statusJob.setSystem(true); 525 statusJob.schedule(); 526 try { statusJob.join(); 528 } catch (InterruptedException e) { 529 } 531 532 if (statusJob.getResult().isOK()) { 533 return op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(shell)); 534 } 535 return statusJob.getResult(); 536 } 537 return PlatformUI.getWorkbench().getOperationSupport() 538 .getOperationHistory().execute(op, monitor, 539 WorkspaceUndoUtil.getUIInfoAdapter(shell)); 540 } catch (ExecutionException e) { 541 if (e.getCause() instanceof CoreException) { 542 return ((CoreException)e.getCause()).getStatus(); 543 } 544 return new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, e.getMessage(),e); 545 } 546 } 547 548 553 public boolean belongsTo(Object family) { 554 if (IDEWorkbenchMessages.DeleteResourceAction_jobName 555 .equals(family)) { 556 return true; 557 } 558 return super.belongsTo(family); 559 } 560 561 }; 562 deleteJob.setUser(true); 563 deleteJob.schedule(); 564 } 565 566 571 protected boolean updateSelection(IStructuredSelection selection) { 572 return super.updateSelection(selection) 573 && canDelete(getSelectedResourcesArray()); 574 } 575 576 584 public String [] getModelProviderIds() { 585 return modelProviderIds; 586 } 587 588 598 public void setModelProviderIds(String [] modelProviderIds) { 599 this.modelProviderIds = modelProviderIds; 600 } 601 } 602 | Popular Tags |