1 11 12 package org.eclipse.team.internal.ui.history; 13 14 import java.util.*; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.jface.action.*; 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.dnd.*; 27 import org.eclipse.swt.widgets.*; 28 import org.eclipse.team.core.RepositoryProvider; 29 import org.eclipse.team.core.history.IFileHistoryProvider; 30 import org.eclipse.team.internal.ui.*; 31 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement; 32 import org.eclipse.team.ui.history.*; 33 import org.eclipse.ui.*; 34 import org.eclipse.ui.actions.ActionFactory; 35 import org.eclipse.ui.ide.ResourceUtil; 36 import org.eclipse.ui.part.*; 37 38 public class GenericHistoryView extends ViewPart implements IHistoryView, IPropertyChangeListener, IShowInTarget { 39 40 private static final String HISTORY_VIEW_GROUP = "org.eclipse.team.ui.historyView"; private static final String NAVIGATION_GROUP = "org.eclipse.team.ui.navigation"; private static final int MAX_NAVIGATION_HISTORY_ENTRIES = 15; 43 44 static boolean sameSource(IHistoryPageSource source1, IHistoryPageSource source2) { 45 return source1 == source2 || (source1 != null && source2 != null && source1.equals(source2)); 46 } 47 48 class PageContainer { 49 private Page page; 50 private SubActionBars subBars; 51 private final IHistoryPageSource source; 52 53 public PageContainer(Page page, IHistoryPageSource source) { 54 this.page = page; 55 this.source = source; 56 } 57 58 public Page getPage() { 59 return page; 60 } 61 62 public void setPage(Page page) { 63 this.page = page; 64 } 65 66 public SubActionBars getSubBars() { 67 return subBars; 68 } 69 70 public void setSubBars(SubActionBars subBars) { 71 this.subBars = subBars; 72 } 73 74 public IHistoryPageSource getSource() { 75 return source; 76 } 77 78 public boolean matches(Object object, IHistoryPageSource pageSource) { 79 if (page instanceof IHistoryPage) { 80 Object input = ((IHistoryPage)page).getInput(); 81 if (input != null) 82 return input.equals(object) && sameSource(getPageSourceFor(object, pageSource), getPageSourceFor(input, source)); 83 } 84 return false; 85 } 86 87 public boolean canShow(Object object, IHistoryPageSource pageSource) { 88 if (page instanceof IHistoryPage && sameSource(getPageSourceFor(object, pageSource), getPageSourceFor(((IHistoryPage)page).getInput(), source))) { 89 return ((IHistoryPage)page).isValidInput(object); 90 } 91 return false; 92 } 93 } 94 95 99 class NavigationHistory { 100 List history = new ArrayList(); 101 int position; 102 private boolean navigating; 103 public int size() { 104 return history.size(); 105 } 106 public void gotoPreviousEntry() { 107 if (position > 0) { 108 position--; 109 gotoEntry(); 110 } else { 111 position = history.size() - 1; 112 gotoEntry(); 113 } 114 } 115 private void gotoEntry() { 116 try { 117 navigating = true; 118 NavigationHistoryEntry currentEntry = getCurrentEntry(); 119 showHistoryPageFor(currentEntry.object, true, true, currentEntry.source); 120 } finally { 121 navigating = false; 122 } 123 } 124 private NavigationHistoryEntry getCurrentEntry() { 125 return (NavigationHistoryEntry)history.get(position); 126 } 127 public void addEntry(Object object, String name, IHistoryPageSource source) { 128 if (!navigating) { 129 NavigationHistoryEntry navigationHistoryEntry = new NavigationHistoryEntry(object, name, source); 130 if (history.contains(navigationHistoryEntry)) { 131 history.remove(navigationHistoryEntry); 132 } 133 history.add(navigationHistoryEntry); 134 if (history.size() > MAX_NAVIGATION_HISTORY_ENTRIES) { 135 history.remove(0); 136 } 137 position = history.size() - 1; 138 } 139 navigateAction.update(); 140 } 141 public NavigationHistoryEntry[] getEntries() { 142 return (NavigationHistoryEntry[]) history.toArray(new NavigationHistoryEntry[history.size()]); 143 } 144 private NavigationHistoryEntry getEntry(int i) { 145 return (NavigationHistoryEntry)history.get(i); 146 } 147 public void gotoEntry(NavigationHistoryEntry navigationHistoryEntry) { 148 position = history.indexOf(navigationHistoryEntry); 149 gotoEntry(); 150 } 151 public NavigationHistoryEntry getPreviousEntry() { 152 int next = position - 1; 153 if (next < 0) 154 next = size() - 1; 155 return getEntry(next); 156 } 157 158 public void updateName(IHistoryPage historyPage, 159 IHistoryPageSource pageSource) { 160 NavigationHistoryEntry[] historyEntries = getEntries(); 161 for (int i = 0; i < historyEntries.length; i++) { 162 NavigationHistoryEntry historyEntry = historyEntries[i]; 163 if (historyEntry.matches(historyPage, pageSource)) 164 historyEntry.name = historyPage.getName(); 165 } 166 navigateAction.update(); 167 } 168 } 169 170 static class NavigationHistoryEntry { 171 Object object; 172 String name; 173 IHistoryPageSource source; 174 public NavigationHistoryEntry(Object object, String name, IHistoryPageSource source) { 175 this.object = object; 176 this.name = name; 177 this.source = source; 178 } 179 public boolean equals(Object obj) { 180 if (obj instanceof NavigationHistoryEntry) { 181 NavigationHistoryEntry other = (NavigationHistoryEntry) obj; 182 return other.object.equals(this.object) && sameSource(source, other.source); 183 } 184 return false; 185 } 186 public boolean matches(IHistoryPage historyPage, 187 IHistoryPageSource pageSource) { 188 return object.equals(historyPage.getInput()) 189 && sameSource(source, pageSource); 190 } 191 public int hashCode() { 192 return object.hashCode(); 193 } 194 } 195 196 abstract class MenuCreator implements IMenuCreator { 197 private MenuManager menuManager; 198 public void dispose() { 199 if(menuManager != null) { 200 menuManager.dispose(); 201 menuManager = null; 202 } 203 } 204 public Menu getMenu(Control parent) { 205 Menu fMenu = null; 206 if (menuManager == null) { 207 menuManager = new MenuManager(); 208 fMenu = menuManager.createContextMenu(parent); 209 IAction[] actions = getDropDownActions(); 210 for (int i = actions.length - 1; i >= 0 ; i--) { 211 IAction action = actions[i]; 212 menuManager.add(action); 213 } 214 updateMenuState(); 215 } else { 216 fMenu = menuManager.getMenu(); 217 } 218 return fMenu; 219 } 220 protected void updateMenuState() { 221 if (menuManager != null) 222 menuManager.update(true); 223 } 224 225 protected abstract IAction[] getDropDownActions(); 226 227 public Menu getMenu(Menu parent) { 228 return null; 229 } 230 231 public void rebuildMenu() { 232 if(menuManager != null) { 233 menuManager.dispose(); 234 menuManager = null; 235 } 236 } 237 } 238 239 class NavigationHistoryAction extends Action { 240 private MenuCreator menuCreator; 241 private IAction[] actions; 242 243 public NavigationHistoryAction() { 244 menuCreator = new MenuCreator() { 245 protected IAction[] getDropDownActions() { 246 return getActions(); 247 } 248 }; 249 setMenuCreator(menuCreator); 250 update(); 251 } 252 private IAction[] createActions() { 253 NavigationHistoryEntry[] entries = getDropDownEntries(); 254 List actions = new ArrayList(); 255 for (int i = 0; i < entries.length; i++) { 256 NavigationHistoryEntry navigationHistoryEntry = entries[i]; 257 actions.add(new NavigationHistoryEntryAction(navigationHistoryEntry)); 258 } 259 return (IAction[]) actions.toArray(new IAction[actions.size()]); 260 } 261 protected NavigationHistoryEntry[] getDropDownEntries() { 262 return navigationHistory.getEntries(); 263 } 264 public void run() { 265 navigationHistory.gotoPreviousEntry(); 266 updateCheckState(); 267 } 268 public void update() { 269 setEnabled(navigationHistory.size() > 1); 270 if (isEnabled()) { 271 setToolTipText(NLS.bind(TeamUIMessages.GenericHistoryView_1, navigationHistory.getPreviousEntry().name)); 272 } else { 273 setToolTipText(TeamUIMessages.GenericHistoryView_2); 274 } 275 actions = null; 276 menuCreator.rebuildMenu(); 277 updateCheckState(); 278 } 279 280 private void updateCheckState() { 281 IAction[] actions = getActions(); 282 for (int i = 0; i < actions.length; i++) { 283 IAction action = actions[i]; 284 if (action instanceof NavigationHistoryEntryAction) { 285 NavigationHistoryEntryAction a = (NavigationHistoryEntryAction) action; 286 a.update(); 287 } 288 } 289 menuCreator.updateMenuState(); 290 } 291 public void dispose() { 292 menuCreator.dispose(); 293 } 294 private IAction[] getActions() { 295 if (actions == null) 296 actions = createActions(); 297 return actions; 298 } 299 } 300 301 class NavigationHistoryEntryAction extends Action { 302 303 private final NavigationHistoryEntry navigationHistoryEntry; 304 305 public NavigationHistoryEntryAction(NavigationHistoryEntry navigationHistoryEntry) { 306 super(navigationHistoryEntry.name); 307 this.navigationHistoryEntry = navigationHistoryEntry; 308 } 309 310 public void run() { 311 navigationHistory.gotoEntry(navigationHistoryEntry); 312 navigateAction.updateCheckState(); 313 } 314 315 public void update() { 316 setChecked(navigationHistory.getCurrentEntry() == navigationHistoryEntry); 317 } 318 319 } 320 321 324 private PageBook book; 325 326 329 private Action refreshAction; 330 private Action linkWithEditorAction; 331 private Action pinAction; 332 private NavigationHistoryAction navigateAction; 333 334 337 private PageContainer defaultPageContainer; 338 339 342 PageContainer currentPageContainer; 343 344 347 DropTarget dropTarget; 348 GenericHistoryDropAdapter dropAdapter; 349 350 NavigationHistory navigationHistory = new NavigationHistory(); 351 352 355 private Object lastSelectedElement; 356 357 private IPartListener partListener = new IPartListener() { 358 public void partActivated(IWorkbenchPart part) { 359 if (part instanceof IEditorPart) 360 editorActivated((IEditorPart) part); 361 } 362 363 public void partBroughtToTop(IWorkbenchPart part) { 364 if (part == GenericHistoryView.this) 365 editorActivated(getViewSite().getPage().getActiveEditor()); 366 } 367 368 public void partOpened(IWorkbenchPart part) { 369 if (part == GenericHistoryView.this) 370 editorActivated(getViewSite().getPage().getActiveEditor()); 371 } 372 373 public void partClosed(IWorkbenchPart part) { 374 } 375 376 public void partDeactivated(IWorkbenchPart part) { 377 } 378 }; 379 380 private IPartListener2 partListener2 = new IPartListener2() { 381 public void partActivated(IWorkbenchPartReference ref) { 382 } 383 384 public void partBroughtToTop(IWorkbenchPartReference ref) { 385 } 386 387 public void partClosed(IWorkbenchPartReference ref) { 388 } 389 390 public void partDeactivated(IWorkbenchPartReference ref) { 391 } 392 393 public void partOpened(IWorkbenchPartReference ref) { 394 } 395 396 public void partHidden(IWorkbenchPartReference ref) { 397 } 398 399 public void partVisible(IWorkbenchPartReference ref) { 400 if (ref.getPart(true) == GenericHistoryView.this) 401 editorActivated(getViewSite().getPage().getActiveEditor()); 402 } 403 404 public void partInputChanged(IWorkbenchPartReference ref) { 405 } 406 }; 407 408 private ISelectionListener selectionListener = new ISelectionListener() { 409 410 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 411 412 if (selection instanceof IStructuredSelection) { 413 IStructuredSelection structSelection = (IStructuredSelection) selection; 414 lastSelectedElement = structSelection.getFirstElement(); 417 418 if (!isLinkingEnabled() || !checkIfPageIsVisible()) { 419 return; 420 } 421 422 if (lastSelectedElement != null){ 423 Object resource; 424 if (lastSelectedElement instanceof SyncInfoModelElement) { 425 SyncInfoModelElement syncInfoModelElement = (SyncInfoModelElement) lastSelectedElement; 426 resource = syncInfoModelElement.getSyncInfo().getLocal(); 427 } else { 428 resource = Utils.getAdapter(lastSelectedElement, IResource.class); 429 } 430 if (resource != null) 431 showHistory((IResource) resource); 432 else 433 showHistory(lastSelectedElement); 434 lastSelectedElement = null; 436 } 437 } 438 } 439 440 }; 441 442 private boolean linkingEnabled; 443 444 private boolean viewPinned; 445 446 449 void refreshGlobalActionHandlers() { 450 IActionBars bars = getViewSite().getActionBars(); 452 bars.clearGlobalActionHandlers(); 453 454 Map newActionHandlers = currentPageContainer.getSubBars().getGlobalActionHandlers(); 456 if (newActionHandlers != null) { 457 Set keys = newActionHandlers.entrySet(); 458 Iterator iter = keys.iterator(); 459 while (iter.hasNext()) { 460 Map.Entry entry = (Map.Entry) iter.next(); 461 bars.setGlobalActionHandler((String ) entry.getKey(), (IAction) entry.getValue()); 462 } 463 } 464 465 bars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction); 467 468 } 469 470 public void createPartControl(Composite parent) { 471 book = new PageBook(parent, SWT.NONE); 473 474 this.linkingEnabled = TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IFileHistoryConstants.PREF_GENERIC_HISTORYVIEW_EDITOR_LINKING); 475 476 defaultPageContainer = createDefaultPage(book); 478 479 configureToolbars(getViewSite().getActionBars()); 481 482 getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction); 484 485 initDragAndDrop(); 487 488 showPageRec(defaultPageContainer); 490 491 getSite().getPage().addPartListener(partListener); 494 getSite().getPage().addPartListener(partListener2); 495 496 getSite().getPage().addPostSelectionListener(selectionListener); 498 } 499 500 private void configureToolbars(IActionBars actionBars) { 501 502 pinAction = new Action(TeamUIMessages.GenericHistoryView_PinCurrentHistory, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PINNED)) { 503 public void run() { 504 if (isChecked()) { 505 linkWithEditorAction.setChecked(false); 507 setLinkingEnabled(false); 508 } 509 setViewPinned(isChecked()); 510 } 511 }; 512 pinAction.setChecked(isViewPinned()); 513 pinAction.setToolTipText(TeamUIMessages.GenericHistoryView_0); 514 515 refreshAction = new Action(TeamUIMessages.GenericHistoryView_Refresh, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_REFRESH)) { 516 public void run() { 517 ((IHistoryPage) currentPageContainer.getPage()).refresh(); 518 } 519 }; 520 refreshAction.setToolTipText(TeamUIMessages.GenericHistoryView_RefreshTooltip); 521 refreshAction.setEnabled(true); 522 523 524 linkWithEditorAction = new Action(TeamUIMessages.GenericHistoryView_LinkWithEditor, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_LINK_WITH)) { 525 public void run() { 526 if (isChecked()) { 527 pinAction.setChecked(false); 529 setViewPinned(false); 530 } 531 setLinkingEnabled(isViewPinned() ? false : isChecked()); 532 } 533 }; 534 linkWithEditorAction.setChecked(isLinkingEnabled()); 535 linkWithEditorAction.setToolTipText(TeamUIMessages.GenericHistoryView_LinkWithTooltip); 536 537 navigateAction = new NavigationHistoryAction(); 538 Utils.initAction(navigateAction, "action.previousHistory."); 540 IToolBarManager tbm = actionBars.getToolBarManager(); 542 tbm.add(new Separator(HISTORY_VIEW_GROUP)); 543 tbm.appendToGroup(HISTORY_VIEW_GROUP, refreshAction); 544 tbm.appendToGroup(HISTORY_VIEW_GROUP, linkWithEditorAction); 545 tbm.appendToGroup(HISTORY_VIEW_GROUP, pinAction); 546 tbm.add(new Separator(NAVIGATION_GROUP)); 547 tbm.appendToGroup(NAVIGATION_GROUP, navigateAction); 548 tbm.update(false); 549 } 550 551 boolean isLinkingEnabled() { 552 return linkingEnabled; 553 } 554 555 559 public void setLinkingEnabled(boolean enabled) { 560 this.linkingEnabled = enabled; 561 562 TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IFileHistoryConstants.PREF_GENERIC_HISTORYVIEW_EDITOR_LINKING, enabled); 564 565 if (enabled) { 567 editorActivated(getSite().getPage().getActiveEditor()); 568 } 569 } 570 571 575 void setViewPinned(boolean pinned) { 576 this.viewPinned = pinned; 577 } 578 579 582 void initDragAndDrop() { 583 int ops = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; 584 Transfer[] transfers = new Transfer[] {ResourceTransfer.getInstance(), PluginTransfer.getInstance()}; 585 586 dropTarget = new DropTarget(book, ops); 587 dropTarget.setTransfer(transfers); 588 dropAdapter = new GenericHistoryDropAdapter(this); 589 dropTarget.addDropListener(dropAdapter); 590 } 591 592 public void setFocus() { 593 if (isLinkingEnabled()){ 594 if (lastSelectedElement != null){ 595 if (lastSelectedElement instanceof IEditorPart){ 596 editorActivated((IEditorPart) lastSelectedElement); 597 } else { 598 Object resource = Utils.getAdapter(lastSelectedElement, IResource.class); 599 if (resource != null) 600 showHistoryPageFor((IResource) resource, false, false, null); 601 else 602 showHistoryPageFor(lastSelectedElement, false, false, null); 603 } 604 lastSelectedElement = null; 606 } 607 } 608 609 if (currentPageContainer.page instanceof IPage){ 610 ((IPage) currentPageContainer.page).setFocus(); 611 } 612 } 613 614 625 protected void showPageRec(PageContainer pageContainer) { 626 if (currentPageContainer == pageContainer) { 628 addNavigationHistoryEntry(); 629 return; 630 } 631 632 if (currentPageContainer != null) { 634 currentPageContainer.getSubBars().deactivate(); 635 ((IHistoryPage)currentPageContainer.getPage()).removePropertyChangeListener(this); 637 currentPageContainer.getPage().dispose(); 638 currentPageContainer.getSubBars().dispose(); 639 } 640 currentPageContainer = pageContainer; 642 643 Control pageControl = currentPageContainer.getPage().getControl(); 644 if (pageControl != null && !pageControl.isDisposed()) { 645 book.showPage(pageControl); 648 ((IHistoryPage)currentPageContainer.getPage()).addPropertyChangeListener(this); 649 currentPageContainer.getSubBars().activate(); 650 refreshGlobalActionHandlers(); 651 getViewSite().getActionBars().updateActionBars(); 653 addNavigationHistoryEntry(); 654 } 655 } 656 657 private void addNavigationHistoryEntry() { 658 if (currentPageContainer != null) { 659 Object input = ((IHistoryPage)currentPageContainer.getPage()).getInput(); 660 if (input != null) 661 navigationHistory.addEntry(input, ((IHistoryPage)currentPageContainer.getPage()).getName(), currentPageContainer.getSource()); 662 } 663 } 664 665 677 protected PageSite initPage(IPageBookViewPage page) { 678 try { 679 PageSite site = new PageSite(getViewSite()); 680 page.init(site); 681 return site; 682 } catch (PartInitException e) { 683 TeamUIPlugin.log(e); 684 } 685 return null; 686 } 687 688 public IHistoryPage showHistoryFor(Object object, boolean force) { 689 return showHistoryPageFor(object, true, force, null); 690 } 691 692 public IHistoryPage showHistoryPageFor(Object object, boolean refresh, boolean force, IHistoryPageSource pageSource) { 693 694 if (!this.getSite().getPage().isPartVisible(this)) 697 return null; 698 699 pageSource = getPageSourceFor(object, pageSource); 701 if (pageSource == null || !pageSource.canShowHistoryFor(object)) 702 return null; 703 704 IHistoryPage existingPage = checkForExistingPage(object, refresh, force, pageSource); 706 if (existingPage != null){ 707 return existingPage; 708 } 709 710 if (isViewPinned() && !force) { 712 return handlePinnedView(object, refresh, pageSource); 713 } 714 715 PageContainer tempPageContainer = null; 718 if (currentPageContainer!= null && currentPageContainer.canShow(object, pageSource)) { 719 tempPageContainer = currentPageContainer; 720 } else { 721 tempPageContainer = createPage(pageSource, object); 722 } 723 724 IHistoryPage historyPage = ((IHistoryPage)tempPageContainer.getPage()); 726 historyPage.setInput(object); 727 ((HistoryPage)historyPage).setHistoryView(this); 728 setContentDescription(historyPage.getName()); 729 showPageRec(tempPageContainer); 730 return historyPage; 731 } 732 733 private IHistoryPageSource getPageSourceFor(Object object, IHistoryPageSource pageSource) { 734 if (object == null || pageSource != null) 735 return pageSource; 736 IResource resource = Utils.getResource(object); 737 if (resource == null) { 738 return (IHistoryPageSource) Utils.getAdapter(object, IHistoryPageSource.class); 739 } else { 740 if (resource.getProject() == null) 741 return null; 742 RepositoryProvider teamProvider = RepositoryProvider.getProvider(resource.getProject()); 744 if (teamProvider == null){ 745 return LocalHistoryPageSource.getInstance(); 747 } else { 748 IFileHistoryProvider fileHistory = teamProvider.getFileHistoryProvider(); 749 750 if (fileHistory != null) { 751 IHistoryPageSource source = (IHistoryPageSource)Utils.getAdapter(fileHistory, IHistoryPageSource.class,true); 752 if (source != null) 753 return source; 754 } 755 return (IHistoryPageSource)Utils.getAdapter(teamProvider, IHistoryPageSource.class,true); 756 } 757 } 758 } 759 760 private IHistoryPage handlePinnedView(Object object, boolean refresh, IHistoryPageSource source) { 761 try { 762 GenericHistoryView historyView = findUnpinnedHistoryView(); 764 if (historyView != null){ 765 getSite().getPage().activate(historyView); 766 return historyView.showHistoryPageFor(object, refresh, true, source); 767 } 768 String id = VIEW_ID + System.currentTimeMillis(); 770 IViewPart view = getSite().getPage().showView(VIEW_ID, id, IWorkbenchPage.VIEW_CREATE); 771 getSite().getPage().activate(view); 772 if (view instanceof GenericHistoryView) 773 return ((GenericHistoryView) view).showHistoryPageFor(object, refresh, true, source); 774 775 } catch (PartInitException e) { 776 } 777 return null; 778 } 779 780 private IHistoryPage checkForExistingPage(Object object, boolean refresh, boolean thisViewOnly, IHistoryPageSource pageSource) { 781 IHistoryPage tempPage = checkForExistingPage(object, refresh, pageSource); 783 if (tempPage != null || thisViewOnly) 784 return tempPage; 785 tempPage = searchHistoryViewsForObject(object, refresh, pageSource); 786 if (tempPage != null) 787 getSite().getPage().bringToTop((IWorkbenchPart)tempPage.getHistoryView()); 788 return tempPage; 789 } 790 791 private IHistoryPage checkForExistingPage(Object object, boolean refresh, IHistoryPageSource pageSource) { 792 if (currentPageContainer != null) { 794 if (currentPageContainer.matches(object, pageSource)){ 795 IHistoryPage tempPage =((IHistoryPage)currentPageContainer.getPage()); 797 if (refresh) 798 tempPage.refresh(); 799 800 return tempPage; 801 } 802 } 803 return null; 804 } 805 806 private IHistoryPage searchHistoryViewsForObject(Object object, boolean refresh, IHistoryPageSource pageSource) { 807 IWorkbenchPage page = getSite().getPage(); 808 IViewReference[] historyViews = page.getViewReferences(); 809 for (int i = 0; i < historyViews.length; i++) { 810 if (historyViews[i].getId().equals(VIEW_ID)){ 811 IViewPart historyView = historyViews[i].getView(true); 812 if (historyView instanceof GenericHistoryView) { 813 GenericHistoryView ghv = (GenericHistoryView)historyView; 814 IHistoryPage historyPage = ghv.checkForExistingPage(object, refresh, pageSource); 815 if (historyPage != null) { 816 return historyPage; 817 } 818 } 819 } 820 } 821 return null; 822 } 823 824 public GenericHistoryView findUnpinnedHistoryView(){ 825 IWorkbenchPage page = getSite().getPage(); 826 IViewReference[] historyViews = page.getViewReferences(); 827 for (int i = 0; i < historyViews.length; i++) { 828 if (historyViews[i].getId().equals(VIEW_ID)){ 829 IViewPart historyView = historyViews[i].getView(false); 830 if (!((GenericHistoryView)historyView).isViewPinned()) 831 return (GenericHistoryView) historyView; 832 } 833 } 834 return null; 835 } 836 837 boolean isViewPinned() { 838 return viewPinned; 839 } 840 841 private PageContainer createPage(IHistoryPageSource source, Object object) { 842 Page page = source.createPage(object); 843 PageSite site = initPage(page); 844 ((IHistoryPage) page).setSite(new WorkbenchHistoryPageSite(this, page.getSite())); 845 page.createControl(book); 846 PageContainer container = new PageContainer(page, source); 847 container.setSubBars((SubActionBars) site.getActionBars()); 848 return container; 849 } 850 851 protected PageContainer createDefaultPage(PageBook book) { 852 GenericHistoryViewDefaultPage page = new GenericHistoryViewDefaultPage(); 853 PageSite site = initPage(page); 854 page.createControl(book); 855 PageContainer container = new PageContainer(page, null); 856 container.setSubBars((SubActionBars) site.getActionBars()); 857 return container; 858 } 859 860 866 protected void editorActivated(IEditorPart editor) { 867 if (editor != null && !checkIfPageIsVisible()) 870 lastSelectedElement = editor; 871 872 if (editor == null || !isLinkingEnabled() || !checkIfPageIsVisible() || isViewPinned()) { 874 return; 875 } 876 IEditorInput input = editor.getEditorInput(); 877 878 IFile file = ResourceUtil.getFile(input); 879 if (file != null) { 880 showHistory(file); 881 } else { 882 Object pageSource = Utils.getAdapter(input, IHistoryPageSource.class); 884 if (pageSource != null) 885 showHistory(input); 886 } 887 } 888 889 private boolean checkIfPageIsVisible() { 890 return getViewSite().getPage().isPartVisible(this); 891 } 892 893 public void dispose() { 894 super.dispose(); 895 if (dropTarget != null && !dropTarget.isDisposed()) 897 dropTarget.removeDropListener(dropAdapter); 898 ((IHistoryPage)currentPageContainer.getPage()).removePropertyChangeListener(this); 900 currentPageContainer.getPage().dispose(); 901 defaultPageContainer.getPage().dispose(); 902 currentPageContainer = null; 903 defaultPageContainer = null; 904 getSite().getPage().removePartListener(partListener); 906 getSite().getPage().removePartListener(partListener2); 907 getSite().getPage().removePostSelectionListener(selectionListener); 909 navigateAction.dispose(); 910 } 911 912 public IHistoryPage showHistoryFor(Object object) { 913 return showHistoryFor(object, false); 914 } 915 916 public IHistoryPage getHistoryPage() { 917 if (currentPageContainer != null && 918 currentPageContainer.getPage() != null) 919 return (IHistoryPage) currentPageContainer.getPage(); 920 921 return (IHistoryPage) defaultPageContainer.getPage(); 922 } 923 924 927 public void propertyChange(PropertyChangeEvent event) { 928 if (event.getSource() == currentPageContainer.getPage()) { 929 if (event.getProperty().equals(IHistoryPage.P_NAME)) { 930 Display.getDefault().asyncExec(new Runnable () { 931 public void run() { 932 IHistoryPage historyPage = (IHistoryPage) currentPageContainer.getPage(); 933 setContentDescription(historyPage.getName()); 934 navigationHistory.updateName(historyPage, currentPageContainer.getSource()); 935 } 936 }); 937 } else if (event.getProperty().equals(IHistoryPage.P_DESCRIPTION)) { 938 } 940 } 941 } 942 943 public IHistoryView findAppropriateHistoryViewFor(Object input, 944 IHistoryPageSource pageSource) { 945 IHistoryPage page = searchHistoryViewsForObject(input, false, pageSource); 947 if (page != null) { 948 return page.getHistoryView(); 949 } 950 return findUnpinnedHistoryView(); 951 } 952 953 private void showHistory(Object object) { 954 if (getHistoryPage().getInput() != object) 957 showHistoryPageFor(object, false, false, null); 958 } 959 960 public boolean show(ShowInContext context) { 961 ISelection selection = context.getSelection(); 962 if (selection instanceof IStructuredSelection) { 963 IStructuredSelection ss = (IStructuredSelection) selection; 964 if (ss.size() == 1) { 965 if ((showHistoryFor(ss.getFirstElement()) != null)) 968 return true; 969 } 970 } 971 if (context.getInput() != null) { 972 return (showHistoryFor(context.getInput()) != null); 973 } 974 return false; 975 } 976 } 977 | Popular Tags |