1 11 12 package org.eclipse.ui.part; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.commands.common.EventManager; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 import org.eclipse.jface.util.SafeRunnable; 25 import org.eclipse.jface.viewers.IPostSelectionProvider; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.ISelectionChangedListener; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.SelectionChangedEvent; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.ui.IActionBars; 35 import org.eclipse.ui.IPartListener; 36 import org.eclipse.ui.IViewSite; 37 import org.eclipse.ui.IWorkbenchPart; 38 import org.eclipse.ui.PartInitException; 39 import org.eclipse.ui.SubActionBars; 40 import org.eclipse.ui.internal.WorkbenchPlugin; 41 import org.eclipse.ui.internal.util.Util; 42 43 103 public abstract class PageBookView extends ViewPart implements IPartListener { 104 107 private PageBook book; 108 109 112 private PageRec defaultPageRec; 113 114 118 private Map mapPartToRec = new HashMap (); 119 120 124 private Map mapPageToSite = new HashMap (); 125 126 129 private Map mapPageToNumRecs = new HashMap (); 130 131 134 private PageRec activeRec; 135 136 139 private IPropertyChangeListener actionBarPropListener = new IPropertyChangeListener() { 140 public void propertyChange(PropertyChangeEvent event) { 141 if (event.getProperty().equals(SubActionBars.P_ACTION_HANDLERS) 142 && activeRec != null 143 && event.getSource() == activeRec.subActionBars) { 144 refreshGlobalActionHandlers(); 145 } 146 } 147 }; 148 149 152 private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() { 153 public void selectionChanged(SelectionChangedEvent event) { 154 pageSelectionChanged(event); 155 } 156 }; 157 158 161 private ISelectionChangedListener postSelectionListener = new ISelectionChangedListener() { 162 public void selectionChanged(SelectionChangedEvent event) { 163 postSelectionChanged(event); 164 } 165 }; 166 167 170 private SelectionProvider selectionProvider = new SelectionProvider(); 171 172 176 protected static class PageRec { 177 178 181 public IWorkbenchPart part; 182 183 186 public IPage page; 187 188 191 public SubActionBars subActionBars; 192 193 199 public PageRec(IWorkbenchPart part, IPage page) { 200 this.part = part; 201 this.page = page; 202 } 203 204 207 public void dispose() { 208 part = null; 209 page = null; 210 } 211 } 212 213 private static class SelectionManager extends EventManager { 214 219 public void addSelectionChangedListener( 220 ISelectionChangedListener listener) { 221 addListenerObject(listener); 222 } 223 224 229 public void removeSelectionChangedListener( 230 ISelectionChangedListener listener) { 231 removeListenerObject(listener); 232 } 233 234 239 public void selectionChanged(final SelectionChangedEvent event) { 240 Object [] listeners = getListeners(); 242 for (int i = 0; i < listeners.length; ++i) { 243 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; 244 Platform.run(new SafeRunnable() { 245 public void run() { 246 l.selectionChanged(event); 247 } 248 }); 249 } 250 } 251 252 } 253 254 258 protected class SelectionProvider implements IPostSelectionProvider { 259 260 private SelectionManager fSelectionListener = new SelectionManager(); 261 262 private SelectionManager fPostSelectionListeners = new SelectionManager(); 263 264 267 public void addSelectionChangedListener( 268 ISelectionChangedListener listener) { 269 fSelectionListener.addSelectionChangedListener(listener); 270 } 271 272 275 public ISelection getSelection() { 276 IPage currentPage = getCurrentPage(); 278 if (currentPage == null) { 281 return StructuredSelection.EMPTY; 282 } 283 IPageSite site = getPageSite(currentPage); 284 if (site == null) { 285 return StructuredSelection.EMPTY; 286 } 287 ISelectionProvider selProvider = site.getSelectionProvider(); 288 if (selProvider != null) { 289 return selProvider.getSelection(); 290 } 291 return StructuredSelection.EMPTY; 292 } 293 294 297 public void removeSelectionChangedListener( 298 ISelectionChangedListener listener) { 299 fSelectionListener.removeSelectionChangedListener(listener); 300 } 301 302 309 public void selectionChanged(final SelectionChangedEvent event) { 310 fSelectionListener.selectionChanged(event); 311 } 312 313 319 public void postSelectionChanged(final SelectionChangedEvent event) { 320 fPostSelectionListeners.selectionChanged(event); 321 } 322 323 326 public void setSelection(ISelection selection) { 327 IPage currentPage = getCurrentPage(); 329 if (currentPage == null) { 332 return; 333 } 334 IPageSite site = getPageSite(currentPage); 335 if (site == null) { 336 return; 337 } 338 ISelectionProvider selProvider = site.getSelectionProvider(); 339 if (selProvider != null) { 341 selProvider.setSelection(selection); 342 } 343 } 344 345 350 public void addPostSelectionChangedListener( 351 ISelectionChangedListener listener) { 352 fPostSelectionListeners.addSelectionChangedListener(listener); 353 } 354 355 360 public void removePostSelectionChangedListener( 361 ISelectionChangedListener listener) { 362 fPostSelectionListeners.removeSelectionChangedListener(listener); 363 } 364 } 365 366 369 protected PageBookView() { 370 super(); 371 } 372 373 388 protected abstract IPage createDefaultPage(PageBook book); 389 390 398 private PageRec createPage(IWorkbenchPart part) { 399 PageRec rec = doCreatePage(part); 400 if (rec != null) { 401 mapPartToRec.put(part, rec); 402 preparePage(rec); 403 } 404 return rec; 405 } 406 407 412 private void preparePage(PageRec rec) { 413 IPageSite site = null; 414 Integer count; 415 416 if (!doesPageExist(rec.page)) { 417 if (rec.page instanceof IPageBookViewPage) { 418 site = ((IPageBookViewPage) rec.page).getSite(); 419 } 420 if (site == null) { 421 site = new PageSite(getViewSite()); 423 } 424 mapPageToSite.put(rec.page, site); 425 426 rec.subActionBars = (SubActionBars) site.getActionBars(); 427 rec.subActionBars.addPropertyChangeListener(actionBarPropListener); 428 rec.page.setActionBars(rec.subActionBars); 430 431 count = new Integer (0); 432 } else { 433 site = (IPageSite) mapPageToSite.get(rec.page); 434 rec.subActionBars = (SubActionBars) site.getActionBars(); 435 count = ((Integer ) mapPageToNumRecs.get(rec.page)); 436 } 437 438 mapPageToNumRecs.put(rec.page, new Integer (count.intValue() + 1)); 439 } 440 441 454 protected void initPage(IPageBookViewPage page) { 455 try { 456 page.init(new PageSite(getViewSite())); 457 } catch (PartInitException e) { 458 WorkbenchPlugin.log(getClass(), "initPage", e); } 460 } 461 462 467 public void createPartControl(Composite parent) { 468 469 book = new PageBook(parent, SWT.NONE); 471 472 IPage defaultPage = createDefaultPage(book); 474 defaultPageRec = new PageRec(null, defaultPage); 475 preparePage(defaultPageRec); 476 477 showPageRec(defaultPageRec); 479 480 getSite().getPage().addPartListener(this); 482 showBootstrapPart(); 483 } 484 485 490 public void dispose() { 491 getSite().getPage().removePartListener(this); 493 494 activeRec = null; 496 if (defaultPageRec != null) { 497 defaultPageRec.page.dispose(); 500 defaultPageRec = null; 501 } 502 Map clone = (Map ) ((HashMap ) mapPartToRec).clone(); 503 Iterator itr = clone.values().iterator(); 504 while (itr.hasNext()) { 505 PageRec rec = (PageRec) itr.next(); 506 removePage(rec); 507 } 508 509 super.dispose(); 511 } 512 513 531 protected abstract PageRec doCreatePage(IWorkbenchPart part); 532 533 546 protected abstract void doDestroyPage(IWorkbenchPart part, 547 PageRec pageRecord); 548 549 556 protected boolean doesPageExist(IPage page) { 557 return mapPageToNumRecs.containsKey(page); 558 } 559 560 565 public Object getAdapter(Class key) { 566 IPage page = getCurrentPage(); 568 Object adapter = Util.getAdapter(page, key); 569 if (adapter != null) { 570 return adapter; 571 } 572 adapter = getViewAdapter(key); 575 if (adapter != null) { 576 return adapter; 577 } 578 return super.getAdapter(key); 580 } 581 582 597 protected Object getViewAdapter(Class adapter) { 598 return null; 599 } 600 601 618 protected abstract IWorkbenchPart getBootstrapPart(); 619 620 626 protected IWorkbenchPart getCurrentContributingPart() { 627 if (activeRec == null) { 628 return null; 629 } 630 return activeRec.part; 631 } 632 633 639 public IPage getCurrentPage() { 640 if (activeRec == null) { 641 return null; 642 } 643 return activeRec.page; 644 } 645 646 653 protected PageSite getPageSite(IPage page) { 654 return (PageSite) mapPageToSite.get(page); 655 } 656 657 662 public IPage getDefaultPage() { 663 return defaultPageRec.page; 664 } 665 666 671 protected PageBook getPageBook() { 672 return book; 673 } 674 675 683 protected PageRec getPageRec(IWorkbenchPart part) { 684 return (PageRec) mapPartToRec.get(part); 685 } 686 687 695 protected PageRec getPageRec(IPage page) { 696 Iterator itr = mapPartToRec.values().iterator(); 697 while (itr.hasNext()) { 698 PageRec rec = (PageRec) itr.next(); 699 if (rec.page == page) { 700 return rec; 701 } 702 } 703 return null; 704 } 705 706 717 protected abstract boolean isImportant(IWorkbenchPart part); 718 719 722 public void init(IViewSite site) throws PartInitException { 723 site.setSelectionProvider(selectionProvider); 724 super.init(site); 725 } 726 727 732 public void partActivated(IWorkbenchPart part) { 733 if (!isImportant(part)) { 735 return; 736 } 737 738 PageRec rec = getPageRec(part); 740 if (rec == null) { 741 rec = createPage(part); 742 } 743 744 if (rec != null) { 746 showPageRec(rec); 747 } else { 748 showPageRec(defaultPageRec); 749 } 750 } 751 752 756 public void partBroughtToTop(IWorkbenchPart part) { 757 } 759 760 765 public void partClosed(IWorkbenchPart part) { 766 if (activeRec != null && activeRec.part == part) { 768 showPageRec(defaultPageRec); 769 } 770 771 PageRec rec = getPageRec(part); 773 if (rec != null) { 774 removePage(rec); 775 } 776 } 777 778 782 public void partDeactivated(IWorkbenchPart part) { 783 } 785 786 791 public void partOpened(IWorkbenchPart part) { 792 } 794 795 798 private void refreshGlobalActionHandlers() { 799 IActionBars bars = getViewSite().getActionBars(); 801 bars.clearGlobalActionHandlers(); 802 803 Map newActionHandlers = activeRec.subActionBars 805 .getGlobalActionHandlers(); 806 if (newActionHandlers != null) { 807 Set keys = newActionHandlers.entrySet(); 808 Iterator iter = keys.iterator(); 809 while (iter.hasNext()) { 810 Map.Entry entry = (Map.Entry ) iter.next(); 811 bars.setGlobalActionHandler((String ) entry.getKey(), 812 (IAction) entry.getValue()); 813 } 814 } 815 } 816 817 823 private void removePage(PageRec rec) { 824 mapPartToRec.remove(rec.part); 825 826 int newCount = ((Integer ) mapPageToNumRecs.get(rec.page)).intValue() - 1; 827 828 if (newCount == 0) { 829 Object site = mapPageToSite.remove(rec.page); 830 mapPageToNumRecs.remove(rec.page); 831 832 if (rec.subActionBars != null) { 833 rec.subActionBars.dispose(); 834 } 835 836 Control control = rec.page.getControl(); 837 if (control != null && !control.isDisposed()) { 838 control.dispose(); 845 } 846 847 if (site instanceof PageSite) { 848 ((PageSite) site).dispose(); 849 } 850 851 doDestroyPage(rec.part, rec); 853 } else { 854 mapPageToNumRecs.put(rec.page, new Integer (newCount)); 855 } 856 } 857 858 861 public void setFocus() { 862 if (book != null) { 865 book.setFocus(); 866 } 867 if (activeRec != null) { 869 activeRec.page.setFocus(); 870 } 871 } 872 873 878 private void pageSelectionChanged(SelectionChangedEvent event) { 879 SelectionProvider provider = (SelectionProvider) getSite() 881 .getSelectionProvider(); 882 if (provider != null) { 883 provider.selectionChanged(event); 884 } 885 } 886 887 892 private void postSelectionChanged(SelectionChangedEvent event) { 893 SelectionProvider provider = (SelectionProvider) getSite() 895 .getSelectionProvider(); 896 if (provider != null) { 897 provider.postSelectionChanged(event); 898 } 899 } 900 901 904 private void showBootstrapPart() { 905 IWorkbenchPart part = getBootstrapPart(); 906 if (part != null) { 907 partActivated(part); 908 } 909 } 910 911 923 protected void showPageRec(PageRec pageRec) { 924 if (activeRec == pageRec) { 926 return; 927 } 928 if (activeRec != null && pageRec != null 930 && activeRec.page == pageRec.page) { 931 activeRec = pageRec; 932 return; 933 } 934 935 if (activeRec != null) { 937 PageSite pageSite = (PageSite) mapPageToSite.get(activeRec.page); 938 939 activeRec.subActionBars.deactivate(); 940 941 pageSite.deactivate(); 943 944 ISelectionProvider provider = pageSite.getSelectionProvider(); 946 if (provider != null) { 947 provider 948 .removeSelectionChangedListener(selectionChangedListener); 949 if (provider instanceof IPostSelectionProvider) { 950 ((IPostSelectionProvider) provider) 951 .removePostSelectionChangedListener(postSelectionListener); 952 } 953 } 954 } 955 956 activeRec = pageRec; 958 Control pageControl = activeRec.page.getControl(); 959 if (pageControl != null && !pageControl.isDisposed()) { 960 PageSite pageSite = (PageSite) mapPageToSite.get(activeRec.page); 961 962 book.showPage(pageControl); 965 activeRec.subActionBars.activate(); 966 refreshGlobalActionHandlers(); 967 968 pageSite.activate(); 970 971 ISelectionProvider provider = pageSite.getSelectionProvider(); 973 if (provider != null) { 974 provider.addSelectionChangedListener(selectionChangedListener); 975 if (provider instanceof IPostSelectionProvider) { 976 ((IPostSelectionProvider) provider) 977 .addPostSelectionChangedListener(postSelectionListener); 978 } 979 } 980 getViewSite().getActionBars().updateActionBars(); 982 } 983 } 984 985 990 protected SelectionProvider getSelectionProvider() { 991 return selectionProvider; 992 } 993 } 994 | Popular Tags |