1 11 package org.eclipse.jdt.internal.ui.browsing; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Comparator ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.core.runtime.IPath; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IMarker; 26 import org.eclipse.core.resources.IResource; 27 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.dnd.DND; 30 import org.eclipse.swt.dnd.Transfer; 31 import org.eclipse.swt.events.KeyAdapter; 32 import org.eclipse.swt.events.KeyEvent; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Display; 35 import org.eclipse.swt.widgets.Menu; 36 import org.eclipse.swt.widgets.Shell; 37 38 import org.eclipse.help.IContextProvider; 39 40 import org.eclipse.jface.action.IAction; 41 import org.eclipse.jface.action.IMenuListener; 42 import org.eclipse.jface.action.IMenuManager; 43 import org.eclipse.jface.action.IStatusLineManager; 44 import org.eclipse.jface.action.IToolBarManager; 45 import org.eclipse.jface.action.MenuManager; 46 import org.eclipse.jface.util.IPropertyChangeListener; 47 import org.eclipse.jface.util.PropertyChangeEvent; 48 import org.eclipse.jface.util.TransferDragSourceListener; 49 import org.eclipse.jface.util.TransferDropTargetListener; 50 import org.eclipse.jface.viewers.IContentProvider; 51 import org.eclipse.jface.viewers.ILabelProvider; 52 import org.eclipse.jface.viewers.IOpenListener; 53 import org.eclipse.jface.viewers.ISelection; 54 import org.eclipse.jface.viewers.ISelectionChangedListener; 55 import org.eclipse.jface.viewers.ISelectionProvider; 56 import org.eclipse.jface.viewers.IStructuredSelection; 57 import org.eclipse.jface.viewers.OpenEvent; 58 import org.eclipse.jface.viewers.SelectionChangedEvent; 59 import org.eclipse.jface.viewers.StructuredSelection; 60 import org.eclipse.jface.viewers.StructuredViewer; 61 62 import org.eclipse.jface.text.ITextSelection; 63 64 import org.eclipse.ui.IActionBars; 65 import org.eclipse.ui.IEditorInput; 66 import org.eclipse.ui.IEditorPart; 67 import org.eclipse.ui.IFileEditorInput; 68 import org.eclipse.ui.IMemento; 69 import org.eclipse.ui.IPartListener2; 70 import org.eclipse.ui.ISelectionListener; 71 import org.eclipse.ui.IViewSite; 72 import org.eclipse.ui.IWorkbenchPage; 73 import org.eclipse.ui.IWorkbenchPart; 74 import org.eclipse.ui.IWorkbenchPartReference; 75 import org.eclipse.ui.IWorkbenchPartSite; 76 import org.eclipse.ui.IWorkingSet; 77 import org.eclipse.ui.IWorkingSetManager; 78 import org.eclipse.ui.PartInitException; 79 import org.eclipse.ui.PlatformUI; 80 import org.eclipse.ui.actions.ActionContext; 81 import org.eclipse.ui.actions.ActionGroup; 82 import org.eclipse.ui.part.IShowInSource; 83 import org.eclipse.ui.part.ResourceTransfer; 84 import org.eclipse.ui.part.ShowInContext; 85 import org.eclipse.ui.part.ViewPart; 86 import org.eclipse.ui.texteditor.ITextEditor; 87 88 import org.eclipse.ui.views.navigator.LocalSelectionTransfer; 89 90 import org.eclipse.search.ui.ISearchResultViewPart; 91 92 import org.eclipse.jdt.core.IClassFile; 93 import org.eclipse.jdt.core.ICompilationUnit; 94 import org.eclipse.jdt.core.IJavaElement; 95 import org.eclipse.jdt.core.IPackageDeclaration; 96 import org.eclipse.jdt.core.IPackageFragment; 97 import org.eclipse.jdt.core.IType; 98 import org.eclipse.jdt.core.JavaCore; 99 import org.eclipse.jdt.core.JavaModelException; 100 101 import org.eclipse.jdt.internal.corext.util.Messages; 102 103 import org.eclipse.jdt.ui.IContextMenuConstants; 104 import org.eclipse.jdt.ui.IWorkingCopyManager; 105 import org.eclipse.jdt.ui.JavaElementComparator; 106 import org.eclipse.jdt.ui.JavaElementLabelProvider; 107 import org.eclipse.jdt.ui.JavaElementLabels; 108 import org.eclipse.jdt.ui.JavaUI; 109 import org.eclipse.jdt.ui.PreferenceConstants; 110 import org.eclipse.jdt.ui.actions.BuildActionGroup; 111 import org.eclipse.jdt.ui.actions.CCPActionGroup; 112 import org.eclipse.jdt.ui.actions.CustomFiltersActionGroup; 113 import org.eclipse.jdt.ui.actions.GenerateActionGroup; 114 import org.eclipse.jdt.ui.actions.ImportActionGroup; 115 import org.eclipse.jdt.ui.actions.JavaSearchActionGroup; 116 import org.eclipse.jdt.ui.actions.OpenEditorActionGroup; 117 import org.eclipse.jdt.ui.actions.OpenViewActionGroup; 118 import org.eclipse.jdt.ui.actions.RefactorActionGroup; 119 120 import org.eclipse.jdt.internal.ui.JavaPlugin; 121 import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup; 122 import org.eclipse.jdt.internal.ui.actions.NewWizardsActionGroup; 123 import org.eclipse.jdt.internal.ui.dnd.DelegatingDropAdapter; 124 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDragAdapter; 125 import org.eclipse.jdt.internal.ui.dnd.ResourceTransferDragAdapter; 126 import org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView; 127 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 128 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 129 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDragAdapter; 130 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDropAdapter; 131 import org.eclipse.jdt.internal.ui.search.SearchUtil; 132 import org.eclipse.jdt.internal.ui.util.JavaUIHelp; 133 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; 134 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; 135 import org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider; 136 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 137 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; 138 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer; 139 import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater; 140 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetFilterActionGroup; 141 142 143 abstract class JavaBrowsingPart extends ViewPart implements IMenuListener, ISelectionListener, IViewPartInputProvider { 144 145 private static final String TAG_SELECTED_ELEMENTS= "selectedElements"; private static final String TAG_SELECTED_ELEMENT= "selectedElement"; private static final String TAG_LOGICAL_PACKAGE= "logicalPackage"; private static final String TAG_SELECTED_ELEMENT_PATH= "selectedElementPath"; 150 private JavaUILabelProvider fLabelProvider; 151 private ILabelProvider fTitleProvider; 152 private StructuredViewer fViewer; 153 private IMemento fMemento; 154 private JavaElementTypeComparator fTypeComparator; 155 156 private WorkingSetFilterActionGroup fWorkingSetFilterActionGroup; 158 private boolean fHasWorkingSetFilter= true; 159 private boolean fHasCustomFilter= true; 160 private OpenEditorActionGroup fOpenEditorGroup; 161 private CCPActionGroup fCCPActionGroup; 162 private BuildActionGroup fBuildActionGroup; 163 private ToggleLinkingAction fToggleLinkingAction; 164 protected CompositeActionGroup fActionGroups; 165 166 167 private CustomFiltersActionGroup fCustomFiltersActionGroup; 169 170 protected IWorkbenchPart fPreviousSelectionProvider; 171 protected Object fPreviousSelectedElement; 172 173 private boolean fLinkingEnabled; 175 176 180 private boolean fProcessSelectionEvents= true; 181 182 private IPartListener2 fPartListener= new IPartListener2() { 183 public void partActivated(IWorkbenchPartReference ref) { 184 } 185 public void partBroughtToTop(IWorkbenchPartReference ref) { 186 } 187 public void partInputChanged(IWorkbenchPartReference ref) { 188 } 189 public void partClosed(IWorkbenchPartReference ref) { 190 } 191 public void partDeactivated(IWorkbenchPartReference ref) { 192 } 193 public void partOpened(IWorkbenchPartReference ref) { 194 } 195 public void partVisible(IWorkbenchPartReference ref) { 196 if (ref != null && ref.getId() == getSite().getId()){ 197 fProcessSelectionEvents= true; 198 IWorkbenchPage page= getSite().getWorkbenchWindow().getActivePage(); 199 if (page != null) 200 selectionChanged(page.getActivePart(), page.getSelection()); 201 } 202 } 203 public void partHidden(IWorkbenchPartReference ref) { 204 if (ref != null && ref.getId() == getSite().getId()) 205 fProcessSelectionEvents= false; 206 } 207 }; 208 209 public JavaBrowsingPart() { 210 super(); 211 initLinkingEnabled(); 212 } 213 214 217 public void init(IViewSite site, IMemento memento) throws PartInitException { 218 super.init(site, memento); 219 fMemento= memento; 220 } 221 222 225 public void saveState(IMemento memento) { 226 if (fViewer == null) { 227 if (fMemento != null) memento.putMemento(fMemento); 230 return; 231 } 232 if (fHasWorkingSetFilter) 233 fWorkingSetFilterActionGroup.saveState(memento); 234 if (fHasCustomFilter) 235 fCustomFiltersActionGroup.saveState(memento); 236 saveSelectionState(memento); 237 saveLinkingEnabled(memento); 238 } 239 240 private void saveLinkingEnabled(IMemento memento) { 241 memento.putInteger(getLinkToEditorKey(), fLinkingEnabled ? 1 : 0); 242 } 243 244 private void saveSelectionState(IMemento memento) { 245 Object elements[]= ((IStructuredSelection) fViewer.getSelection()).toArray(); 246 if (elements.length > 0) { 247 IMemento selectionMem= memento.createChild(TAG_SELECTED_ELEMENTS); 248 for (int i= 0; i < elements.length; i++) { 249 IMemento elementMem= selectionMem.createChild(TAG_SELECTED_ELEMENT); 250 Object o= elements[i]; 251 if (o instanceof IJavaElement) 252 elementMem.putString(TAG_SELECTED_ELEMENT_PATH, ((IJavaElement) elements[i]).getHandleIdentifier()); 253 else if (o instanceof LogicalPackage) { 254 IPackageFragment[] packages=((LogicalPackage)o).getFragments(); 255 for (int j= 0; j < packages.length; j++) { 256 IMemento packageMem= elementMem.createChild(TAG_LOGICAL_PACKAGE); 257 packageMem.putString(TAG_SELECTED_ELEMENT_PATH, packages[j].getHandleIdentifier()); 258 } 259 } 260 } 261 } 262 } 263 264 protected void restoreState(IMemento memento) { 265 if (fHasWorkingSetFilter) 266 fWorkingSetFilterActionGroup.restoreState(memento); 267 if (fHasCustomFilter) 268 fCustomFiltersActionGroup.restoreState(memento); 269 270 if (fHasCustomFilter || fHasWorkingSetFilter) { 271 fViewer.getControl().setRedraw(false); 272 fViewer.refresh(); 273 fViewer.getControl().setRedraw(true); 274 } 275 } 276 277 private ISelection restoreSelectionState(IMemento memento) { 278 if (memento == null) 279 return null; 280 281 IMemento childMem; 282 childMem= memento.getChild(TAG_SELECTED_ELEMENTS); 283 if (childMem != null) { 284 ArrayList list= new ArrayList (); 285 IMemento[] elementMem= childMem.getChildren(TAG_SELECTED_ELEMENT); 286 for (int i= 0; i < elementMem.length; i++) { 287 String javaElementHandle= elementMem[i].getString(TAG_SELECTED_ELEMENT_PATH); 288 if (javaElementHandle == null) { 289 IMemento[] packagesMem= elementMem[i].getChildren(TAG_LOGICAL_PACKAGE); 291 LogicalPackage lp= null; 292 for (int j= 0; j < packagesMem.length; j++) { 293 javaElementHandle= packagesMem[j].getString(TAG_SELECTED_ELEMENT_PATH); 294 Object pack= JavaCore.create(javaElementHandle); 295 if (pack instanceof IPackageFragment && ((IPackageFragment)pack).exists()) { 296 if (lp == null) 297 lp= new LogicalPackage((IPackageFragment)pack); 298 else 299 lp.add((IPackageFragment)pack); 300 } 301 } 302 if (lp != null) 303 list.add(lp); 304 } else { 305 IJavaElement element= JavaCore.create(javaElementHandle); 306 if (element != null && element.exists()) 307 list.add(element); 308 } 309 } 310 return new StructuredSelection(list); 311 } 312 return null; 313 } 314 315 private void restoreLinkingEnabled(IMemento memento) { 316 Integer val= memento.getInteger(getLinkToEditorKey()); 317 if (val != null) { 318 fLinkingEnabled= val.intValue() != 0; 319 } 320 } 321 322 325 public void createPartControl(Composite parent) { 326 Assert.isTrue(fViewer == null); 327 328 329 fTypeComparator= new JavaElementTypeComparator(); 330 331 fViewer= createViewer(parent); 333 334 initDragAndDrop(); 335 336 fLabelProvider= createLabelProvider(); 337 fViewer.setLabelProvider(createDecoratingLabelProvider(fLabelProvider)); 338 339 fViewer.setComparator(createJavaElementComparator()); 340 fViewer.setUseHashlookup(true); 341 fTitleProvider= createTitleProvider(); 342 343 createContextMenu(); 344 getSite().setSelectionProvider(fViewer); 345 346 if (fMemento != null) { restoreLinkingEnabled(fMemento); 348 } 349 350 createActions(); addKeyListener(); 352 353 if (fMemento != null) 354 restoreState(fMemento); 355 356 getSite().setSelectionProvider(fViewer); 357 358 IStatusLineManager slManager= getViewSite().getActionBars().getStatusLineManager(); 360 fViewer.addSelectionChangedListener(createStatusBarUpdater(slManager)); 361 362 363 hookViewerListeners(); 364 365 addFilters(); 367 368 fViewer.setContentProvider(createContentProvider()); 370 setInitialInput(); 371 372 setInitialSelection(); 374 fMemento= null; 375 376 getViewSite().getPage().addPostSelectionListener(this); 378 getViewSite().getPage().addPartListener(fPartListener); 379 380 fillActionBars(getViewSite().getActionBars()); 381 382 setHelp(); 383 } 384 385 388 public Object getAdapter(Class key) { 389 if (key == IShowInSource.class) { 390 return getShowInSource(); 391 } 392 if (key == IContextProvider.class) 393 return JavaUIHelp.getHelpContextProvider(this, getHelpContextId()); 394 395 return super.getAdapter(key); 396 } 397 398 402 protected IShowInSource getShowInSource() { 403 return new IShowInSource() { 404 public ShowInContext getShowInContext() { 405 return new ShowInContext( 406 null, 407 getSite().getSelectionProvider().getSelection()); 408 } 409 }; 410 } 411 412 protected DecoratingJavaLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) { 413 return new DecoratingJavaLabelProvider(provider); 416 } 417 418 protected JavaElementComparator createJavaElementComparator() { 419 return new JavaElementComparator(); 420 } 421 422 protected StatusBarUpdater createStatusBarUpdater(IStatusLineManager slManager) { 423 return new StatusBarUpdater(slManager); 424 } 425 426 protected void createContextMenu() { 427 MenuManager menuManager= new MenuManager("#PopupMenu"); menuManager.setRemoveAllWhenShown(true); 429 menuManager.addMenuListener(this); 430 Menu contextMenu= menuManager.createContextMenu(fViewer.getControl()); 431 fViewer.getControl().setMenu(contextMenu); 432 getSite().registerContextMenu(menuManager, fViewer); 433 } 434 435 protected void initDragAndDrop() { 436 int ops= DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK; 437 Transfer[] dropTransfers= new Transfer[] { 439 LocalSelectionTransfer.getInstance() 440 }; 441 TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] { 442 new SelectionTransferDropAdapter(fViewer) 443 }; 444 fViewer.addDropSupport(ops | DND.DROP_DEFAULT, dropTransfers, new DelegatingDropAdapter(dropListeners)); 445 446 Transfer[] dragTransfers= new Transfer[] { 448 LocalSelectionTransfer.getInstance(), 449 ResourceTransfer.getInstance()}; 450 TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] { 451 new SelectionTransferDragAdapter(fViewer), 452 new ResourceTransferDragAdapter(fViewer) 453 }; 454 fViewer.addDragSupport(ops, dragTransfers, new JdtViewerDragAdapter(fViewer, dragListeners)); 455 } 456 457 protected void fillActionBars(IActionBars actionBars) { 458 IToolBarManager toolBar= actionBars.getToolBarManager(); 459 fillToolBar(toolBar); 460 461 462 if (fHasWorkingSetFilter) 463 fWorkingSetFilterActionGroup.fillActionBars(getViewSite().getActionBars()); 464 465 actionBars.updateActionBars(); 466 467 fActionGroups.fillActionBars(actionBars); 468 469 if (fHasCustomFilter) 470 fCustomFiltersActionGroup.fillActionBars(actionBars); 471 472 IMenuManager menu= actionBars.getMenuManager(); 473 menu.add(fToggleLinkingAction); 474 } 475 476 478 479 public void setFocus() { 480 fViewer.getControl().setFocus(); 481 } 482 483 public void dispose() { 484 if (fViewer != null) { 485 getViewSite().getPage().removePostSelectionListener(this); 486 getViewSite().getPage().removePartListener(fPartListener); 487 fViewer= null; 488 } 489 if (fActionGroups != null) 490 fActionGroups.dispose(); 491 492 if (fWorkingSetFilterActionGroup != null) { 493 fWorkingSetFilterActionGroup.dispose(); 494 } 495 496 super.dispose(); 497 } 498 499 502 protected void addKeyListener() { 503 fViewer.getControl().addKeyListener(new KeyAdapter() { 504 public void keyReleased(KeyEvent event) { 505 handleKeyReleased(event); 506 } 507 }); 508 } 509 510 protected void handleKeyReleased(KeyEvent event) { 511 if (event.stateMask != 0) 512 return; 513 514 int key= event.keyCode; 515 if (key == SWT.F5) { 516 IAction action= fBuildActionGroup.getRefreshAction(); 517 if (action.isEnabled()) 518 action.run(); 519 } 520 } 521 522 524 protected void fillToolBar(IToolBarManager tbm) { 525 } 526 527 530 public void menuAboutToShow(IMenuManager menu) { 531 JavaPlugin.createStandardGroups(menu); 532 533 IStructuredSelection selection= (IStructuredSelection) fViewer.getSelection(); 534 int size= selection.size(); 535 Object element= selection.getFirstElement(); 536 537 if (size == 1) 538 addOpenNewWindowAction(menu, element); 539 fActionGroups.setContext(new ActionContext(selection)); 540 fActionGroups.fillContextMenu(menu); 541 fActionGroups.setContext(null); 542 } 543 544 private void addOpenNewWindowAction(IMenuManager menu, Object element) { 545 if (element instanceof IJavaElement) { 546 element= ((IJavaElement)element).getResource(); 547 } 548 if (!(element instanceof IContainer)) 549 return; 550 menu.appendToGroup( 551 IContextMenuConstants.GROUP_OPEN, 552 new PatchedOpenInNewWindowAction(getSite().getWorkbenchWindow(), (IContainer)element)); 553 } 554 555 protected void createActions() { 556 fActionGroups= new CompositeActionGroup(new ActionGroup[] { 557 new NewWizardsActionGroup(this.getSite()), 558 fOpenEditorGroup= new OpenEditorActionGroup(this), 559 new OpenViewActionGroup(this), 560 fCCPActionGroup= new CCPActionGroup(this), 561 new GenerateActionGroup(this), 562 new RefactorActionGroup(this), 563 new ImportActionGroup(this), 564 fBuildActionGroup= new BuildActionGroup(this), 565 new JavaSearchActionGroup(this)}); 566 567 568 if (fHasWorkingSetFilter) { 569 String viewId= getConfigurationElement().getAttribute("id"); Assert.isNotNull(viewId); 571 IPropertyChangeListener workingSetListener= new IPropertyChangeListener() { 572 public void propertyChange(PropertyChangeEvent event) { 573 doWorkingSetChanged(event); 574 } 575 }; 576 fWorkingSetFilterActionGroup= new WorkingSetFilterActionGroup(getSite(), workingSetListener); 577 fViewer.addFilter(fWorkingSetFilterActionGroup.getWorkingSetFilter()); 578 } 579 580 if (fHasCustomFilter) 582 fCustomFiltersActionGroup= new CustomFiltersActionGroup(this, fViewer); 583 584 fToggleLinkingAction= new ToggleLinkingAction(this); 585 } 586 587 private void doWorkingSetChanged(PropertyChangeEvent event) { 588 String property= event.getProperty(); 589 if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property)) 590 updateTitle(); 591 else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property)) { 592 updateTitle(); 593 fViewer.getControl().setRedraw(false); 594 fViewer.refresh(); 595 fViewer.getControl().setRedraw(true); 596 } 597 598 } 599 600 601 606 Shell getShell() { 607 return fViewer.getControl().getShell(); 608 } 609 610 protected final Display getDisplay() { 611 return fViewer.getControl().getDisplay(); 612 } 613 614 618 ISelectionProvider getSelectionProvider() { 619 return fViewer; 620 } 621 622 629 abstract protected boolean isValidInput(Object element); 630 631 638 protected boolean isValidElement(Object element) { 639 if (!(element instanceof IJavaElement)) { 640 return false; 641 } 642 Object input= getViewer().getInput(); 643 if (input == null) 644 return false; 645 if (input instanceof Collection ) 646 return ((Collection )input).contains(element); 647 else 648 return input.equals(element); 649 650 } 651 652 private boolean isInputResetBy(Object newInput, Object input, IWorkbenchPart part) { 653 if (newInput == null) 654 return part == fPreviousSelectionProvider; 655 656 if (input instanceof IJavaElement && newInput instanceof IJavaElement) 657 return getTypeComparator().compare(newInput, input) > 0; 658 659 if((newInput instanceof List ) && (part instanceof PackagesView)) 660 return true; 661 662 else 663 return false; 664 } 665 666 private boolean isInputResetBy(IWorkbenchPart part) { 667 if (!(part instanceof JavaBrowsingPart)) 668 return true; 669 Object thisInput= getViewer().getInput(); 670 Object partInput= ((JavaBrowsingPart)part).getViewer().getInput(); 671 672 if(thisInput instanceof Collection ) 673 thisInput= ((Collection )thisInput).iterator().next(); 674 675 if(partInput instanceof Collection ) 676 partInput= ((Collection )partInput).iterator().next(); 677 678 if (thisInput instanceof IJavaElement && partInput instanceof IJavaElement) 679 return getTypeComparator().compare(partInput, thisInput) > 0; 680 else 681 return true; 682 } 683 684 protected boolean isAncestorOf(Object ancestor, Object element) { 685 if (element instanceof IJavaElement && ancestor instanceof IJavaElement) 686 return !element.equals(ancestor) && internalIsAncestorOf((IJavaElement)ancestor, (IJavaElement)element); 687 return false; 688 } 689 690 private boolean internalIsAncestorOf(IJavaElement ancestor, IJavaElement element) { 691 if (element != null) 692 return element.equals(ancestor) || internalIsAncestorOf(ancestor, element.getParent()); 693 else 694 return false; 695 } 696 697 private boolean isSearchResultView(IWorkbenchPart part) { 698 return SearchUtil.isSearchPlugInActivated() && part instanceof ISearchResultViewPart; 699 } 700 701 protected boolean needsToProcessSelectionChanged(IWorkbenchPart part, ISelection selection) { 702 if (!fProcessSelectionEvents || part == this || isSearchResultView(part) || part instanceof AbstractInfoView){ 703 if (part == this) 704 fPreviousSelectionProvider= part; 705 return false; 706 } 707 return true; 708 } 709 710 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 711 if (!needsToProcessSelectionChanged(part, selection)) 712 return; 713 714 if (fToggleLinkingAction.isChecked() && (part instanceof ITextEditor)) { 715 setSelectionFromEditor(part, selection); 716 return; 717 } 718 719 if (!(selection instanceof IStructuredSelection)) 720 return; 721 722 Object selectedElement= getSingleElementFromSelection(selection); 724 725 if (selectedElement != null && (part == null || part.equals(fPreviousSelectionProvider)) && selectedElement.equals(fPreviousSelectedElement)) 726 return; 727 728 fPreviousSelectedElement= selectedElement; 729 730 Object currentInput= getViewer().getInput(); 731 if (selectedElement != null && selectedElement.equals(currentInput)) { 732 IJavaElement elementToSelect= findElementToSelect(selectedElement); 733 if (elementToSelect != null && getTypeComparator().compare(selectedElement, elementToSelect) < 0) 734 setSelection(new StructuredSelection(elementToSelect), true); 735 else if (elementToSelect == null && (this instanceof MembersView)) { 736 setSelection(StructuredSelection.EMPTY, true); 737 fPreviousSelectedElement= StructuredSelection.EMPTY; 738 } 739 fPreviousSelectionProvider= part; 740 return; 741 } 742 743 if (part != fPreviousSelectionProvider && selectedElement != null && !selectedElement.equals(currentInput) && isInputResetBy(selectedElement, currentInput, part)) { 745 if (!isAncestorOf(selectedElement, currentInput)) 746 setInput(null); 747 fPreviousSelectionProvider= part; 748 return; 749 } else if (selection.isEmpty() && !isInputResetBy(part)) { 750 fPreviousSelectionProvider= part; 751 return; 752 } else if (selectedElement == null && part == fPreviousSelectionProvider) { 753 setInput(null); 754 fPreviousSelectionProvider= part; 755 return; 756 } 757 fPreviousSelectionProvider= part; 758 759 adjustInputAndSetSelection(selectedElement); 761 } 762 763 764 void setHasWorkingSetFilter(boolean state) { 765 fHasWorkingSetFilter= state; 766 } 767 768 void setHasCustomSetFilter(boolean state) { 769 fHasCustomFilter= state; 770 } 771 772 protected Object getInput() { 773 return fViewer.getInput(); 774 } 775 776 protected void setInput(Object input) { 777 setViewerInput(input); 778 updateTitle(); 779 } 780 781 boolean isLinkingEnabled() { 782 return fLinkingEnabled; 783 } 784 785 private void initLinkingEnabled() { 786 fLinkingEnabled= PreferenceConstants.getPreferenceStore().getBoolean(getLinkToEditorKey()); 787 } 788 789 private void setViewerInput(Object input) { 790 fProcessSelectionEvents= false; 791 fViewer.setInput(input); 792 fProcessSelectionEvents= true; 793 } 794 795 void updateTitle() { 796 setTitleToolTip(getToolTipText(fViewer.getInput())); 797 } 798 799 804 String getToolTipText(Object element) { 805 String result; 806 if (!(element instanceof IResource)) { 807 result= JavaElementLabels.getTextLabel(element, AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS); 808 } else { 809 IPath path= ((IResource) element).getFullPath(); 810 if (path.isRoot()) { 811 result= getConfigurationElement().getAttribute("name"); } else { 813 result= path.makeRelative().toString(); 814 } 815 } 816 817 if (fWorkingSetFilterActionGroup == null || fWorkingSetFilterActionGroup.getWorkingSet() == null) 818 return result; 819 820 IWorkingSet ws= fWorkingSetFilterActionGroup.getWorkingSet(); 821 String wsstr= Messages.format(JavaBrowsingMessages.JavaBrowsingPart_toolTip, new String [] { ws.getLabel() }); 822 if (result.length() == 0) 823 return wsstr; 824 return Messages.format(JavaBrowsingMessages.JavaBrowsingPart_toolTip2, new String [] { result, ws.getLabel() }); 825 } 826 827 830 public String getTitleToolTip() { 831 if (fViewer == null) 832 return super.getTitleToolTip(); 833 return getToolTipText(fViewer.getInput()); 834 } 835 836 protected final StructuredViewer getViewer() { 837 return fViewer; 838 } 839 840 protected final void setViewer(StructuredViewer viewer){ 841 fViewer= viewer; 842 } 843 844 protected JavaUILabelProvider createLabelProvider() { 845 return new AppearanceAwareLabelProvider( 846 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS, 847 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS 848 ); 849 } 850 851 protected ILabelProvider createTitleProvider() { 852 return new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_SMALL_ICONS); 853 } 854 855 protected final ILabelProvider getLabelProvider() { 856 return fLabelProvider; 857 } 858 859 protected final ILabelProvider getTitleProvider() { 860 return fTitleProvider; 861 } 862 863 869 protected StructuredViewer createViewer(Composite parent) { 870 return new ProblemTableViewer(parent, SWT.MULTI); 871 } 872 873 protected int getLabelProviderFlags() { 874 return JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_OVERLAY_ICONS | 875 JavaElementLabelProvider.SHOW_SMALL_ICONS | JavaElementLabelProvider.SHOW_VARIABLE | JavaElementLabelProvider.SHOW_PARAMETERS; 876 } 877 878 881 protected void addFilters() { 882 } 884 885 889 protected IContentProvider createContentProvider() { 890 return new JavaBrowsingContentProvider(true, this); 891 } 892 893 protected void setInitialInput() { 894 ISelection selection= getSite().getPage().getSelection(); 896 Object input= getSingleElementFromSelection(selection); 897 if (!(input instanceof IJavaElement)) { 898 input= getSite().getPage().getInput(); 900 if (!(input instanceof IJavaElement) && input instanceof IAdaptable) 901 input= ((IAdaptable)input).getAdapter(IJavaElement.class); 902 } 903 setInput(findInputForJavaElement((IJavaElement)input)); 904 } 905 906 protected void setInitialSelection() { 907 Object input; 909 IWorkbenchPage page= getSite().getPage(); 910 ISelection selection= null; 911 if (page != null) 912 selection= page.getSelection(); 913 if (selection instanceof ITextSelection) { 914 Object part= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 915 if (part instanceof IEditorPart) { 916 setSelectionFromEditor((IEditorPart)part); 917 if (fViewer.getSelection() != null) 918 return; 919 } 920 } 921 922 if (selection == null || selection.isEmpty()) 924 selection= restoreSelectionState(fMemento); 925 926 if (selection == null || selection.isEmpty()) { 927 input= getSite().getPage().getInput(); 929 if (!(input instanceof IJavaElement)) { 930 if (input instanceof IAdaptable) 931 input= ((IAdaptable)input).getAdapter(IJavaElement.class); 932 else 933 return; 934 } 935 selection= new StructuredSelection(input); 936 } 937 selectionChanged(null, selection); 938 } 939 940 protected final void setHelp() { 941 JavaUIHelp.setHelp(fViewer, getHelpContextId()); 942 } 943 944 949 abstract protected String getHelpContextId(); 950 951 956 abstract protected String getLinkToEditorKey(); 957 958 963 protected void hookViewerListeners() { 964 fViewer.addPostSelectionChangedListener(new ISelectionChangedListener() { 965 public void selectionChanged(SelectionChangedEvent event) { 966 if (!fProcessSelectionEvents) 967 return; 968 969 fPreviousSelectedElement= getSingleElementFromSelection(event.getSelection()); 970 971 IWorkbenchPage page= getSite().getPage(); 972 if (page == null) 973 return; 974 975 if (page.equals(JavaPlugin.getActivePage()) && JavaBrowsingPart.this.equals(page.getActivePart())) { 976 linkToEditor((IStructuredSelection)event.getSelection()); 977 } 978 } 979 }); 980 981 fViewer.addOpenListener(new IOpenListener() { 982 public void open(OpenEvent event) { 983 IAction open= fOpenEditorGroup.getOpenAction(); 984 if (open.isEnabled()) { 985 open.run(); 986 restoreSelection(); 987 } 988 } 989 }); 990 } 991 992 void restoreSelection() { 993 } 995 996 void adjustInputAndSetSelection(Object o) { 997 if (!(o instanceof IJavaElement)) { 998 if (o == null) 999 setInput(null); 1000 setSelection(StructuredSelection.EMPTY, true); 1001 return; 1002 } 1003 1004 IJavaElement je= (IJavaElement)o; 1005 IJavaElement elementToSelect= findElementToSelect(je); 1006 IJavaElement newInput= findInputForJavaElement(je); 1007 IJavaElement oldInput= null; 1008 if (getInput() instanceof IJavaElement) 1009 oldInput= (IJavaElement)getInput(); 1010 1011 if (elementToSelect == null && !isValidInput(newInput) && (newInput == null && !isAncestorOf(je, oldInput))) 1012 setInput(null); 1014 else if (mustSetNewInput(elementToSelect, oldInput, newInput)) { 1015 setInput(newInput); 1017 } 1018 1019 if (elementToSelect != null && elementToSelect.exists()) 1020 setSelection(new StructuredSelection(elementToSelect), true); 1021 else 1022 setSelection(StructuredSelection.EMPTY, true); 1023 } 1024 1025 1034 private boolean mustSetNewInput(IJavaElement elementToSelect, IJavaElement oldInput, IJavaElement newInput) { 1035 return (newInput == null || !newInput.equals(oldInput)) 1036 && (elementToSelect == null 1037 || oldInput == null 1038 || (!((elementToSelect instanceof IPackageDeclaration) 1039 && (elementToSelect.getParent().equals(oldInput.getParent())) 1040 && (!isAncestorOf(getViewPartInput(), elementToSelect))))); 1041 } 1042 1043 1050 protected IJavaElement findInputForJavaElement(IJavaElement je) { 1051 if (je == null || !je.exists()) 1052 return null; 1053 if (isValidInput(je)) 1054 return je; 1055 return findInputForJavaElement(je.getParent()); 1056 } 1057 1058 protected final IJavaElement findElementToSelect(Object obj) { 1059 if (obj instanceof IJavaElement) 1060 return findElementToSelect((IJavaElement)obj); 1061 return null; 1062 } 1063 1064 1070 abstract protected IJavaElement findElementToSelect(IJavaElement je); 1071 1072 1073 protected final Object getSingleElementFromSelection(ISelection selection) { 1074 if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) 1075 return null; 1076 1077 Iterator iter= ((IStructuredSelection)selection).iterator(); 1078 Object firstElement= iter.next(); 1079 if (!(firstElement instanceof IJavaElement)) { 1080 if (firstElement instanceof IMarker) 1081 firstElement= ((IMarker)firstElement).getResource(); 1082 if (firstElement instanceof IAdaptable) { 1083 IJavaElement je= (IJavaElement)((IAdaptable)firstElement).getAdapter(IJavaElement.class); 1084 if (je == null && firstElement instanceof IFile) { 1085 IContainer parent= ((IFile)firstElement).getParent(); 1086 if (parent != null) 1087 return parent.getAdapter(IJavaElement.class); 1088 else return null; 1089 } else 1090 return je; 1091 1092 } else 1093 return firstElement; 1094 } 1095 Object currentInput= getViewer().getInput(); 1096 if (currentInput == null || !currentInput.equals(findInputForJavaElement((IJavaElement)firstElement))) 1097 if (iter.hasNext()) 1098 return null; 1100 else 1101 return firstElement; 1103 1104 while (iter.hasNext()) { 1106 Object element= iter.next(); 1107 if (!(element instanceof IJavaElement)) 1108 return null; 1109 if (!currentInput.equals(findInputForJavaElement((IJavaElement)element))) 1110 return null; 1111 } 1112 return firstElement; 1113 } 1114 1115 1119 protected Comparator getTypeComparator() { 1120 return fTypeComparator; 1121 } 1122 1123 1127 private void linkToEditor(IStructuredSelection selection) { 1128 Object obj= selection.getFirstElement(); 1129 1130 if (selection.size() == 1) { 1131 IEditorPart part= EditorUtility.isOpenInEditor(obj); 1132 if (part != null) { 1133 IWorkbenchPage page= getSite().getPage(); 1134 page.bringToTop(part); 1135 if (obj instanceof IJavaElement) 1136 EditorUtility.revealInEditor(part, (IJavaElement) obj); 1137 } 1138 } 1139 } 1140 1141 void setSelectionFromEditor(IWorkbenchPart part) { 1142 if (!fProcessSelectionEvents || !linkBrowsingViewSelectionToEditor() || !(part instanceof IEditorPart)) 1143 return; 1144 1145 IWorkbenchPartSite site= part.getSite(); 1146 if (site == null) 1147 return; 1148 ISelectionProvider provider= site.getSelectionProvider(); 1149 if (provider != null) 1150 setSelectionFromEditor(part, provider.getSelection()); 1151 } 1152 1153 private void setSelectionFromEditor(IWorkbenchPart part, ISelection selection) { 1154 if (part instanceof IEditorPart) { 1155 IJavaElement element= null; 1156 if (selection instanceof IStructuredSelection) { 1157 Object obj= getSingleElementFromSelection(selection); 1158 if (obj instanceof IJavaElement) 1159 element= (IJavaElement)obj; 1160 } 1161 IEditorInput ei= ((IEditorPart)part).getEditorInput(); 1162 if (selection instanceof ITextSelection) { 1163 int offset= ((ITextSelection)selection).getOffset(); 1164 element= getElementAt(ei, offset); 1165 } 1166 if (element != null) { 1167 adjustInputAndSetSelection(element); 1168 return; 1169 } 1170 if (ei instanceof IFileEditorInput) { 1171 IFile file= ((IFileEditorInput)ei).getFile(); 1172 IJavaElement je= (IJavaElement)file.getAdapter(IJavaElement.class); 1173 if (je == null) { 1174 IContainer container= ((IFileEditorInput)ei).getFile().getParent(); 1175 if (container != null) 1176 je= (IJavaElement)container.getAdapter(IJavaElement.class); 1177 } 1178 if (je == null) { 1179 setSelection(null, false); 1180 return; 1181 } 1182 adjustInputAndSetSelection(je); 1183 } else if (ei instanceof IClassFileEditorInput) { 1184 IClassFile cf= ((IClassFileEditorInput)ei).getClassFile(); 1185 adjustInputAndSetSelection(cf); 1186 } 1187 } 1188 } 1189 1190 1195 Object getElementOfInput(IEditorInput input) { 1196 if (input instanceof IFileEditorInput) 1197 return ((IFileEditorInput)input).getFile(); 1198 if (input != null) 1199 return JavaUI.getEditorInputJavaElement(input); 1200 return null; 1201 } 1202 1203 void setSelection(ISelection selection, boolean reveal) { 1204 if (selection != null && selection.equals(fViewer.getSelection())) 1205 return; 1206 fProcessSelectionEvents= false; 1207 fViewer.setSelection(selection, reveal); 1208 fProcessSelectionEvents= true; 1209 } 1210 1211 1216 protected IJavaElement getElementAt(IEditorInput input, int offset) { 1217 if (input instanceof IClassFileEditorInput) { 1218 try { 1219 return ((IClassFileEditorInput)input).getClassFile().getElementAt(offset); 1220 } catch (JavaModelException ex) { 1221 return null; 1222 } 1223 } 1224 1225 IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); 1226 ICompilationUnit unit= manager.getWorkingCopy(input); 1227 if (unit != null) 1228 try { 1229 if (unit.isConsistent()) 1230 return unit.getElementAt(offset); 1231 else { 1232 1237 } 1238 } catch (JavaModelException ex) { 1239 } 1241 return null; 1242 } 1243 1244 protected IType getTypeForCU(ICompilationUnit cu) { 1245 IType primaryType= cu.findPrimaryType(); 1247 if (primaryType != null) 1248 return primaryType; 1249 1250 try { 1252 IType[] types= cu.getTypes(); 1253 if (types.length > 0) 1254 return types[0]; 1255 else 1256 return null; 1257 } catch (JavaModelException ex) { 1258 return null; 1259 } 1260 } 1261 1262 void setProcessSelectionEvents(boolean state) { 1263 fProcessSelectionEvents= state; 1264 } 1265 1266 1269 public Object getViewPartInput() { 1270 if (fViewer != null) { 1271 return fViewer.getInput(); 1272 } 1273 return null; 1274 } 1275 1276 protected void setActionGroups(CompositeActionGroup actionGroups) { 1277 fActionGroups= actionGroups; 1278 } 1279 1280 protected void setBuildActionGroup(BuildActionGroup actionGroup) { 1281 fBuildActionGroup= actionGroup; 1282 } 1283 1284 protected void setCCPActionGroup(CCPActionGroup actionGroup) { 1285 fCCPActionGroup= actionGroup; 1286 } 1287 1288 protected void setCustomFiltersActionGroup(CustomFiltersActionGroup customFiltersActionGroup) { 1289 fCustomFiltersActionGroup= customFiltersActionGroup; 1290 } 1291 1292 protected boolean hasCustomFilter() { 1293 return fHasCustomFilter; 1294 } 1295 1296 protected boolean hasWorkingSetFilter() { 1297 return fHasWorkingSetFilter; 1298 } 1299 1300 protected void setOpenEditorGroup(OpenEditorActionGroup openEditorGroup) { 1301 fOpenEditorGroup= openEditorGroup; 1302 } 1303 1304 protected OpenEditorActionGroup getOpenEditorGroup() { 1305 return fOpenEditorGroup; 1306 } 1307 1308 protected BuildActionGroup getBuildActionGroup() { 1309 return fBuildActionGroup; 1310 } 1311 1312 protected CCPActionGroup getCCPActionGroup() { 1313 return fCCPActionGroup; 1314 } 1315 1316 private boolean linkBrowsingViewSelectionToEditor() { 1317 return isLinkingEnabled(); 1318 } 1319 1320 public void setLinkingEnabled(boolean enabled) { 1321 fLinkingEnabled= enabled; 1322 PreferenceConstants.getPreferenceStore().setValue(getLinkToEditorKey(), enabled); 1323 if (enabled) { 1324 IEditorPart editor = getSite().getPage().getActiveEditor(); 1325 if (editor != null) { 1326 setSelectionFromEditor(editor); 1327 } 1328 } 1329 } 1330 1331} 1332 | Popular Tags |