1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import java.text.MessageFormat ; 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.eclipse.core.commands.operations.IUndoContext; 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IMarker; 28 import org.eclipse.core.resources.IResource; 29 import org.eclipse.core.resources.IResourceChangeEvent; 30 import org.eclipse.core.resources.IResourceChangeListener; 31 import org.eclipse.core.resources.ResourcesPlugin; 32 import org.eclipse.core.resources.mapping.ResourceMapping; 33 import org.eclipse.core.resources.mapping.ResourceMappingContext; 34 import org.eclipse.core.resources.mapping.ResourceTraversal; 35 import org.eclipse.core.runtime.CoreException; 36 import org.eclipse.core.runtime.IAdaptable; 37 import org.eclipse.core.runtime.IProgressMonitor; 38 import org.eclipse.core.runtime.IStatus; 39 import org.eclipse.core.runtime.NullProgressMonitor; 40 import org.eclipse.core.runtime.Status; 41 import org.eclipse.core.runtime.jobs.IJobChangeListener; 42 import org.eclipse.core.runtime.jobs.Job; 43 import org.eclipse.help.HelpSystem; 44 import org.eclipse.help.IContext; 45 import org.eclipse.help.IContextProvider; 46 import org.eclipse.jface.action.IMenuManager; 47 import org.eclipse.jface.action.IToolBarManager; 48 import org.eclipse.jface.action.MenuManager; 49 import org.eclipse.jface.action.Separator; 50 import org.eclipse.jface.dialogs.Dialog; 51 import org.eclipse.jface.dialogs.IDialogSettings; 52 import org.eclipse.jface.preference.IPreferenceStore; 53 import org.eclipse.jface.util.IPropertyChangeListener; 54 import org.eclipse.jface.util.PropertyChangeEvent; 55 import org.eclipse.jface.viewers.AbstractTreeViewer; 56 import org.eclipse.jface.viewers.ISelection; 57 import org.eclipse.jface.viewers.IStructuredSelection; 58 import org.eclipse.jface.viewers.OpenEvent; 59 import org.eclipse.jface.viewers.StructuredSelection; 60 import org.eclipse.jface.viewers.TreeViewer; 61 import org.eclipse.jface.window.Window; 62 import org.eclipse.osgi.util.NLS; 63 import org.eclipse.swt.dnd.Clipboard; 64 import org.eclipse.swt.dnd.DND; 65 import org.eclipse.swt.dnd.DragSourceAdapter; 66 import org.eclipse.swt.dnd.DragSourceEvent; 67 import org.eclipse.swt.dnd.DragSourceListener; 68 import org.eclipse.swt.dnd.TextTransfer; 69 import org.eclipse.swt.dnd.Transfer; 70 import org.eclipse.swt.events.HelpEvent; 71 import org.eclipse.swt.events.HelpListener; 72 import org.eclipse.swt.events.KeyEvent; 73 import org.eclipse.swt.events.TreeAdapter; 74 import org.eclipse.swt.events.TreeEvent; 75 import org.eclipse.swt.widgets.Composite; 76 import org.eclipse.swt.widgets.Tree; 77 import org.eclipse.ui.IActionBars; 78 import org.eclipse.ui.IEditorInput; 79 import org.eclipse.ui.IEditorPart; 80 import org.eclipse.ui.IMemento; 81 import org.eclipse.ui.ISelectionListener; 82 import org.eclipse.ui.IViewSite; 83 import org.eclipse.ui.IWorkbenchActionConstants; 84 import org.eclipse.ui.IWorkbenchPart; 85 import org.eclipse.ui.PartInitException; 86 import org.eclipse.ui.PlatformUI; 87 import org.eclipse.ui.WorkbenchException; 88 import org.eclipse.ui.XMLMemento; 89 import org.eclipse.ui.actions.ActionFactory; 90 import org.eclipse.ui.actions.ContributionItemFactory; 91 import org.eclipse.ui.actions.SelectionProviderAction; 92 import org.eclipse.ui.ide.IDE; 93 import org.eclipse.ui.ide.ResourceUtil; 94 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 95 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 96 import org.eclipse.ui.keys.IBindingService; 97 import org.eclipse.ui.operations.RedoActionHandler; 98 import org.eclipse.ui.operations.UndoActionHandler; 99 import org.eclipse.ui.part.IShowInSource; 100 import org.eclipse.ui.part.MarkerTransfer; 101 import org.eclipse.ui.part.ShowInContext; 102 import org.eclipse.ui.preferences.ViewPreferencesAction; 103 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 104 import org.eclipse.ui.progress.WorkbenchJob; 105 import org.eclipse.ui.views.markers.internal.MarkerAdapter.MarkerCategory; 106 import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter; 107 108 112 public abstract class MarkerView extends TableView { 113 114 private static final String TAG_SELECTION = "selection"; 116 private static final String TAG_MARKER = "marker"; 118 private static final String TAG_RESOURCE = "resource"; 120 private static final String TAG_ID = "id"; 122 private static final String TAG_FILTERS_SECTION = "filters"; 124 private static final String TAG_FILTER_ENTRY = "filter"; 126 private static final String MENU_FILTERS_GROUP = "group.filter"; 128 private static final String MENU_SHOW_IN_GROUP = "group.showIn"; 130 private static final String OLD_FILTER_SECTION = "filter"; 133 static final Object MARKER_UPDATE_FAMILY = new Object (); 134 135 class MarkerProcessJob extends Job { 136 137 140 MarkerProcessJob() { 141 super(MarkerMessages.MarkerView_processUpdates); 142 setSystem(true); 143 } 144 145 150 protected IStatus run(IProgressMonitor monitor) { 151 updateForContentsRefresh(monitor); 152 return Status.OK_STATUS; 153 154 } 155 156 161 public boolean shouldRun() { 162 return PlatformUI.isWorkbenchRunning(); 164 } 165 166 171 public boolean belongsTo(Object family) { 172 return MARKER_UPDATE_FAMILY == family; 173 } 174 175 } 176 177 MarkerProcessJob markerProcessJob = new MarkerProcessJob(); 178 179 private class UpdateJob extends WorkbenchJob { 180 181 private class MarkerDescriptor { 182 String description; 183 184 String folder; 185 186 String resource; 187 188 int line; 189 190 MarkerDescriptor(ConcreteMarker marker) { 191 description = marker.getDescription(); 192 folder = marker.getFolder(); 193 resource = marker.getResourceName(); 194 line = marker.getLine(); 195 } 196 197 boolean isEquivalentTo(ConcreteMarker marker) { 198 return marker.getDescription().equals(description) 199 && marker.getFolder().equals(folder) 200 && marker.getResourceName().equals(resource) 201 && marker.getLine() == line; 202 } 203 204 } 205 206 private Collection categoriesToExpand = new HashSet (); 207 208 private Collection preservedSelection = new ArrayList (); 209 210 UpdateJob() { 211 super(MarkerMessages.MarkerView_queueing_updates); 212 setSystem(true); 213 } 214 215 220 public IStatus runInUIThread(IProgressMonitor monitor) { 221 222 if (getViewer().getControl().isDisposed()) { 223 return Status.CANCEL_STATUS; 224 } 225 226 if (monitor.isCanceled()) 227 return Status.CANCEL_STATUS; 228 229 getViewer().refresh(true); 230 231 Tree tree = getTree(); 232 233 if (tree != null && !tree.isDisposed()) { 234 updateStatusMessage(); 235 updateTitle(); 236 if (getCurrentMarkers().getSize() < 20) { 238 getViewer().expandAll(); 239 } else { MarkerCategory[] categories = getMarkerAdapter() 241 .getCategories(); 242 if (categories == null) 243 categoriesToExpand.clear(); 244 else { 245 if (categories.length == 1) { getViewer().expandAll(); 249 categoriesToExpand.clear(); 250 if (monitor.isCanceled()) 251 return Status.CANCEL_STATUS; 252 categoriesToExpand.add(categories[0].getName()); 253 } else { 254 Collection newCategories = new HashSet (); 255 for (int i = 0; i < categories.length; i++) { 256 if (monitor.isCanceled()) 257 return Status.CANCEL_STATUS; 258 MarkerCategory category = categories[i]; 259 if (categoriesToExpand.contains(category 260 .getName())) { 261 getViewer().expandToLevel(category, 262 AbstractTreeViewer.ALL_LEVELS); 263 newCategories.add(category.getName()); 264 } 265 266 } 267 categoriesToExpand = newCategories; 268 } 269 } 270 271 } 272 } 273 274 if (preservedSelection.size() > 0) { 275 276 Collection newSelection = new ArrayList (); 277 ConcreteMarker[] markers = getCurrentMarkers().toArray(); 278 279 for (int i = 0; i < markers.length; i++) { 280 Iterator preserved = preservedSelection.iterator(); 281 while (preserved.hasNext()) { 282 MarkerDescriptor next = (MarkerDescriptor) preserved 283 .next(); 284 if (next.isEquivalentTo(markers[i])) { 285 newSelection.add(markers[i]); 286 continue; 287 } 288 } 289 } 290 291 getViewer().setSelection( 292 new StructuredSelection(newSelection.toArray()), true); 293 preservedSelection.clear(); 294 } 295 if (getViewer().getTree().getItemCount() > 0) 296 getViewer().getTree().setTopItem( 297 getViewer().getTree().getItem(0)); 298 299 return Status.OK_STATUS; 300 } 301 302 307 public void addExpandedCategory(MarkerCategory category) { 308 categoriesToExpand.add(category.getName()); 309 310 } 311 312 317 public void removeExpandedCategory(MarkerCategory category) { 318 categoriesToExpand.remove(category.getName()); 319 320 } 321 322 327 public boolean belongsTo(Object family) { 328 return family == MARKER_UPDATE_FAMILY; 329 } 330 331 336 public void saveSelection(ISelection selection) { 337 preservedSelection.clear(); 338 if (selection instanceof IStructuredSelection) { 339 IStructuredSelection structured = (IStructuredSelection) selection; 340 Iterator iterator = structured.iterator(); 341 while (iterator.hasNext()) { 342 MarkerNode next = (MarkerNode) iterator.next(); 343 if (next.isConcrete()) { 344 preservedSelection.add(new MarkerDescriptor(next 345 .getConcreteRepresentative())); 346 } 347 } 348 } 349 350 } 351 352 357 public boolean shouldRun() { 358 return !getMarkerAdapter().isBuilding(); 359 } 360 361 } 362 363 private UpdateJob updateJob = new UpdateJob(); 364 365 private int preBusyMarkers = 0; 368 369 protected Object [] focusElements; 370 371 private Clipboard clipboard; 372 373 IResourceChangeListener markerUpdateListener = new IResourceChangeListener() { 374 375 380 public void resourceChanged(IResourceChangeEvent event) { 381 if (!hasMarkerDelta(event)) 382 return; 383 384 if (event.getType() == IResourceChangeEvent.POST_BUILD) { 385 scheduleMarkerUpdate(Util.SHORT_DELAY); 386 return; 387 } 388 389 391 IWorkbenchSiteProgressService progressService = getProgressService(); 392 if (progressService == null) 393 markerProcessJob.schedule(Util.LONG_DELAY); 394 else 395 getProgressService() 396 .schedule(markerProcessJob, Util.LONG_DELAY); 397 398 } 399 400 410 private boolean hasMarkerDelta(IResourceChangeEvent event) { 411 String [] markerTypes = getMarkerTypes(); 412 for (int i = 0; i < markerTypes.length; i++) { 413 if (event.findMarkerDeltas(markerTypes[i], true).length > 0) { 414 return true; 415 } 416 } 417 return false; 418 } 419 420 }; 421 422 private class ContextProvider implements IContextProvider { 423 public int getContextChangeMask() { 424 return SELECTION; 425 } 426 427 public IContext getContext(Object target) { 428 String contextId = null; 429 ConcreteMarker marker = getSelectedConcreteMarker(); 431 if (marker != null) { 432 contextId = IDE.getMarkerHelpRegistry().getHelp( 433 marker.getMarker()); 434 } 435 436 if (contextId == null) { 437 contextId = getStaticContextId(); 438 } 439 return HelpSystem.getContext(contextId); 440 } 441 442 448 private ConcreteMarker getSelectedConcreteMarker() { 449 450 IStructuredSelection selection = (IStructuredSelection) getViewer() 451 .getSelection(); 452 if (selection.isEmpty()) 453 return null; 454 455 if (selection.getFirstElement() instanceof ConcreteMarker) 456 return (ConcreteMarker) selection.getFirstElement(); 457 return null; 458 } 459 460 465 public String getSearchExpression(Object target) { 466 return null; 467 } 468 } 469 470 private ContextProvider contextProvider = new ContextProvider(); 471 472 protected ActionCopyMarker copyAction; 473 474 protected ActionPasteMarker pasteAction; 475 476 protected SelectionProviderAction revealAction; 477 478 protected SelectionProviderAction openAction; 479 480 protected SelectionProviderAction deleteAction; 481 482 protected SelectionProviderAction selectAllAction; 483 484 protected SelectionProviderAction propertiesAction; 485 486 protected UndoActionHandler undoAction; 487 488 protected RedoActionHandler redoAction; 489 490 private ISelectionListener focusListener = new ISelectionListener() { 491 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 492 MarkerView.this.focusSelectionChanged(part, selection); 493 } 494 }; 495 496 private int totalMarkers = 0; 497 498 private MarkerFilter[] markerFilters = new MarkerFilter[0]; 499 500 private MarkerFilter[] enabledFilters = null; 502 503 private MenuManager filtersMenu; 504 505 private MenuManager showInMenu; 506 507 private IPropertyChangeListener workingSetListener; 508 509 private MarkerAdapter adapter; 510 511 private IPropertyChangeListener preferenceListener; 512 513 516 public MarkerView() { 517 super(); 518 preferenceListener = new IPropertyChangeListener() { 519 524 public void propertyChange(PropertyChangeEvent event) { 525 if (event.getProperty().equals(getFiltersPreferenceName())) { 526 loadFiltersPreferences(); 527 clearEnabledFilters(); 528 refreshForFocusUpdate(); 529 } 530 } 531 }; 532 IDEWorkbenchPlugin.getDefault().getPreferenceStore() 533 .addPropertyChangeListener(preferenceListener); 534 } 535 536 541 public MarkerList getCurrentMarkers() { 542 return getMarkerAdapter().getCurrentMarkers(); 543 } 544 545 550 protected MarkerAdapter getMarkerAdapter() { 551 return adapter; 552 } 553 554 559 public void updateForContentsRefresh(IProgressMonitor monitor) { 560 updateJob.cancel(); 561 getMarkerAdapter().buildAllMarkers(monitor); 562 getProgressService().schedule(updateJob); 563 } 564 565 571 public void init(IViewSite site, IMemento memento) throws PartInitException { 572 super.init(site, memento); 573 IWorkbenchSiteProgressService progressService = getProgressService(); 574 if (progressService != null) { 575 getProgressService().showBusyForFamily( 576 ResourcesPlugin.FAMILY_MANUAL_BUILD); 577 getProgressService().showBusyForFamily( 578 ResourcesPlugin.FAMILY_AUTO_BUILD); 579 getProgressService().showBusyForFamily(MARKER_UPDATE_FAMILY); 580 } 581 loadFiltersPreferences(); 582 583 } 584 585 588 private void loadFiltersPreferences() { 589 590 String preference = IDEWorkbenchPlugin.getDefault() 591 .getPreferenceStore().getString(getFiltersPreferenceName()); 592 593 if (preference.equals(IPreferenceStore.STRING_DEFAULT_DEFAULT)) { 594 createDefaultFilter(); 595 return; 596 } 597 598 StringReader reader = new StringReader (preference); 599 try { 600 restoreFilters(XMLMemento.createReadRoot(reader)); 601 } catch (WorkbenchException e) { 602 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e); 603 } 604 605 } 606 607 611 void updateForFilterChanges() { 612 613 XMLMemento memento = XMLMemento.createWriteRoot(TAG_FILTERS_SECTION); 614 615 writeFiltersSettings(memento); 616 617 StringWriter writer = new StringWriter (); 618 try { 619 memento.save(writer); 620 } catch (IOException e) { 621 IDEWorkbenchPlugin.getDefault().getLog().log(Util.errorStatus(e)); 622 } 623 624 IDEWorkbenchPlugin.getDefault().getPreferenceStore().putValue( 625 getFiltersPreferenceName(), writer.toString()); 626 IDEWorkbenchPlugin.getDefault().savePluginPreferences(); 627 628 clearEnabledFilters(); 629 refreshFilterMenu(); 630 refreshViewer(); 631 } 632 633 638 protected void writeFiltersSettings(XMLMemento memento) { 639 MarkerFilter[] filters = getUserFilters(); 640 for (int i = 0; i < filters.length; i++) { 641 IMemento child = memento.createChild(TAG_FILTER_ENTRY, filters[i] 642 .getName()); 643 filters[i].saveFilterSettings(child); 644 } 645 } 646 647 652 abstract String getFiltersPreferenceName(); 653 654 659 void restoreFilters(IMemento memento) { 660 661 IMemento[] sections = null; 662 if (memento != null) { 663 sections = memento.getChildren(TAG_FILTER_ENTRY); 664 } 665 666 if (sections == null) { 667 IDialogSettings mainSettings = getDialogSettings(); 669 IDialogSettings filtersSection = mainSettings 670 .getSection(OLD_FILTER_SECTION); 671 if (filtersSection != null) { 672 MarkerFilter markerFilter = createFilter(MarkerMessages.MarkerFilter_defaultFilterName); 673 markerFilter.restoreFilterSettings(filtersSection); 674 setFilters(new MarkerFilter[] { markerFilter }); 675 } 676 677 } else { 678 MarkerFilter[] newFilters = new MarkerFilter[sections.length]; 679 680 for (int i = 0; i < sections.length; i++) { 681 newFilters[i] = createFilter(sections[i].getID()); 682 newFilters[i].restoreState(sections[i]); 683 } 684 setFilters(newFilters); 685 } 686 687 if (markerFilters.length == 0) { createDefaultFilter(); 689 } 690 691 } 692 693 697 private void createDefaultFilter() { 698 MarkerFilter filter = createFilter(MarkerMessages.MarkerFilter_defaultFilterName); 699 setFilters(new MarkerFilter[] { filter }); 700 } 701 702 708 protected abstract MarkerFilter createFilter(String name); 709 710 715 protected abstract String getSectionTag(); 716 717 722 public void createPartControl(Composite parent) { 723 724 clipboard = new Clipboard(parent.getDisplay()); 725 super.createPartControl(parent); 726 727 initDragAndDrop(); 728 729 getSite().getPage().addSelectionListener(focusListener); 730 focusSelectionChanged(getSite().getPage().getActivePart(), getSite() 731 .getPage().getSelection()); 732 PlatformUI.getWorkbench().getWorkingSetManager() 733 .addPropertyChangeListener(getWorkingSetListener()); 734 735 getViewer().getControl().addHelpListener(new HelpListener() { 737 742 public void helpRequested(HelpEvent e) { 743 IContext context = contextProvider.getContext(getViewer() 744 .getControl()); 745 PlatformUI.getWorkbench().getHelpSystem().displayHelp(context); 746 } 747 }); 748 749 ResourcesPlugin.getWorkspace().addResourceChangeListener( 751 markerUpdateListener, 752 IResourceChangeEvent.POST_CHANGE 753 | IResourceChangeEvent.PRE_BUILD 754 | IResourceChangeEvent.POST_BUILD); 755 } 756 757 762 public Object getAdapter(Class adaptable) { 763 if (adaptable.equals(IContextProvider.class)) { 764 return contextProvider; 765 } 766 if (adaptable.equals(IShowInSource.class)) { 767 return new IShowInSource() { 768 public ShowInContext getShowInContext() { 769 ISelection selection = getViewer().getSelection(); 770 if (!(selection instanceof IStructuredSelection)) { 771 return null; 772 } 773 IStructuredSelection structured = (IStructuredSelection) selection; 774 Iterator markerIterator = structured.iterator(); 775 List newSelection = new ArrayList (); 776 while (markerIterator.hasNext()) { 777 ConcreteMarker element = (ConcreteMarker) markerIterator 778 .next(); 779 newSelection.add(element.getResource()); 780 } 781 return new ShowInContext(getViewer().getInput(), 782 new StructuredSelection(newSelection)); 783 } 784 785 }; 786 } 787 return super.getAdapter(adaptable); 788 } 789 790 795 protected void viewerSelectionChanged(IStructuredSelection selection) { 796 797 Object [] rawSelection = selection.toArray(); 798 799 List markers = new ArrayList (); 800 801 for (int idx = 0; idx < rawSelection.length; idx++) { 802 803 if (rawSelection[idx] instanceof ConcreteMarker) 804 markers.add(((ConcreteMarker) rawSelection[idx]).getMarker()); 805 } 806 807 setSelection(new StructuredSelection(markers)); 808 809 updateStatusMessage(selection); 810 } 811 812 817 public void dispose() { 818 super.dispose(); 819 cancelJobs(); 820 821 ResourcesPlugin.getWorkspace().removeResourceChangeListener( 822 markerUpdateListener); 823 PlatformUI.getWorkbench().getWorkingSetManager() 824 .removePropertyChangeListener(workingSetListener); 825 IDEWorkbenchPlugin.getDefault().getPreferenceStore() 826 .removePropertyChangeListener(preferenceListener); 827 getSite().getPage().removeSelectionListener(focusListener); 828 829 if (openAction != null) { 832 openAction.dispose(); 833 copyAction.dispose(); 834 selectAllAction.dispose(); 835 deleteAction.dispose(); 836 revealAction.dispose(); 837 propertiesAction.dispose(); 838 undoAction.dispose(); 839 redoAction.dispose(); 840 clipboard.dispose(); 841 } 842 if (showInMenu != null) { 843 showInMenu.dispose(); 844 } 845 } 846 847 852 protected void createActions() { 853 revealAction = new ActionRevealMarker(this, getViewer()); 854 openAction = new ActionOpenMarker(this, getViewer()); 855 copyAction = new ActionCopyMarker(this, getViewer()); 856 copyAction.setClipboard(clipboard); 857 copyAction.setProperties(getSortingFields()); 858 pasteAction = new ActionPasteMarker(this, getViewer(), getMarkerName()); 859 pasteAction.setClipboard(clipboard); 860 pasteAction.setPastableTypes(getMarkerTypes()); 861 deleteAction = new ActionRemoveMarker(this, getViewer(), 862 getMarkerName()); 863 selectAllAction = new ActionSelectAll(this); 864 propertiesAction = new ActionMarkerProperties(this, getViewer(), 865 getMarkerName()); 866 867 IUndoContext undoContext = getUndoContext(); 868 undoAction = new UndoActionHandler(getSite(), undoContext); 869 redoAction = new RedoActionHandler(getSite(), undoContext); 870 871 super.createActions(); 872 873 setFilterAction(new FiltersAction(this)); 874 875 setPreferencesAction(new ViewPreferencesAction() { 876 881 public void openViewPreferencesDialog() { 882 openPreferencesDialog(getMarkerEnablementPreferenceName(), 883 getMarkerLimitPreferenceName()); 884 885 } 886 887 }); 888 } 889 890 896 private void openPreferencesDialog(String markerEnablementPreferenceName, 897 String markerLimitPreferenceName) { 898 899 Dialog dialog = new MarkerViewPreferenceDialog(getSite() 900 .getWorkbenchWindow().getShell(), 901 markerEnablementPreferenceName, markerLimitPreferenceName, 902 MarkerMessages.MarkerPreferences_DialogTitle); 903 if (dialog.open() == Window.OK) { 904 refreshViewer(); 905 } 906 907 } 908 909 914 abstract String getMarkerLimitPreferenceName(); 915 916 abstract String [] getMarkerTypes(); 917 918 923 protected void initToolBar(IToolBarManager tbm) { 924 tbm.add(deleteAction); 925 tbm.add(getFilterAction()); 926 tbm.update(false); 927 } 928 929 934 protected void registerGlobalActions(IActionBars actionBars) { 935 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), 936 copyAction); 937 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), 938 pasteAction); 939 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), 940 deleteAction); 941 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 942 selectAllAction); 943 actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), 944 propertiesAction); 945 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), 946 undoAction); 947 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), 948 redoAction); 949 950 copyAction.setActionDefinitionId("org.eclipse.ui.edit.copy"); pasteAction.setActionDefinitionId("org.eclipse.ui.edit.paste"); deleteAction.setActionDefinitionId("org.eclipse.ui.edit.delete"); selectAllAction.setActionDefinitionId("org.eclipse.ui.edit.selectAll"); propertiesAction 955 .setActionDefinitionId("org.eclipse.ui.file.properties"); undoAction.setActionDefinitionId("org.eclipse.ui.edit.undo"); redoAction.setActionDefinitionId("org.eclipse.ui.edit.redo"); } 959 960 protected void initDragAndDrop() { 961 int operations = DND.DROP_COPY; 962 Transfer[] transferTypes = new Transfer[] { 963 MarkerTransfer.getInstance(), TextTransfer.getInstance() }; 964 DragSourceListener listener = new DragSourceAdapter() { 965 public void dragSetData(DragSourceEvent event) { 966 performDragSetData(event); 967 } 968 969 public void dragFinished(DragSourceEvent event) { 970 } 971 }; 972 973 getViewer().addDragSupport(operations, transferTypes, listener); 974 } 975 976 980 private void performDragSetData(DragSourceEvent event) { 981 if (MarkerTransfer.getInstance().isSupportedType(event.dataType)) { 982 983 event.data = getSelectedMarkers(); 984 return; 985 } 986 if (TextTransfer.getInstance().isSupportedType(event.dataType)) { 987 List selection = ((IStructuredSelection) getViewer().getSelection()) 988 .toList(); 989 try { 990 IMarker[] markers = new IMarker[selection.size()]; 991 selection.toArray(markers); 992 if (markers != null) { 993 event.data = copyAction.createMarkerReport(markers); 994 } 995 } catch (ArrayStoreException e) { 996 } 997 } 998 } 999 1000 1005 private IMarker[] getSelectedMarkers() { 1006 Object [] selection = ((IStructuredSelection) getViewer().getSelection()) 1007 .toArray(); 1008 ArrayList markers = new ArrayList (); 1009 for (int i = 0; i < selection.length; i++) { 1010 if (selection[i] instanceof ConcreteMarker) { 1011 markers.add(((ConcreteMarker) selection[i]).getMarker()); 1012 } 1013 } 1014 return (IMarker[]) markers.toArray(new IMarker[markers.size()]); 1015 } 1016 1017 1022 protected void fillContextMenu(IMenuManager manager) { 1023 if (manager == null) { 1024 return; 1025 } 1026 manager.add(openAction); 1027 createShowInMenu(manager); 1028 manager.add(new Separator()); 1029 manager.add(copyAction); 1030 pasteAction.updateEnablement(); 1031 manager.add(pasteAction); 1032 1033 if (canBeEditable()) { 1034 manager.add(deleteAction); 1035 } 1036 manager.add(selectAllAction); 1037 fillContextMenuAdditions(manager); 1038 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 1039 manager.add(new Separator()); 1040 manager.add(propertiesAction); 1041 } 1042 1043 1049 boolean canBeEditable() { 1050 return true; 1051 } 1052 1053 1058 abstract void fillContextMenuAdditions(IMenuManager manager); 1059 1060 1065 protected final MarkerFilter[] getUserFilters() { 1066 return markerFilters; 1067 } 1068 1069 1074 protected void handleKeyPressed(KeyEvent event) { 1075 } 1077 1078 1083 protected void handleOpenEvent(OpenEvent event) { 1084 if (openAction.isEnabled()) { 1085 openAction.run(); 1086 } 1087 } 1088 1089 1094 protected void saveSelection(IMemento memento) { 1095 IStructuredSelection selection = (IStructuredSelection) getViewer() 1096 .getSelection(); 1097 IMemento selectionMem = memento.createChild(TAG_SELECTION); 1098 for (Iterator iterator = selection.iterator(); iterator.hasNext();) { 1099 Object next = iterator.next(); 1100 if (!(next instanceof ConcreteMarker)) { 1101 continue; 1102 } 1103 ConcreteMarker marker = (ConcreteMarker) next; 1104 IMemento elementMem = selectionMem.createChild(TAG_MARKER); 1105 elementMem.putString(TAG_RESOURCE, marker.getMarker().getResource() 1106 .getFullPath().toString()); 1107 elementMem.putString(TAG_ID, String.valueOf(marker.getMarker() 1108 .getId())); 1109 } 1110 } 1111 1112 protected abstract String [] getRootTypes(); 1113 1114 1118 protected void focusSelectionChanged(IWorkbenchPart part, 1119 ISelection selection) { 1120 1121 List selectedElements = new ArrayList (); 1122 if (part instanceof IEditorPart) { 1123 IEditorPart editor = (IEditorPart) part; 1124 IFile file = ResourceUtil.getFile(editor.getEditorInput()); 1125 if (file == null) { 1126 IEditorInput editorInput = editor.getEditorInput(); 1127 if (editorInput != null) { 1128 Object mapping = editorInput 1129 .getAdapter(ResourceMapping.class); 1130 if (mapping != null) { 1131 selectedElements.add(mapping); 1132 } 1133 } 1134 } else { 1135 selectedElements.add(file); 1136 } 1137 } else { 1138 if (selection instanceof IStructuredSelection) { 1139 for (Iterator iterator = ((IStructuredSelection) selection) 1140 .iterator(); iterator.hasNext();) { 1141 Object object = iterator.next(); 1142 if (object instanceof IAdaptable) { 1143 ITaskListResourceAdapter taskListResourceAdapter; 1144 Object adapter = ((IAdaptable) object) 1145 .getAdapter(ITaskListResourceAdapter.class); 1146 if (adapter != null 1147 && adapter instanceof ITaskListResourceAdapter) { 1148 taskListResourceAdapter = (ITaskListResourceAdapter) adapter; 1149 } else { 1150 taskListResourceAdapter = DefaultMarkerResourceAdapter 1151 .getDefault(); 1152 } 1153 1154 IResource resource = taskListResourceAdapter 1155 .getAffectedResource((IAdaptable) object); 1156 if (resource == null) { 1157 Object mapping = ((IAdaptable) object) 1158 .getAdapter(ResourceMapping.class); 1159 if (mapping != null) { 1160 selectedElements.add(mapping); 1161 } 1162 } else { 1163 selectedElements.add(resource); 1164 } 1165 } 1166 } 1167 } 1168 } 1169 updateFocusMarkers(selectedElements.toArray()); 1170 } 1171 1172 1177 protected final void updateFilterSelection(Object [] elements) { 1178 1179 Collection resourceCollection = new ArrayList (); 1180 for (int i = 0; i < elements.length; i++) { 1181 if (elements[i] instanceof IResource) { 1182 resourceCollection.add(elements[i]); 1183 } else { 1184 addResources(resourceCollection, 1185 ((ResourceMapping) elements[i])); 1186 } 1187 } 1188 1189 IResource[] resources = new IResource[resourceCollection.size()]; 1190 resourceCollection.toArray(resources); 1191 1192 for (int i = 0; i < markerFilters.length; i++) { 1193 markerFilters[i].setFocusResource(resources); 1194 } 1195 1196 Iterator systemFilters = MarkerSupportRegistry.getInstance() 1197 .getRegisteredFilters().iterator(); 1198 1199 while (systemFilters.hasNext()) { 1200 MarkerFilter filter = (MarkerFilter) systemFilters.next(); 1201 filter.setFocusResource(resources); 1202 1203 } 1204 1205 } 1206 1207 1213 private void addResources(Collection resources, ResourceMapping mapping) { 1214 try { 1215 ResourceTraversal[] traversals = mapping.getTraversals( 1216 ResourceMappingContext.LOCAL_CONTEXT, 1217 new NullProgressMonitor()); 1218 for (int i = 0; i < traversals.length; i++) { 1219 ResourceTraversal traversal = traversals[i]; 1220 IResource[] result = traversal.getResources(); 1221 for (int j = 0; j < result.length; j++) { 1222 resources.add(result[j]); 1223 } 1224 } 1225 } catch (CoreException e) { 1226 Util.log(e); 1227 return; 1228 } 1229 1230 } 1231 1232 protected abstract String getStaticContextId(); 1233 1234 1239 void updateFocusMarkers(Object [] elements) { 1240 boolean updateNeeded = updateNeeded(focusElements, elements); 1241 if (updateNeeded) { 1242 focusElements = elements; 1243 refreshForFocusUpdate(); 1244 } 1245 } 1246 1247 private boolean updateNeeded(Object [] oldElements, Object [] newElements) { 1248 MarkerFilter[] filters = getEnabledFilters(); 1250 boolean updateNeeded = false; 1251 1252 for (int i = 0; i < filters.length; i++) { 1253 MarkerFilter filter = filters[i]; 1254 if (!filter.isEnabled()) { 1255 continue; 1256 } 1257 1258 int onResource = filter.getOnResource(); 1259 if (onResource == MarkerFilter.ON_ANY 1260 || onResource == MarkerFilter.ON_WORKING_SET) { 1261 continue; 1262 } 1263 if (newElements == null || newElements.length < 1) { 1264 continue; 1265 } 1266 if (oldElements == null || oldElements.length < 1) { 1267 return true; 1268 } 1269 if (Arrays.equals(oldElements, newElements)) { 1270 continue; 1271 } 1272 if (onResource == MarkerFilter.ON_ANY_IN_SAME_CONTAINER) { 1273 Collection oldProjects = MarkerFilter 1274 .getProjectsAsCollection(oldElements); 1275 Collection newProjects = MarkerFilter 1276 .getProjectsAsCollection(newElements); 1277 1278 if (oldProjects.size() == newProjects.size()) { 1279 if (newProjects.containsAll(oldProjects)) { 1280 continue; 1281 } 1282 } 1283 1284 return true; 1285 } 1286 updateNeeded = true; } 1289 1290 return updateNeeded; 1291 } 1292 1293 void updateTitle() { 1294 String status = Util.EMPTY_STRING; 1295 int filteredCount = getCurrentMarkers().getItemCount(); 1296 int totalCount = getTotalMarkers(); 1297 if (filteredCount == totalCount) { 1298 status = NLS.bind(MarkerMessages.filter_itemsMessage, new Integer ( 1299 totalCount)); 1300 } else { 1301 status = NLS.bind(MarkerMessages.filter_matchedMessage, 1302 new Integer (filteredCount), new Integer (totalCount)); 1303 } 1304 setContentDescription(status); 1305 } 1306 1307 1324 protected void updateStatusMessage() { 1325 ISelection selection = getViewer().getSelection(); 1326 1327 if (selection instanceof IStructuredSelection) { 1328 updateStatusMessage((IStructuredSelection) selection); 1329 } else { 1330 updateStatusMessage(null); 1331 } 1332 } 1333 1334 1351 protected void updateStatusMessage(IStructuredSelection selection) { 1352 String message = ""; 1354 if (selection == null || selection.size() == 0) { 1355 message = updateSummaryVisible(); 1357 } else if (selection.size() == 1) { 1358 Object first = selection.getFirstElement(); 1360 if (first instanceof ConcreteMarker) { 1361 message = ((ConcreteMarker) first).getDescription(); 1362 } 1363 } else if (selection.size() > 1) { 1364 message = updateSummarySelected(selection); 1366 } 1367 getViewSite().getActionBars().getStatusLineManager() 1368 .setMessage(message); 1369 } 1370 1371 1375 protected String updateSummarySelected(IStructuredSelection selection) { 1376 return MessageFormat.format( 1378 MarkerMessages.marker_statusSummarySelected, 1379 new Object [] { new Integer (selection.size()) }); 1380 } 1381 1382 1385 protected String updateSummaryVisible() { 1386 return ""; } 1388 1389 1393 public final void openFiltersDialog() { 1394 1395 DialogMarkerFilter dialog = createFiltersDialog(); 1396 1397 if (dialog.open() == Window.OK) { 1398 1399 MarkerFilter[] result = dialog.getFilters(); 1400 if (result == null) { 1401 return; 1402 } 1403 if (result.length == 0) { 1404 setFilters(new MarkerFilter[] { createFilter(MarkerMessages.MarkerFilter_defaultFilterName) }); 1405 } else { 1406 setFilters(result); 1407 } 1408 1409 updateForFilterChanges(); 1410 } 1411 } 1412 1413 1416 public void refreshViewer() { 1417 scheduleMarkerUpdate(Util.SHORT_DELAY); 1418 } 1419 1420 1425 void setFilters(MarkerFilter[] newFilters) { 1426 markerFilters = newFilters; 1427 } 1428 1429 1433 void clearEnabledFilters() { 1434 enabledFilters = null; 1435 } 1436 1437 1440 private void refreshFilterMenu() { 1441 if (filtersMenu == null) { 1442 return; 1443 } 1444 filtersMenu.removeAll(); 1445 MarkerFilter[] filters = getAllFilters(); 1446 for (int i = 0; i < filters.length; i++) { 1447 filtersMenu.add(new FilterEnablementAction(filters[i], this)); 1448 } 1449 1450 } 1451 1452 1455 protected abstract DialogMarkerFilter createFiltersDialog(); 1456 1457 1464 public void setSelection(IStructuredSelection structuredSelection, 1465 boolean reveal) { 1466 TreeViewer viewer = getViewer(); 1467 1468 List newSelection = new ArrayList (structuredSelection.size()); 1469 1470 for (Iterator i = structuredSelection.iterator(); i.hasNext();) { 1471 Object next = i.next(); 1472 if (next instanceof IMarker) { 1473 ConcreteMarker marker = getCurrentMarkers().getMarker( 1474 (IMarker) next); 1475 if (marker != null) { 1476 newSelection.add(marker); 1477 } 1478 } 1479 } 1480 1481 if (viewer != null) { 1482 viewer.setSelection(new StructuredSelection(newSelection), reveal); 1483 } 1484 } 1485 1486 protected MarkerList getVisibleMarkers() { 1487 return getCurrentMarkers(); 1488 } 1489 1490 1497 int getTotalMarkers() { 1498 1506 return totalMarkers; 1507 } 1508 1509 1514 public void showBusy(boolean busy) { 1515 super.showBusy(busy); 1516 1517 if (busy) { 1518 preBusyMarkers = totalMarkers; 1519 } else { if (totalMarkers != preBusyMarkers) { 1521 getProgressService().warnOfContentChange(); 1522 } 1523 } 1524 1525 } 1526 1527 1532 MarkerFilter[] getEnabledFilters() { 1533 1534 if (enabledFilters == null) { 1535 Collection filters = findEnabledFilters(); 1536 1537 enabledFilters = new MarkerFilter[filters.size()]; 1538 filters.toArray(enabledFilters); 1539 } 1540 return enabledFilters; 1541 1542 } 1543 1544 1549 protected Collection findEnabledFilters() { 1550 MarkerFilter[] allFilters = getAllFilters(); 1551 ArrayList filters = new ArrayList (0); 1552 for (int i = 0; i < allFilters.length; i++) { 1553 if (allFilters[i].isEnabled()) { 1554 filters.add(allFilters[i]); 1555 } 1556 } 1557 return filters; 1558 } 1559 1560 1565 MarkerFilter[] getAllFilters() { 1566 return getUserFilters(); 1567 } 1568 1569 1574 void addDropDownContributions(IMenuManager menu) { 1575 super.addDropDownContributions(menu); 1576 1577 menu.add(new Separator(MENU_FILTERS_GROUP)); 1578 filtersMenu = new MenuManager(MarkerMessages.filtersSubMenu_title); 1580 refreshFilterMenu(); 1581 menu.appendToGroup(MENU_FILTERS_GROUP, filtersMenu); 1582 } 1583 1584 1589 void createShowInMenu(IMenuManager menu) { 1590 ISelection selection = getViewer().getSelection(); 1591 if (!(selection instanceof IStructuredSelection)) { 1592 return; 1593 } 1594 1595 IStructuredSelection structured = (IStructuredSelection) selection; 1596 if (!Util.isSingleConcreteSelection(structured)) { 1597 return; 1598 } 1599 1600 menu.add(new Separator(MENU_SHOW_IN_GROUP)); 1601 1603 String showInLabel = IDEWorkbenchMessages.Workbench_showIn; 1604 IBindingService bindingService = (IBindingService) PlatformUI 1605 .getWorkbench().getAdapter(IBindingService.class); 1606 if (bindingService != null) { 1607 String keyBinding = bindingService 1608 .getBestActiveBindingFormattedFor("org.eclipse.ui.navigate.showInQuickMenu"); if (keyBinding != null) { 1610 showInLabel += '\t' + keyBinding; 1611 } 1612 } 1613 showInMenu = new MenuManager(showInLabel); 1614 showInMenu.add(ContributionItemFactory.VIEWS_SHOW_IN 1615 .create(getViewSite().getWorkbenchWindow())); 1616 1617 menu.appendToGroup(MENU_SHOW_IN_GROUP, showInMenu); 1618 1619 } 1620 1621 1626 void refreshMarkerCounts(IProgressMonitor monitor) { 1627 monitor.subTask(MarkerMessages.MarkerView_refreshing_counts); 1628 try { 1629 totalMarkers = MarkerList.compute(getMarkerTypes()).length; 1630 } catch (CoreException e) { 1631 Util.log(e); 1632 return; 1633 } 1634 1635 } 1636 1637 1642 int getMarkerLimit() { 1643 1644 if (IDEWorkbenchPlugin.getDefault().getPreferenceStore().getBoolean( 1646 getMarkerEnablementPreferenceName())) { 1647 return IDEWorkbenchPlugin.getDefault().getPreferenceStore().getInt( 1648 getMarkerLimitPreferenceName()); 1649 1650 } 1651 return -1; 1652 1653 } 1654 1655 1660 abstract String getMarkerEnablementPreferenceName(); 1661 1662 1667 Object createViewerInput() { 1668 adapter = new MarkerAdapter(this); 1669 return adapter; 1670 } 1671 1672 1677 public void addUpdateFinishListener(IJobChangeListener listener) { 1678 updateJob.addJobChangeListener(listener); 1679 1680 } 1681 1682 1687 public void removeUpdateFinishListener(IJobChangeListener listener) { 1688 updateJob.removeJobChangeListener(listener); 1689 1690 } 1691 1692 1697 private IPropertyChangeListener getWorkingSetListener() { 1698 workingSetListener = new IPropertyChangeListener() { 1699 1704 public void propertyChange(PropertyChangeEvent event) { 1705 clearEnabledFilters(); 1706 refreshViewer(); 1707 1708 } 1709 }; 1710 return workingSetListener; 1711 } 1712 1713 1718 void scheduleMarkerUpdate(int time) { 1719 cancelJobs(); 1720 getProgressService().schedule(markerProcessJob, time); 1721 } 1722 1723 1726 private void cancelJobs() { 1727 markerProcessJob.cancel(); 1728 updateJob.cancel(); 1729 } 1730 1731 1736 protected Tree createTree(Composite parent) { 1737 Tree tree = super.createTree(parent); 1738 tree.addTreeListener(new TreeAdapter() { 1739 1744 public void treeCollapsed(TreeEvent e) { 1745 updateJob.removeExpandedCategory((MarkerCategory) e.item 1746 .getData()); 1747 } 1748 1749 1754 public void treeExpanded(TreeEvent e) { 1755 updateJob 1756 .addExpandedCategory((MarkerCategory) e.item.getData()); 1757 } 1758 }); 1759 1760 return tree; 1761 } 1762 1763 1766 private void refreshForFocusUpdate() { 1767 if (focusElements != null) { 1768 updateFilterSelection(focusElements); 1769 refreshViewer(); 1770 } 1771 } 1772 1773 1776 protected void preserveSelection() { 1777 updateJob.saveSelection(getViewer().getSelection()); 1778 1779 } 1780 1781 1784 protected abstract String getMarkerName(); 1785 1786 1791 protected IUndoContext getUndoContext() { 1792 return (IUndoContext) ResourcesPlugin.getWorkspace().getAdapter( 1793 IUndoContext.class); 1794 } 1795 1796} 1797 | Popular Tags |