1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Hashtable ; 17 18 import org.eclipse.core.resources.IMarker; 19 import org.eclipse.core.runtime.SubProgressMonitor; 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.TitleAreaDialog; 23 import org.eclipse.jface.resource.JFaceResources; 24 import org.eclipse.jface.viewers.CheckStateChangedEvent; 25 import org.eclipse.jface.viewers.CheckboxTableViewer; 26 import org.eclipse.jface.viewers.ColumnWeightData; 27 import org.eclipse.jface.viewers.ICheckStateListener; 28 import org.eclipse.jface.viewers.ILabelProviderListener; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.ISelectionChangedListener; 31 import org.eclipse.jface.viewers.IStructuredContentProvider; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.viewers.ITableLabelProvider; 34 import org.eclipse.jface.viewers.LabelProvider; 35 import org.eclipse.jface.viewers.ListViewer; 36 import org.eclipse.jface.viewers.SelectionChangedEvent; 37 import org.eclipse.jface.viewers.TableLayout; 38 import org.eclipse.jface.viewers.Viewer; 39 import org.eclipse.jface.viewers.ViewerComparator; 40 import org.eclipse.jface.wizard.ProgressMonitorPart; 41 import org.eclipse.osgi.util.NLS; 42 import org.eclipse.swt.SWT; 43 import org.eclipse.swt.custom.BusyIndicator; 44 import org.eclipse.swt.events.SelectionAdapter; 45 import org.eclipse.swt.events.SelectionEvent; 46 import org.eclipse.swt.graphics.Image; 47 import org.eclipse.swt.layout.FormAttachment; 48 import org.eclipse.swt.layout.FormData; 49 import org.eclipse.swt.layout.FormLayout; 50 import org.eclipse.swt.layout.GridData; 51 import org.eclipse.swt.layout.GridLayout; 52 import org.eclipse.swt.widgets.Button; 53 import org.eclipse.swt.widgets.Composite; 54 import org.eclipse.swt.widgets.Control; 55 import org.eclipse.swt.widgets.Label; 56 import org.eclipse.swt.widgets.Shell; 57 import org.eclipse.swt.widgets.Table; 58 import org.eclipse.swt.widgets.TableColumn; 59 import org.eclipse.ui.IMarkerResolution; 60 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; 61 import org.eclipse.ui.views.markers.WorkbenchMarkerResolution; 62 63 69 public class MarkerResolutionDialog extends TitleAreaDialog { 70 71 private IMarker originalMarker; 72 73 private IMarkerResolution[] resolutions; 74 75 private CheckboxTableViewer markersTable; 76 77 private ListViewer resolutionsList; 78 79 private ProgressMonitorPart progressPart; 80 81 private MarkerView markerView; 82 83 private ViewerComparator resolutionsComparator; 84 85 private boolean calculatingResolutions; 86 87 private boolean progressCancelled = false; 88 89 private Button addMatching; 90 91 private Hashtable markerMap = new Hashtable (0); 92 93 103 public MarkerResolutionDialog(Shell shell, IMarker marker, 104 IMarkerResolution[] newResolutions, MarkerView view) { 105 super(shell); 106 setShellStyle(getShellStyle() | SWT.RESIZE); 107 initializeResolutionsSorter(); 108 resolutionsComparator.sort(view.getViewer(), newResolutions); 109 resolutions = newResolutions; 110 originalMarker = marker; 111 markerView = view; 112 } 113 114 119 protected void configureShell(Shell newShell) { 120 super.configureShell(newShell); 121 newShell.setText(MarkerMessages.resolveMarkerAction_dialogTitle); 122 } 123 124 129 protected Control createDialogArea(Composite parent) { 130 131 initializeDialogUnits(parent); 132 133 setTitleImage(JFaceResources 134 .getResources() 135 .createImageWithDefault( 136 IDEInternalWorkbenchImages 137 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_DLGBAN_QUICKFIX_DLG))); 138 139 Composite mainArea = (Composite) super.createDialogArea(parent); 140 141 Composite control = new Composite(mainArea, SWT.NONE); 144 control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 145 146 FormLayout layout = new FormLayout(); 147 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 148 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 149 layout.spacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 150 control.setLayout(layout); 151 152 Label resolutionsLabel = new Label(control, SWT.NONE); 153 resolutionsLabel 154 .setText(MarkerMessages.MarkerResolutionDialog_Resolutions_List_Title); 155 156 resolutionsLabel.setLayoutData(new FormData()); 157 158 resolutionsList = new ListViewer(control, SWT.BORDER | SWT.SINGLE 159 | SWT.V_SCROLL); 160 resolutionsList.setContentProvider(new IStructuredContentProvider() { 161 public Object [] getElements(Object inputElement) { 162 return resolutions; 163 } 164 165 170 public void dispose() { 171 172 } 173 174 180 public void inputChanged(Viewer viewer, Object oldInput, 181 Object newInput) { 182 183 } 184 }); 185 186 resolutionsList.setLabelProvider(new LabelProvider() { 187 public String getText(Object element) { 188 return ((IMarkerResolution) element).getLabel(); 189 } 190 }); 191 192 resolutionsList 193 .addSelectionChangedListener(new ISelectionChangedListener() { 194 199 public void selectionChanged(SelectionChangedEvent event) { 200 201 WorkbenchMarkerResolution resolution = getSelectedWorkbenchResolution(); 202 if (resolution == null 203 || markerMap.containsKey(resolution)) 204 addMatching.setEnabled(false); 205 else 206 addMatching.setEnabled(true); 207 markersTable.refresh(); 208 } 209 }); 210 211 resolutionsList.setInput(this); 212 213 resolutionsList.setComparator(resolutionsComparator); 214 215 FormData listData = new FormData(); 216 listData.top = new FormAttachment(resolutionsLabel, 0); 217 listData.left = new FormAttachment(0); 218 listData.right = new FormAttachment(100, 0); 219 listData.height = convertHeightInCharsToPixels(10); 220 resolutionsList.getControl().setLayoutData(listData); 221 222 Label title = new Label(control, SWT.NONE); 223 title 224 .setText(MarkerMessages.MarkerResolutionDialog_Problems_List_Title); 225 FormData labelData = new FormData(); 226 labelData.top = new FormAttachment(resolutionsList.getControl(), 0); 227 labelData.left = new FormAttachment(0); 228 title.setLayoutData(labelData); 229 230 Composite buttons = createTableButtons(control); 231 FormData buttonData = new FormData(); 232 buttonData.top = new FormAttachment(title, 0); 233 buttonData.right = new FormAttachment(100); 234 buttonData.height = convertHeightInCharsToPixels(10); 235 buttons.setLayoutData(buttonData); 236 237 createMarkerTable(control); 238 239 FormData tableData = new FormData(); 240 tableData.top = new FormAttachment(buttons, 0, SWT.TOP); 241 tableData.left = new FormAttachment(0); 242 tableData.right = new FormAttachment(buttons, 0); 243 tableData.height = convertHeightInCharsToPixels(10); 244 markersTable.getControl().setLayoutData(tableData); 245 246 progressPart = new ProgressMonitorPart(control, new GridLayout()); 247 248 FormData progressData = new FormData(); 249 progressData.top = new FormAttachment(markersTable.getControl(), 0); 250 progressData.left = new FormAttachment(0); 251 progressData.right = new FormAttachment(100, 0); 252 progressPart.setLayoutData(progressData); 253 254 Dialog.applyDialogFont(control); 255 256 String message = NLS.bind( 257 MarkerMessages.MarkerResolutionDialog_Description, Util 258 .getProperty(IMarker.MESSAGE, originalMarker)); 259 if (message.length() > 50) { 260 int insertionIndex = chooseWhitespace(message); 262 if (insertionIndex > 0) { 263 StringBuffer buffer = new StringBuffer (); 264 buffer.append(message.substring(0, insertionIndex)); 265 buffer.append("\n"); buffer.append(message.substring(insertionIndex, message 267 .length())); 268 message = buffer.toString(); 269 } 270 } 271 272 setMessage(message); 273 return mainArea; 274 275 } 276 277 284 private int chooseWhitespace(String message) { 285 286 for (int i = message.length() / 2; i < message.length(); i++) { 287 if (Character.isWhitespace(message.charAt(i))) 288 return i; 289 } 290 return -1; 291 } 292 293 296 private void initializeResolutionsSorter() { 297 resolutionsComparator = new ViewerComparator() { 298 304 public int compare(Viewer viewer, Object e1, Object e2) { 305 return ((IMarkerResolution) e1).getLabel().compareTo( 306 ((IMarkerResolution) e1).getLabel()); 307 } 308 }; 309 } 310 311 317 private Composite createTableButtons(Composite control) { 318 319 Composite buttonComposite = new Composite(control, SWT.NONE); 320 GridLayout layout = new GridLayout(); 321 layout.marginWidth = 0; 322 layout.marginHeight = 0; 323 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 324 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 325 buttonComposite.setLayout(layout); 326 327 Button selectAll = new Button(buttonComposite, SWT.PUSH); 328 selectAll.setText(MarkerMessages.selectAllAction_title); 329 selectAll.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false)); 330 331 selectAll.addSelectionListener(new SelectionAdapter() { 332 337 public void widgetSelected(SelectionEvent arg0) { 338 markersTable.setAllChecked(true); 339 setComplete(!resolutionsList.getSelection().isEmpty()); 340 } 341 }); 342 343 Button deselectAll = new Button(buttonComposite, SWT.PUSH); 344 deselectAll.setText(MarkerMessages.filtersDialog_deselectAll); 345 deselectAll 346 .setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false)); 347 348 deselectAll.addSelectionListener(new SelectionAdapter() { 349 354 public void widgetSelected(SelectionEvent arg0) { 355 markersTable.setAllChecked(false); 356 setComplete(false); 357 } 358 }); 359 360 addMatching = new Button(buttonComposite, SWT.PUSH); 361 addMatching.setText(MarkerMessages.MarkerResolutionDialog_AddOthers); 362 addMatching 363 .setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false)); 364 addMatching.setEnabled(true); 365 addMatching.addSelectionListener(new SelectionAdapter() { 366 371 public void widgetSelected(SelectionEvent arg0) { 372 373 WorkbenchMarkerResolution selected = getSelectedWorkbenchResolution(); 374 if (selected == null) { 375 return; 376 } 377 378 if (addMatchingMarkers(selected)) { 379 addMatching.setEnabled(false); 380 } 381 } 382 }); 383 384 return buttonComposite; 385 } 386 387 393 private WorkbenchMarkerResolution getSelectedWorkbenchResolution() { 394 Object selection = getSelectedResolution(); 395 if (selection == null 396 || !(selection instanceof WorkbenchMarkerResolution)) { 397 return null; 398 } 399 return (WorkbenchMarkerResolution) selection; 400 401 } 402 403 409 private IMarkerResolution getSelectedResolution() { 410 ISelection selection = resolutionsList.getSelection(); 411 if (!(selection instanceof IStructuredSelection)) { 412 return null; 413 } 414 415 Object first = ((IStructuredSelection) selection).getFirstElement(); 416 417 return (IMarkerResolution) first; 418 419 } 420 421 427 protected boolean addMatchingMarkers( 428 final WorkbenchMarkerResolution resolution) { 429 430 calculatingResolutions = true; 431 progressPart.beginTask( 432 MarkerMessages.MarkerResolutionDialog_CalculatingTask, 100); 433 434 progressPart.worked(10); 435 if (progressCancelled()) { 436 calculatingResolutions = false; 437 return false; 438 } 439 440 progressPart.subTask(NLS.bind( 441 MarkerMessages.MarkerResolutionDialog_WorkingSubTask, 442 resolution.getLabel())); 443 444 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 445 450 public void run() { 451 IMarker[] others = resolution.findOtherMarkers(markerView 452 .getCurrentMarkers().getIMarkers()); 453 454 Collection currentMarkers = new ArrayList (); 455 currentMarkers.add(originalMarker); 456 457 for (int i = 0; i < others.length; i++) { 458 currentMarkers.add(others[i]); 459 } 460 461 markerMap.put(resolution, currentMarkers); 462 463 progressPart.worked(90); 464 progressPart.done(); 465 progressCancelled = false; 466 calculatingResolutions = false; 467 markersTable.refresh(); 468 469 } 470 }); 471 472 return true; 473 } 474 475 481 private boolean progressCancelled() { 482 getShell().getDisplay().readAndDispatch(); 483 if (progressCancelled) { 484 progressCancelled = false; 485 calculatingResolutions = false; 486 progressPart.done(); 487 return true; 488 } 489 return false; 490 } 491 492 497 private void createMarkerTable(Composite control) { 498 markersTable = CheckboxTableViewer.newCheckList(control, SWT.BORDER 499 | SWT.V_SCROLL); 500 501 createTableColumns(); 502 503 markersTable.setContentProvider(new IStructuredContentProvider() { 504 509 public void dispose() { 510 511 } 512 513 518 public Object [] getElements(Object inputElement) { 519 IMarkerResolution selected = getSelectedResolution(); 520 if (selected == null) { 521 return new Object [0]; 522 } 523 524 if (markerMap.containsKey(selected)) { 525 return ((Collection ) markerMap.get(selected)).toArray(); 526 } 527 return new IMarker[] { originalMarker }; 528 } 529 530 536 public void inputChanged(Viewer viewer, Object oldInput, 537 Object newInput) { 538 539 } 540 }); 541 542 markersTable.setLabelProvider(new ITableLabelProvider() { 543 544 550 public Image getColumnImage(Object element, int columnIndex) { 551 if (columnIndex == 0) 552 return Util.getImage(((IMarker) element).getAttribute( 553 IMarker.SEVERITY, -1)); 554 return null; 555 } 556 557 563 public String getColumnText(Object element, int columnIndex) { 564 if (columnIndex == 0) 565 return Util.getResourceName((IMarker) element); 566 int line = ((IMarker) element).getAttribute( 567 IMarker.LINE_NUMBER, -1); 568 if (line < 0) { 569 return MarkerMessages.Unknown; 570 } 571 return NLS.bind(MarkerMessages.label_lineNumber, Integer 572 .toString(line)); 573 } 574 575 580 public void addListener(ILabelProviderListener listener) { 581 583 } 584 585 590 public void dispose() { 591 593 } 594 595 601 public boolean isLabelProperty(Object element, String property) { 602 return false; 603 } 604 605 610 public void removeListener(ILabelProviderListener listener) { 611 613 } 614 }); 615 616 markersTable.addCheckStateListener(new ICheckStateListener() { 617 622 public void checkStateChanged(CheckStateChangedEvent event) { 623 if (event.getChecked() == true) { 624 setComplete(true); 625 } else { 626 setComplete(markersTable.getCheckedElements().length > 0); 627 } 628 629 } 630 }); 631 632 markersTable.setInput(this); 633 markersTable.setAllChecked(true); 634 } 635 636 639 private void createTableColumns() { 640 TableLayout layout = new TableLayout(); 641 642 Table table = markersTable.getTable(); 643 table.setLayout(layout); 644 table.setLinesVisible(true); 645 table.setHeaderVisible(true); 646 647 layout.addColumnData(new ColumnWeightData(70, true)); 648 TableColumn tc = new TableColumn(table, SWT.NONE, 0); 649 tc 650 .setText(MarkerMessages.MarkerResolutionDialog_Problems_List_Location); 651 layout.addColumnData(new ColumnWeightData(30, true)); 652 tc = new TableColumn(table, SWT.NONE, 0); 653 tc 654 .setText(MarkerMessages.MarkerResolutionDialog_Problems_List_Resource); 655 656 } 657 658 663 protected void setComplete(boolean complete) { 664 getButton(IDialogConstants.OK_ID).setEnabled(complete); 665 666 } 667 668 673 public IMarkerResolution[] getResolutions() { 674 return resolutions; 675 } 676 677 682 public void create() { 683 super.create(); 684 setTitle(MarkerMessages.MarkerResolutionDialog_Title); 685 resolutionsList.getList().select(0); 686 markersTable.refresh(); 687 markersTable.setAllChecked(true); 688 setComplete(true); 689 } 690 691 696 protected void okPressed() { 697 IMarkerResolution resolution = getSelectedResolution(); 698 if (resolution == null) { 699 return; 700 } 701 702 Object [] checked = markersTable.getCheckedElements(); 703 704 progressPart.beginTask(MarkerMessages.MarkerResolutionDialog_Fixing, 705 checked.length + 1); 706 progressPart.worked(1); 707 708 calculatingResolutions = true; 709 710 if (resolution instanceof WorkbenchMarkerResolution) { 711 712 IMarker[] markers = new IMarker[checked.length]; 713 System.arraycopy(checked, 0, markers, 0, checked.length); 714 ((WorkbenchMarkerResolution) resolution).run(markers, 715 new SubProgressMonitor(progressPart, checked.length)); 716 } else { 717 718 getShell().getDisplay().readAndDispatch(); 720 if (!progressCancelled() && checked.length == 1) { 721 722 IMarker marker = (IMarker) checked[0]; 724 725 progressPart.subTask(Util.getProperty(IMarker.MESSAGE, marker)); 726 resolution.run(marker); 727 progressPart.worked(1); 728 } 729 730 } 731 732 calculatingResolutions = false; 733 progressPart.done(); 734 progressCancelled = false; 735 super.okPressed(); 736 } 737 738 743 protected void cancelPressed() { 744 if (calculatingResolutions) { 745 progressCancelled = true; 746 progressPart.setCanceled(true); 747 return; 748 } 749 super.cancelPressed(); 750 } 751 752 } 753 | Popular Tags |