1 11 package org.eclipse.ui.views.properties.tabbed; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.viewers.ILabelProviderListener; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.ISelectionChangedListener; 26 import org.eclipse.jface.viewers.IStructuredContentProvider; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 30 import org.eclipse.jface.viewers.SelectionChangedEvent; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.graphics.Point; 35 import org.eclipse.swt.layout.FillLayout; 36 import org.eclipse.swt.layout.FormAttachment; 37 import org.eclipse.swt.layout.FormData; 38 import org.eclipse.swt.layout.FormLayout; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.ui.IActionBars; 42 import org.eclipse.ui.IEditorPart; 43 import org.eclipse.ui.IPartListener; 44 import org.eclipse.ui.IViewPart; 45 import org.eclipse.ui.IWorkbenchPart; 46 import org.eclipse.ui.IWorkbenchWindow; 47 import org.eclipse.ui.actions.ActionFactory; 48 import org.eclipse.ui.internal.views.properties.tabbed.view.Tab; 49 import org.eclipse.ui.internal.views.properties.tabbed.view.TabDescriptor; 50 import org.eclipse.ui.internal.views.properties.tabbed.view.TabListContentProvider; 51 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyComposite; 52 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistry; 53 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistryFactory; 54 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyTitle; 55 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyViewer; 56 import org.eclipse.ui.part.IContributedContentsView; 57 import org.eclipse.ui.part.Page; 58 import org.eclipse.ui.views.properties.IPropertySheetPage; 59 import org.eclipse.ui.views.properties.PropertySheet; 60 61 66 public class TabbedPropertySheetPage 67 extends Page 68 implements IPropertySheetPage, ILabelProviderListener { 69 70 private TabbedPropertyComposite tabbedPropertyComposite; 72 73 private TabbedPropertySheetWidgetFactory widgetFactory; 74 75 private ITabbedPropertySheetPageContributor contributor; 76 77 private TabbedPropertyRegistry registry; 78 79 private ITabbedPropertySheetPageContributor selectionContributor = null; 80 81 85 private String currentContributorId; 86 87 protected IStructuredContentProvider tabListContentProvider; 88 89 private ISelection currentSelection; 90 91 private boolean activePropertySheet; 92 93 private TabbedPropertyViewer tabbedPropertyViewer; 94 95 private Tab currentTab; 96 97 private Map descriptorToTab; 98 99 private Map tabToComposite; 100 101 private List selectionQueue; 102 103 private boolean selectionQueueLocked; 104 105 private List tabSelectionListeners; 106 107 private IWorkbenchWindow cachedWorkbenchWindow; 108 109 private boolean hasTitleBar; 110 111 114 private IPartListener partActivationListener = new IPartListener() { 115 116 public void partActivated(IWorkbenchPart part) { 117 handlePartActivated(part); 118 } 119 120 public void partBroughtToTop(IWorkbenchPart part) { 121 } 122 123 public void partClosed(IWorkbenchPart part) { 124 } 125 126 public void partDeactivated(IWorkbenchPart part) { 127 } 128 129 public void partOpened(IWorkbenchPart part) { 130 } 131 }; 132 133 private class TabbedPropertySheetPageContributorFromSelection 134 implements ITabbedPropertySheetPageContributor { 135 136 private String contributorId; 137 138 144 public TabbedPropertySheetPageContributorFromSelection( 145 String contributorId) { 146 super(); 147 this.contributorId = contributorId; 148 } 149 150 153 public String getContributorId() { 154 return contributorId; 155 } 156 157 } 158 159 162 class TabbedPropertySheetPageLabelProvider 163 extends LabelProvider { 164 165 public String getText(Object element) { 166 if (element instanceof TabDescriptor) { 167 return ((TabDescriptor) element).getLabel(); 168 } 169 return null; 170 } 171 } 172 173 176 class SelectionChangedListener 177 implements ISelectionChangedListener { 178 179 182 public void selectionChanged(SelectionChangedEvent event) { 183 IStructuredSelection selection = (IStructuredSelection) event 184 .getSelection(); 185 Tab tab = null; 186 TabDescriptor descriptor = (TabDescriptor) selection 187 .getFirstElement(); 188 189 if (descriptor == null) { 190 hideTab(currentTab); 192 } else { 193 tab = (Tab) descriptorToTab.get(descriptor); 197 198 if (tab != currentTab) { 199 hideTab(currentTab); 200 } 201 202 Composite tabComposite = (Composite) tabToComposite.get(tab); 203 if (tabComposite == null) { 204 tabComposite = createTabComposite(); 205 tab.createControls(tabComposite, 206 TabbedPropertySheetPage.this); 207 tabToComposite.put(tab, tabComposite); 209 } 210 tab.setInput(tabbedPropertyViewer.getWorkbenchPart(), 212 (ISelection) tabbedPropertyViewer.getInput()); 213 214 storeCurrentTabSelection(descriptor.getLabel()); 216 217 if (tab != currentTab) { 218 showTab(tab); 219 } 220 221 tab.refresh(); 222 } 223 tabbedPropertyComposite.getTabComposite().layout(true); 224 currentTab = tab; 225 resizeScrolledComposite(); 226 227 if (descriptor != null) { 228 handleTabSelection(descriptor); 229 } 230 } 231 232 235 private void showTab(Tab target) { 236 if (target != null) { 237 Composite tabComposite = (Composite) tabToComposite.get(target); 238 if (tabComposite != null) { 239 243 tabComposite.moveAbove(null); 244 target.aboutToBeShown(); 245 tabComposite.setVisible(true); 246 } 247 } 248 } 249 250 253 private void hideTab(Tab target) { 254 if (target != null) { 255 Composite tabComposite = (Composite) tabToComposite.get(target); 256 if (tabComposite != null) { 257 target.aboutToBeHidden(); 258 tabComposite.setVisible(false); 259 } 260 } 261 } 262 263 } 264 265 271 public TabbedPropertySheetPage( 272 ITabbedPropertySheetPageContributor tabbedPropertySheetPageContributor) { 273 contributor = tabbedPropertySheetPageContributor; 274 tabToComposite = new HashMap (); 275 selectionQueue = new ArrayList (10); 276 tabSelectionListeners = new ArrayList (); 277 initContributor(contributor.getContributorId()); 278 } 279 280 286 protected void handlePartActivated(IWorkbenchPart part) { 287 291 boolean thisActivated = part instanceof PropertySheet 292 && ((PropertySheet) part).getCurrentPage() == this; 293 294 301 if (!thisActivated && !part.equals(contributor) 302 && !part.getSite().getId().equals(contributor.getContributorId())) { 303 307 IContributedContentsView view = (IContributedContentsView) part 308 .getAdapter(IContributedContentsView.class); 309 if (view == null 310 || (view.getContributingPart() != null && !view 311 .getContributingPart().equals(contributor))) { 312 if (activePropertySheet) { 313 if (currentTab != null) { 314 currentTab.aboutToBeHidden(); 315 } 316 activePropertySheet = false; 317 } 318 return; 319 } 320 } 321 if (!activePropertySheet && currentTab != null) { 322 currentTab.aboutToBeShown(); 323 currentTab.refresh(); 324 } 325 activePropertySheet = true; 326 } 327 328 331 public void createControl(Composite parent) { 332 widgetFactory = new TabbedPropertySheetWidgetFactory(); 333 tabbedPropertyComposite = new TabbedPropertyComposite(parent, 334 widgetFactory, hasTitleBar); 335 widgetFactory.paintBordersFor(tabbedPropertyComposite); 336 tabbedPropertyComposite.setLayout(new FormLayout()); 337 FormData formData = new FormData(); 338 formData.left = new FormAttachment(0, 0); 339 formData.right = new FormAttachment(100, 0); 340 formData.top = new FormAttachment(0, 0); 341 formData.bottom = new FormAttachment(100, 0); 342 tabbedPropertyComposite.setLayoutData(formData); 343 344 tabbedPropertyViewer = new TabbedPropertyViewer(tabbedPropertyComposite 345 .getList()); 346 tabbedPropertyViewer.setContentProvider(tabListContentProvider); 347 tabbedPropertyViewer 348 .setLabelProvider(new TabbedPropertySheetPageLabelProvider()); 349 tabbedPropertyViewer 350 .addSelectionChangedListener(new SelectionChangedListener()); 351 352 355 cachedWorkbenchWindow = getSite().getWorkbenchWindow(); 356 cachedWorkbenchWindow.getPartService().addPartListener( 357 partActivationListener); 358 359 362 if (hasTitleBar) { 363 registry.getLabelProvider().addListener(this); 364 } 365 } 366 367 373 private void initContributor(String contributorId) { 374 descriptorToTab = new HashMap (); 375 if (contributor.getContributorId().equals(contributorId)) { 376 379 registry = TabbedPropertyRegistryFactory.getInstance() 380 .createRegistry(contributor); 381 } else { 382 385 selectionContributor = new TabbedPropertySheetPageContributorFromSelection( 386 contributorId); 387 registry = TabbedPropertyRegistryFactory.getInstance() 388 .createRegistry(selectionContributor); 389 } 390 currentContributorId = contributorId; 391 tabListContentProvider = getTabListContentProvider(); 392 hasTitleBar = registry.getLabelProvider() != null; 393 394 if (tabbedPropertyViewer != null) { 395 tabbedPropertyViewer.setContentProvider(tabListContentProvider); 396 } 397 398 401 if (hasTitleBar) { 402 registry.getLabelProvider().addListener(this); 403 } 404 405 } 406 407 412 protected IStructuredContentProvider getTabListContentProvider() { 413 return new TabListContentProvider(registry); 414 } 415 416 424 private void disposeContributor() { 425 429 if (currentTab != null) { 430 currentTab.aboutToBeHidden(); 431 currentTab = null; 432 } 433 434 disposeTabs(descriptorToTab.values()); 435 descriptorToTab = new HashMap (); 436 437 440 if (hasTitleBar && registry != null) { 441 registry.getLabelProvider().removeListener(this); 442 } 443 444 if (selectionContributor != null) { 445 448 TabbedPropertyRegistryFactory.getInstance().disposeRegistry( 449 selectionContributor); 450 selectionContributor = null; 451 } 452 } 453 454 457 public void dispose() { 458 459 disposeContributor(); 460 461 if (widgetFactory != null) { 462 widgetFactory.dispose(); 463 widgetFactory = null; 464 } 465 468 if (cachedWorkbenchWindow != null) { 469 cachedWorkbenchWindow.getPartService().removePartListener( 470 partActivationListener); 471 cachedWorkbenchWindow = null; 472 } 473 474 if (registry != null) { 475 TabbedPropertyRegistryFactory.getInstance().disposeRegistry( 476 contributor); 477 registry = null; 478 } 479 480 contributor = null; 481 currentSelection = null; 482 } 483 484 487 public Control getControl() { 488 return tabbedPropertyComposite; 489 } 490 491 494 public void setActionBars(IActionBars actionBars) { 495 IActionBars partActionBars = null; 498 if (contributor instanceof IEditorPart) { 499 IEditorPart editorPart = (IEditorPart) contributor; 500 partActionBars = editorPart.getEditorSite().getActionBars(); 501 } else if (contributor instanceof IViewPart) { 502 IViewPart viewPart = (IViewPart) contributor; 503 partActionBars = viewPart.getViewSite().getActionBars(); 504 } 505 506 if (partActionBars != null) { 507 IAction action = partActionBars.getGlobalActionHandler(ActionFactory.UNDO 508 .getId()); 509 if (action != null) { 510 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), action); 511 } 512 action = partActionBars.getGlobalActionHandler(ActionFactory.REDO 513 .getId()); 514 if (action != null) { 515 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), action); 516 } 517 } 518 } 519 520 523 public void setFocus() { 524 getControl().setFocus(); 525 } 526 527 531 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 532 setInput(part, selection); 533 } 534 535 542 private void storeCurrentTabSelection(String label) { 543 if (!selectionQueueLocked) { 544 selectionQueue.remove(label); 545 selectionQueue.add(0, label); 546 } 547 } 548 549 private void resizeScrolledComposite() { 550 Point currentTabSize = new Point(0, 0); 551 if (currentTab != null) { 552 Composite sizeReference = (Composite) tabToComposite 553 .get(currentTab); 554 if (sizeReference != null) { 555 currentTabSize = sizeReference.computeSize(SWT.DEFAULT, SWT.DEFAULT); 556 } 557 } 558 tabbedPropertyComposite.getScrolledComposite().setMinSize( 559 currentTabSize.x, currentTabSize.y); 560 } 561 562 private void disposeTabs(Collection tabs) { 563 for (Iterator iter = tabs.iterator(); iter.hasNext();) { 564 Tab tab = (Tab) iter.next(); 565 Composite composite = (Composite) tabToComposite.remove(tab); 566 tab.dispose(); 567 if (composite != null) { 568 composite.dispose(); 569 } 570 } 571 } 572 573 576 private int getLastTabSelection(IWorkbenchPart part, ISelection input) { 577 TabDescriptor[] descriptors = registry.getTabDescriptors(part, input); 578 if (descriptors.length != 0) { 579 for (Iterator iter = selectionQueue.iterator(); iter.hasNext();) { 580 String text = (String ) iter.next(); 581 for (int i = 0; i < descriptors.length; i++) { 582 if (text.equals(descriptors[i].getLabel())) { 583 return i; 584 } 585 } 586 } 587 } 588 return 0; 589 } 590 591 597 protected void updateTabs(TabDescriptor[] descriptors) { 598 Map newTabs = new HashMap (descriptors.length * 2); 599 boolean disposingCurrentTab = (currentTab != null); 600 for (int i = 0; i < descriptors.length; i++) { 601 Tab tab = (Tab) descriptorToTab.remove(descriptors[i]); 602 603 if (tab != null && tab.controlsHaveBeenCreated()) { 604 if (tab == currentTab) { 605 disposingCurrentTab = false; 606 } 607 } else { 608 tab = (descriptors[i]).createTab(); 609 } 610 611 newTabs.put(descriptors[i], tab); 612 } 613 if (disposingCurrentTab) { 614 618 currentTab.aboutToBeHidden(); 619 currentTab = null; 620 } 621 disposeTabs(descriptorToTab.values()); 622 descriptorToTab = newTabs; 623 } 624 625 628 private Composite createTabComposite() { 629 Composite result = widgetFactory.createComposite( 630 tabbedPropertyComposite.getTabComposite(), SWT.NO_FOCUS); 631 result.setVisible(false); 632 result.setLayout(new FillLayout()); 633 FormData data = new FormData(); 634 if (hasTitleBar) { 635 data.top = new FormAttachment(tabbedPropertyComposite.getTitle(), 0); 636 } else { 637 data.top = new FormAttachment(0, 0); 638 } 639 data.bottom = new FormAttachment(100, 0); 640 data.left = new FormAttachment(0, 0); 641 data.right = new FormAttachment(100, 0); 642 result.setLayoutData(data); 643 return result; 644 } 645 646 private void setInput(IWorkbenchPart part, ISelection selection) { 647 if (selection.equals(currentSelection)) { 648 return; 649 } 650 651 this.currentSelection = selection; 652 653 validateRegistry(selection); 655 TabDescriptor[] descriptors = registry.getTabDescriptors(part, 656 currentSelection); 657 if (descriptors.length > 0) { 661 updateTabs(descriptors); 662 } 663 tabbedPropertyViewer.setInput(part, currentSelection); 665 int lastTabSelectionIndex = getLastTabSelection(part, currentSelection); 666 Object selectedTab = tabbedPropertyViewer 667 .getElementAt(lastTabSelectionIndex); 668 selectionQueueLocked = true; 669 try { 670 if (selectedTab == null) { 671 tabbedPropertyViewer.setSelection(null); 672 } else { 673 tabbedPropertyViewer.setSelection(new StructuredSelection( 674 selectedTab)); 675 } 676 } finally { 677 selectionQueueLocked = false; 678 } 679 refreshTitleBar(); 680 } 681 682 685 public void refresh() { 686 currentTab.refresh(); 687 } 688 689 694 public Tab getCurrentTab() { 695 return currentTab; 696 } 697 698 704 private void handleTabSelection(TabDescriptor tabDescriptor) { 705 if (selectionQueueLocked) { 706 709 return; 710 } 711 for (Iterator i = tabSelectionListeners.iterator(); i.hasNext();) { 712 ITabSelectionListener listener = (ITabSelectionListener) i.next(); 713 listener.tabSelected(tabDescriptor); 714 } 715 } 716 717 723 public void addTabSelectionListener(ITabSelectionListener listener) { 724 tabSelectionListeners.add(listener); 725 } 726 727 733 public void removeTabSelectionListener(ITabSelectionListener listener) { 734 tabSelectionListeners.remove(listener); 735 } 736 737 742 public TabbedPropertySheetWidgetFactory getWidgetFactory() { 743 return widgetFactory; 744 } 745 746 749 private void refreshTitleBar() { 750 if (hasTitleBar) { 751 TabbedPropertyTitle title = tabbedPropertyComposite.getTitle(); 752 if (currentTab == null) { 753 757 title.setTitle(null, null); 758 } else { 759 String text = registry.getLabelProvider().getText( 760 currentSelection); 761 Image image = registry.getLabelProvider().getImage( 762 currentSelection); 763 title.setTitle(text, image); 764 } 765 } 766 } 767 768 771 public void labelProviderChanged(LabelProviderChangedEvent event) { 772 refreshTitleBar(); 773 } 774 775 782 private ITabbedPropertySheetPageContributor getTabbedPropertySheetPageContributor( 783 Object object) { 784 if (object instanceof ITabbedPropertySheetPageContributor) { 785 return (ITabbedPropertySheetPageContributor) object; 786 } 787 788 if (object instanceof IAdaptable 789 && ((IAdaptable) object) 790 .getAdapter(ITabbedPropertySheetPageContributor.class) != null) { 791 return (ITabbedPropertySheetPageContributor) (((IAdaptable) object) 792 .getAdapter(ITabbedPropertySheetPageContributor.class)); 793 } 794 795 if (Platform.getAdapterManager().hasAdapter(object, 796 ITabbedPropertySheetPageContributor.class.getName())) { 797 return (ITabbedPropertySheetPageContributor) Platform 798 .getAdapterManager().loadAdapter(object, 799 ITabbedPropertySheetPageContributor.class.getName()); 800 } 801 802 return null; 803 } 804 805 818 private void validateRegistry(ISelection selection) { 819 if (selection == null) { 820 return; 821 } 822 823 if (!(selection instanceof IStructuredSelection)) { 824 return; 825 } 826 827 IStructuredSelection structuredSelection = (IStructuredSelection) selection; 828 if (structuredSelection.size() == 0) { 829 return; 830 } 831 832 ITabbedPropertySheetPageContributor newContributor = getTabbedPropertySheetPageContributor(structuredSelection.getFirstElement()); 833 834 if (newContributor == null) { 835 838 newContributor = contributor; 839 } 840 841 String selectionContributorId = newContributor.getContributorId(); 842 if (selectionContributorId.equals(currentContributorId)) { 843 847 return; 848 } 849 850 856 Iterator i = structuredSelection.iterator(); 857 i.next(); 858 while (i.hasNext()) { 859 newContributor = getTabbedPropertySheetPageContributor(i.next()); 860 if (newContributor == null || !newContributor.getContributorId().equals(selectionContributorId)) { 861 865 if (selectionContributor != null) { 866 disposeContributor(); 867 currentContributorId = contributor.getContributorId(); 868 initContributor(currentContributorId); 869 } 870 return; 871 } 872 } 873 874 878 disposeContributor(); 879 currentContributorId = selectionContributorId; 880 initContributor(currentContributorId); 881 overrideActionBars(); 882 } 883 884 887 private void overrideActionBars() { 888 if (registry.getActionProvider() != null ) { 889 IActionProvider actionProvider = registry.getActionProvider(); 890 actionProvider.setActionBars(contributor, getSite().getActionBars()); 891 } 892 } 893 894 } | Popular Tags |