1 11 package org.eclipse.ui.internal.presentations; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.action.GroupMarker; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.action.Separator; 21 import org.eclipse.jface.resource.ColorRegistry; 22 import org.eclipse.jface.resource.FontRegistry; 23 import org.eclipse.jface.util.Assert; 24 import org.eclipse.jface.util.Geometry; 25 import org.eclipse.jface.util.IPropertyChangeListener; 26 import org.eclipse.jface.util.PropertyChangeEvent; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.custom.CTabFolderEvent; 29 import org.eclipse.swt.custom.CTabItem; 30 import org.eclipse.swt.events.DisposeEvent; 31 import org.eclipse.swt.events.DisposeListener; 32 import org.eclipse.swt.events.MouseAdapter; 33 import org.eclipse.swt.events.MouseEvent; 34 import org.eclipse.swt.events.MouseListener; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.events.ShellAdapter; 38 import org.eclipse.swt.events.ShellEvent; 39 import org.eclipse.swt.events.ShellListener; 40 import org.eclipse.swt.graphics.Color; 41 import org.eclipse.swt.graphics.Font; 42 import org.eclipse.swt.graphics.GC; 43 import org.eclipse.swt.graphics.Image; 44 import org.eclipse.swt.graphics.Point; 45 import org.eclipse.swt.graphics.Rectangle; 46 import org.eclipse.swt.widgets.Control; 47 import org.eclipse.swt.widgets.Event; 48 import org.eclipse.swt.widgets.Label; 49 import org.eclipse.swt.widgets.Listener; 50 import org.eclipse.swt.widgets.Menu; 51 import org.eclipse.swt.widgets.Monitor; 52 import org.eclipse.swt.widgets.Shell; 53 import org.eclipse.swt.widgets.ToolBar; 54 import org.eclipse.swt.widgets.ToolItem; 55 import org.eclipse.ui.IMemento; 56 import org.eclipse.ui.IPropertyListener; 57 import org.eclipse.ui.PlatformUI; 58 import org.eclipse.ui.internal.IWorkbenchConstants; 59 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 60 import org.eclipse.ui.internal.IWorkbenchThemeConstants; 61 import org.eclipse.ui.internal.WorkbenchImages; 62 import org.eclipse.ui.internal.WorkbenchMessages; 63 import org.eclipse.ui.internal.dnd.DragUtil; 64 import org.eclipse.ui.internal.util.Util; 65 import org.eclipse.ui.presentations.IPartMenu; 66 import org.eclipse.ui.presentations.IPresentablePart; 67 import org.eclipse.ui.presentations.IPresentationSerializer; 68 import org.eclipse.ui.presentations.IStackPresentationSite; 69 import org.eclipse.ui.presentations.PresentationUtil; 70 import org.eclipse.ui.presentations.StackDropResult; 71 import org.eclipse.ui.presentations.StackPresentation; 72 import org.eclipse.ui.themes.ITheme; 73 import org.eclipse.ui.themes.IThemeManager; 74 75 80 public class DefaultPartPresentation extends StackPresentation { 81 82 private PaneFolder tabFolder; 83 private IPresentablePart current; 84 private int activeState = StackPresentation.AS_INACTIVE; 85 private MenuManager systemMenuManager = new MenuManager(); 86 private Label titleLabel; 87 private Listener dragListener; 88 private List activationList = new ArrayList (10); 89 private boolean activationListChange = true; 90 91 95 private int dragStart = -1; 96 97 private final static String TAB_DATA = DefaultPartPresentation.class.getName() + ".partId"; 99 private PaneFolderButtonListener buttonListener = new PaneFolderButtonListener() { 100 public void stateButtonPressed(int buttonId) { 101 getSite().setState(buttonId); 102 } 103 104 public void closeButtonPressed(CTabItem item) { 105 IPresentablePart part = getPartForTab(item); 106 107 getSite().close(new IPresentablePart[]{part}); 108 } 109 110 public void showList(CTabFolderEvent event) { 111 event.doit = false; 112 showPartList(); 113 } 114 }; 115 116 private MouseListener mouseListener = new MouseAdapter() { 117 public void mouseDown(MouseEvent e) { 118 if (e.widget instanceof Control) { 119 Control ctrl = (Control)e.widget; 120 121 Point globalPos = ctrl.toDisplay(new Point(e.x, e.y)); 122 123 CTabItem newItem = tabFolder.getItem(tabFolder.getControl().toControl(globalPos)); 126 if (newItem != null) { 127 CTabItem oldItem = tabFolder.getSelection(); 128 if (newItem != oldItem) 129 return; 130 } 131 if (current != null) { 132 current.setFocus(); 133 } 134 } 135 } 136 137 public void mouseDoubleClick(MouseEvent e) { 138 if (getSite().getState() == IStackPresentationSite.STATE_MAXIMIZED) { 139 getSite().setState(IStackPresentationSite.STATE_RESTORED); 140 } else { 141 getSite().setState(IStackPresentationSite.STATE_MAXIMIZED); 142 } 143 } 144 }; 145 146 private Listener menuListener = new Listener() { 147 150 public void handleEvent(Event event) { 151 Point pos = new Point(event.x, event.y); 152 153 showSystemMenu(pos); 154 } 155 }; 156 157 private Listener selectionListener = new Listener() { 158 public void handleEvent(Event e) { 159 if (ignoreSelectionChanges) { 160 return; 161 } 162 163 IPresentablePart item = getPartForTab((CTabItem)e.item); 164 165 if (item != null) { 166 setSelection((CTabItem)e.item); 167 } 168 } 169 }; 170 171 private IPropertyListener childPropertyChangeListener = new IPropertyListener() { 172 public void propertyChanged(Object source, int property) { 173 174 if(isDisposed()) 175 return; 176 177 if (source instanceof IPresentablePart) { 178 IPresentablePart part = (IPresentablePart) source; 179 childPropertyChanged(part, property); 180 } 181 } 182 }; 183 184 private DisposeListener tabDisposeListener = new DisposeListener() { 185 public void widgetDisposed(DisposeEvent e) { 186 if (e.widget instanceof CTabItem) { 187 CTabItem item = (CTabItem)e.widget; 188 189 IPresentablePart part = getPartForTab(item); 190 191 part.removePropertyListener(childPropertyChangeListener); 192 } 193 } 194 }; 195 196 private DisposeListener folderDisposeListener = new DisposeListener() { 197 public void widgetDisposed(DisposeEvent e) { 198 DefaultPartPresentation.this.widgetDisposed(); 199 } 200 }; 201 202 private ToolBar viewToolBar; 203 204 private boolean shellActive = true; 205 206 private boolean ignoreSelectionChanges = false; 207 208 private ShellListener shellListener = new ShellAdapter() { 209 210 public void shellActivated(ShellEvent e) { 211 shellActive = true; 212 updateGradient(); 213 } 214 215 216 public void shellDeactivated(ShellEvent e) { 217 shellActive = false; 218 updateGradient(); 219 } 220 }; 221 222 226 private IPropertyChangeListener themeListener = new IPropertyChangeListener() { 227 228 231 public void propertyChange(PropertyChangeEvent event) { 232 233 if(isDisposed()) 234 return; 235 236 String property = event.getProperty(); 237 if (property.equals(IThemeManager.CHANGE_CURRENT_THEME)) { 238 updateGradient(); 239 setTitleAttributes(); 240 } 241 else if (property.equals(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START) 242 || property.equals(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END) 243 || property.equals(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR) 244 || property.equals(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR) 245 || property.equals(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START) 246 || property.equals(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END) 247 || property.equals(IWorkbenchThemeConstants.TAB_TEXT_FONT)) { 248 updateGradient(); 249 } 250 else if (property.equals(IWorkbenchThemeConstants.VIEW_MESSAGE_TEXT_FONT)) { 251 setTitleAttributes(); 252 } 253 } 254 }; 255 256 public DefaultPartPresentation(PaneFolder control, IStackPresentationSite stackSite) { 257 super(stackSite); 258 259 shellActive = control.getControl().getShell().equals(control.getControl().getDisplay().getActiveShell()); 260 261 tabFolder = control; 262 tabFolder.getControl().addDisposeListener(folderDisposeListener); 264 265 tabFolder.setMinimizeVisible(stackSite.supportsState(IStackPresentationSite.STATE_MINIMIZED)); 266 tabFolder.setMaximizeVisible(stackSite.supportsState(IStackPresentationSite.STATE_MAXIMIZED)); 267 268 tabFolder.getControl().getShell().addShellListener(shellListener); 269 270 titleLabel = new Label(tabFolder.getControl(), SWT.NONE); 271 titleLabel.setVisible(false); 272 titleLabel.moveAbove(null); 273 274 PlatformUI 275 .getWorkbench() 276 .getThemeManager() 277 .addPropertyChangeListener(themeListener); 278 279 280 viewToolBar = new ToolBar(control.getControl(), SWT.FLAT); 281 viewToolBar.moveAbove(null); 282 283 ToolItem pullDownButton = new ToolItem(viewToolBar, SWT.PUSH); 284 Image hoverImage = 286 WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU_HOVER); 287 pullDownButton.setDisabledImage(hoverImage); pullDownButton.setImage(hoverImage); 290 pullDownButton.setToolTipText(WorkbenchMessages.getString("Menu")); pullDownButton.addSelectionListener(new SelectionAdapter() { 292 public void widgetSelected(SelectionEvent e) { 293 showPaneMenu(); 294 } 295 }); 296 297 tabFolder.getControl().addListener(SWT.Selection, selectionListener); 299 300 tabFolder.getControl().addMouseListener(mouseListener); 302 303 tabFolder.getControl().addListener(SWT.MenuDetect, menuListener); 304 305 tabFolder.addButtonListener(buttonListener); 306 307 dragListener = new Listener() { 308 public void handleEvent(Event event) { 309 310 Point localPos = new Point(event.x, event.y); 311 if ((tabFolder.getControl().getStyle() & SWT.TOP) != 0) { 313 if (localPos.y > tabFolder.getTabHeight()) { 314 return; 315 } 316 } else { 317 if (localPos.y < tabFolder.getControl().getBounds().height - tabFolder.getTabHeight()) { 318 return; 319 } 320 } 321 322 CTabItem tabUnderPointer = tabFolder.getItem(localPos); 323 324 if (tabUnderPointer == null) { 325 getSite().dragStart(tabFolder.getControl().toDisplay(localPos), false); 326 return; 327 } 328 329 IPresentablePart part = getPartForTab(tabUnderPointer); 330 331 if (getSite().isPartMoveable(part)) { 332 dragStart = tabFolder.indexOf(tabUnderPointer); 333 getSite().dragStart(part, 334 tabFolder.getControl().toDisplay(localPos), false); 335 dragStart = -1; 336 } 337 } 338 }; 339 340 PresentationUtil.addDragListener(tabFolder.getControl(), dragListener); 341 342 titleLabel.addMouseListener(mouseListener); 343 344 { systemMenuManager.add(new GroupMarker("misc")); systemMenuManager.add(new GroupMarker("restore")); systemMenuManager.add(new UpdatingActionContributionItem(new SystemMenuRestore(getSite()))); 348 349 systemMenuManager.add(new SystemMenuMove(getSite(), getPaneName())); 350 systemMenuManager.add(new GroupMarker("size")); systemMenuManager.add(new GroupMarker("state")); systemMenuManager.add(new UpdatingActionContributionItem(new SystemMenuMinimize(getSite()))); 353 354 systemMenuManager.add(new UpdatingActionContributionItem(new SystemMenuMaximize(getSite()))); 355 systemMenuManager.add(new Separator("close")); systemMenuManager.add(new UpdatingActionContributionItem(new SystemMenuClose(getSite()))); 357 358 getSite().addSystemActions(systemMenuManager); 359 } } 361 362 368 public void restoreState(IPresentationSerializer serializer, IMemento savedState) { 369 IMemento[] parts = savedState.getChildren(IWorkbenchConstants.TAG_PART); 370 371 for (int idx = 0; idx < parts.length; idx++) { 372 String id = parts[idx].getString(IWorkbenchConstants.TAG_ID); 373 374 if (id != null) { 375 IPresentablePart part = serializer.getPart(id); 376 377 if (part != null) { 378 addPart(part, tabFolder.getItemCount()); 379 } 380 } 381 } 382 } 383 384 385 388 public void saveState(IPresentationSerializer context, IMemento memento) { 389 super.saveState(context, memento); 390 391 List parts = getPresentableParts(); 392 393 Iterator iter = parts.iterator(); 394 while (iter.hasNext()) { 395 IPresentablePart next = (IPresentablePart)iter.next(); 396 397 IMemento childMem = memento.createChild(IWorkbenchConstants.TAG_PART); 398 childMem.putString(IWorkbenchConstants.TAG_ID, context.getId(next)); 399 } 400 } 401 402 406 public void init() { 407 updateGradient(); 408 setTitleAttributes(); 409 } 410 411 414 protected void setTitleAttributes() { 415 Assert.isTrue(!isDisposed()); 416 417 ITheme theme = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(); 418 Font messageFont = theme.getFontRegistry().get(IWorkbenchThemeConstants.VIEW_MESSAGE_TEXT_FONT); 419 if (!messageFont.equals(titleLabel.getFont())) { 420 titleLabel.setFont(messageFont); 421 setControlSize(); 422 } 423 } 424 425 428 protected final void setInactiveTabColors() { 429 Assert.isTrue(!isDisposed()); 430 431 ITheme theme = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(); 432 ColorRegistry colorRegistry = theme.getColorRegistry(); 433 434 drawGradient( 435 colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR), 436 new Color [] { 437 colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START), 438 colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END) 439 }, 440 new int [] {theme.getInt(IWorkbenchThemeConstants.INACTIVE_TAB_PERCENT)}, 441 theme.getBoolean(IWorkbenchThemeConstants.INACTIVE_TAB_VERTICAL)); 442 } 443 444 448 protected final void setActiveTabColors() { 449 Assert.isTrue(!isDisposed()); 450 451 ITheme theme = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(); 452 ColorRegistry colorRegistry = theme.getColorRegistry(); 453 454 if (isShellActive()) { 455 drawGradient( 456 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR), 457 new Color [] { 458 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START), 459 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END) 460 }, 461 new int [] {theme.getInt(IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT)}, 462 theme.getBoolean(IWorkbenchThemeConstants.ACTIVE_TAB_VERTICAL)); 463 } 464 else { 465 drawGradient( 466 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_TEXT_COLOR), 467 new Color [] { 468 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_START), 469 colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_END) 470 }, 471 new int [] {theme.getInt(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_PERCENT)}, 472 theme.getBoolean(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_VERTICAL)); 473 474 } 475 } 476 477 482 protected void updateGradient() { 483 Assert.isTrue(!isDisposed()); 484 } 486 487 490 protected int computeTabHeight() { 491 Assert.isTrue(!isDisposed()); 492 493 GC gc = new GC(tabFolder.getControl()); 494 495 int tabHeight = Math.max( 497 viewToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, 498 gc.getFontMetrics().getHeight()); 499 500 gc.dispose(); 501 502 return tabHeight; 503 } 504 505 protected String getPaneName() { 506 return WorkbenchMessages.getString("DefaultPartPresentation.PaneName"); } 508 509 512 public void showPaneMenu() { 513 Assert.isTrue(!isDisposed()); 514 515 IPartMenu menu = getPartMenu(); 516 517 if (menu != null) { 518 Rectangle bounds = DragUtil.getDisplayBounds(viewToolBar); 519 menu.showMenu(new Point(bounds.x, bounds.y + bounds.height)); 520 } 521 } 522 523 528 protected IPresentablePart getCurrent() { 529 Assert.isTrue(!isDisposed()); 530 531 return current; 532 } 533 534 542 protected final int indexOf(IPresentablePart part) { 543 Assert.isTrue(!isDisposed()); 544 545 if (part == null) { 546 return tabFolder.getItemCount(); 547 } 548 549 CTabItem[] items = tabFolder.getItems(); 550 551 for (int idx = 0; idx < items.length; idx++) { 552 IPresentablePart tabPart = getPartForTab(items[idx]); 553 554 if (part == tabPart) { 555 return idx; 556 } 557 } 558 559 return items.length; 560 } 561 562 568 protected final CTabItem getTab(IPresentablePart part) { 569 Assert.isTrue(!isDisposed()); 570 CTabItem[] items = tabFolder.getItems(); 571 572 int idx = indexOf(part); 573 574 if (idx < items.length) { 575 return items[idx]; 576 } 577 578 return null; 579 } 580 581 585 protected void childPropertyChanged(IPresentablePart part, int property) { 586 587 CTabItem tab = getTab(part); 588 if (tab == null) { 592 return; 593 } 594 595 initTab(tab, part); 596 597 switch (property) { 598 case IPresentablePart.PROP_BUSY: 599 break; 600 case IPresentablePart.PROP_HIGHLIGHT_IF_BACK: 601 FontRegistry registry = 602 PlatformUI.getWorkbench(). 603 getThemeManager().getCurrentTheme(). 604 getFontRegistry(); 605 606 if(getCurrent() != part) tab.setFont(registry.getBold(IWorkbenchThemeConstants.TAB_TEXT_FONT)); 608 break; 609 case IPresentablePart.PROP_CONTENT_DESCRIPTION: 610 case IPresentablePart.PROP_TOOLBAR: 611 case IPresentablePart.PROP_PANE_MENU: 612 case IPresentablePart.PROP_TITLE: 613 if (getCurrent() == part) { 614 setControlSize(); 615 } 616 break; 617 } 618 } 619 620 protected final IPresentablePart getPartForTab(CTabItem item) { 621 Assert.isTrue(!isDisposed()); 622 623 IPresentablePart part = (IPresentablePart)item.getData(TAB_DATA); 624 625 return part; 626 } 627 628 633 protected PaneFolder getTabFolder() { 634 Assert.isTrue(!isDisposed()); 635 return tabFolder; 636 } 637 638 643 public boolean isDisposed() { 644 return tabFolder == null || tabFolder.isDisposed(); 645 } 646 647 655 public void drawGradient(Color fgColor, Color [] bgColors, int [] percentages, boolean vertical) { 656 Assert.isTrue(!isDisposed()); 657 658 tabFolder.setSelectionForeground(fgColor); 659 tabFolder.setSelectionBackground(bgColors, percentages, vertical); 660 } 661 662 public boolean isActive() { 663 Assert.isTrue(!isDisposed()); 664 665 return activeState == StackPresentation.AS_ACTIVE_FOCUS; 666 } 667 668 public int getActive() { 669 Assert.isTrue(!isDisposed()); 670 671 return activeState; 672 } 673 674 protected String getCurrentTitle() { 675 Assert.isTrue(!isDisposed()); 676 677 if (current == null) { 678 return ""; } 680 681 String result = current.getTitleStatus(); 682 683 return result; 684 } 685 686 protected void layout(boolean changed) { 687 Assert.isTrue(!isDisposed()); 688 689 if (changed) { 690 String currentTitle = getCurrentTitle(); 691 692 if (!currentTitle.equals(Util.ZERO_LENGTH_STRING)) { 693 tabFolder.setTopLeft(titleLabel); 694 titleLabel.setText(currentTitle); 695 titleLabel.setVisible(true); 696 } else { 697 tabFolder.setTopLeft(null); 698 titleLabel.setVisible(false); 699 } 700 701 Control currentToolbar = getCurrentToolbar(); 702 tabFolder.setTopCenter(currentToolbar); 703 704 IPartMenu partMenu = getPartMenu(); 705 706 if (partMenu != null) { 707 tabFolder.setTopRight(viewToolBar); 708 viewToolBar.setVisible(true); 709 } else { 710 tabFolder.setTopRight(null); 711 viewToolBar.setVisible(false); 712 } 713 } 714 715 tabFolder.layout(changed); 716 717 if (current != null) { 718 Rectangle clientArea = tabFolder.getClientArea(); 719 Rectangle bounds = tabFolder.getControl().getBounds(); 720 clientArea.x += bounds.x; 721 clientArea.y += bounds.y; 722 723 current.setBounds(clientArea); 724 } 725 } 726 727 730 protected void setControlSize() { 731 Assert.isTrue(!isDisposed()); 732 733 layout(true); 734 } 735 736 742 protected IPartMenu getPartMenu() { 743 Assert.isTrue(!isDisposed()); 744 745 IPresentablePart part = getCurrentPart(); 746 if (part == null) { 747 return null; 748 } 749 750 return part.getMenu(); 751 } 752 753 754 758 public final void dispose() { 759 if (isDisposed()) { 760 return; 761 } 762 tabFolder.getControl().dispose(); 767 } 768 769 772 protected void widgetDisposed() { 773 tabFolder.getControl().getShell().removeShellListener(shellListener); 774 PresentationUtil.removeDragListener(tabFolder.getControl(), dragListener); 775 776 systemMenuManager.dispose(); 777 systemMenuManager.removeAll(); 778 779 tabFolder.getControl().removeDisposeListener(folderDisposeListener); 780 tabFolder = null; 781 782 titleLabel.dispose(); 783 titleLabel = null; 784 785 viewToolBar.dispose(); 786 787 PlatformUI 788 .getWorkbench() 789 .getThemeManager() 790 .removePropertyChangeListener(themeListener); 791 792 activationList = null; 793 } 794 795 798 public void setActive(int newState) { 799 Assert.isTrue(!isDisposed()); 800 801 activeState = newState; 802 } 803 804 private CTabItem createPartTab(IPresentablePart part, int tabIndex) { 805 Assert.isTrue(!isDisposed()); 806 807 tabIndex = Math.min(tabIndex, tabFolder.getItemCount()); 808 809 CTabItem tabItem; 810 811 int style = SWT.NONE; 812 813 if (getSite().isCloseable(part)) { 814 style |= SWT.CLOSE; 815 } 816 817 ignoreSelectionChanges = true; 818 try { 819 tabItem = tabFolder.createItem(style, tabIndex); 820 } finally { 821 ignoreSelectionChanges = false; 822 } 823 824 tabItem.setData(TAB_DATA, part); 825 826 part.addPropertyListener(childPropertyChangeListener); 827 tabItem.addDisposeListener(tabDisposeListener); 828 829 initTab(tabItem, part); 830 831 return tabItem; 832 } 833 834 843 protected void initTab(CTabItem tabItem, IPresentablePart part) { 844 Assert.isTrue(!isDisposed()); 845 846 tabItem.setText(part.getName()); 847 848 tabItem.setImage(part.getTitleImage()); 849 850 String toolTipText = part.getTitleToolTip(); 851 if (!toolTipText.equals(Util.ZERO_LENGTH_STRING)) { 852 tabItem.setToolTipText(toolTipText); 853 } 854 855 FontRegistry registry = 856 PlatformUI.getWorkbench(). 857 getThemeManager().getCurrentTheme(). 858 getFontRegistry(); 859 860 if(part.isBusy()) 861 tabItem.setFont(registry.getItalic(IWorkbenchThemeConstants.TAB_TEXT_FONT)); 862 else{ 863 tabItem.setFont(null); 864 } 865 866 } 867 868 871 public void addPart(IPresentablePart newPart, Object cookie) { 872 873 int idx; 874 List lruList = null; 875 876 if (cookie instanceof Integer ) { 877 idx = ((Integer )cookie).intValue(); 878 addPart(newPart, idx); 879 return; 880 } else { 881 if (cookie != null && cookie instanceof List ) { 882 List list = (List )cookie; 891 lruList = (List )list.get(0); 892 idx = ((Integer )list.get(1)).intValue(); 893 } 894 else { 895 idx = getItemInsertionIndex(); 897 lruList = getVisibleItemsLRUList(); 898 } 899 } 900 901 addPart(newPart, idx); 902 903 if (tabFolder.getItemCount() > 1) { 907 CTabItem myItem = getTab(newPart); 909 if (myItem == null) 910 return; 911 while (!myItem.isShowing()) { 913 IPresentablePart lruPart = null; 914 CTabItem lruItem = null; 915 916 for (int i = 0; i < lruList.size(); i++) { 919 lruItem = (CTabItem)lruList.get(i); 920 if (!myItem.equals(lruItem)) { 921 lruItem = (CTabItem)lruList.remove(i); 922 break; 923 } 924 } 925 if (myItem.equals(lruItem) || lruItem == null) 926 break; 927 928 lruPart = getPartForTab(lruItem); 929 try { 932 activationListChange = false; 933 removePart(lruPart); 934 addPart(lruPart, --idx+1); 937 } 938 finally { 939 activationListChange = true; 940 } 941 } 942 } 943 } 944 945 949 private List getVisibleItemsLRUList() { 950 List lruList = new ArrayList (10); 951 953 for (int i = 0; i < tabFolder.getItemCount(); i++) { 959 CTabItem item = tabFolder.getItem(i); 960 if (item.isShowing()) { 962 int lruListSize = lruList.size(); 963 if (lruListSize == 0) 964 lruList.add(item); 965 else { 966 int j = 0; 967 for (j = 0; j < lruListSize; j++) { 971 if (activationList.indexOf(getPartForTab(item)) < 972 activationList.indexOf(getPartForTab((CTabItem)lruList.get(j)))) 973 break; 974 } 975 lruList.add(j, item); 976 } 977 } 978 } 979 return lruList; 980 } 981 982 986 private int getItemInsertionIndex() { 987 int idx = tabFolder.getItemCount(); 990 991 if (!tabFolder.getControl().isVisible() || tabFolder.getControl().getBounds().width <= 1) 996 return idx; 997 for (int i = 0; i < idx; i++) { 998 CTabItem item = tabFolder.getItem(i); 999 if (!item.isShowing() && item.getBounds().x >= 0) { 1001 return i; 1002 } 1003 } 1004 return idx; 1005 } 1006 1007 1014 public void addPart(IPresentablePart newPart, int index) { 1015 if (getTab(newPart) != null) { 1017 return; 1018 } 1019 createPartTab(newPart, index); 1020 1021 setControlSize(); 1022 if (activationListChange) 1026 activationList.add(newPart); 1027 } 1028 1029 1032 public void removePart(IPresentablePart oldPart) { 1033 if (current == oldPart) { 1034 current = null; 1035 tabFolder.setTopLeft(null); 1037 tabFolder.setTopCenter(null); 1038 tabFolder.setTopRight(null); 1039 } 1040 1041 CTabItem item = getTab(oldPart); 1042 if (item == null) { 1043 return; 1044 } 1045 oldPart.setVisible(false); 1046 1047 if (activationListChange) 1051 activationList.remove(oldPart); 1052 1053 ignoreSelectionChanges = true; 1054 try { 1055 item.dispose(); 1056 } finally { 1057 ignoreSelectionChanges = false; 1058 } 1059 } 1060 1061 1064 public void selectPart(IPresentablePart toSelect) { 1065 Assert.isTrue(!isDisposed()); 1066 1067 if (toSelect == current) { 1068 return; 1069 } 1070 1071 IPresentablePart oldPart = current; 1072 1073 current = toSelect; 1074 1075 if (current != null) { 1076 CTabItem item = getTab(toSelect); 1077 if (item != null) 1078 if (isVisibleTabs()) { 1083 if (!item.isShowing() && tabFolder.getItemCount() > 1) { 1084 try { 1085 activationListChange = false; 1086 List lruList = getVisibleItemsLRUList(); 1088 int idx = getItemInsertionIndex(); 1089 if (item.getBounds().x < 0) 1092 idx--; 1093 1094 removePart(toSelect); 1095 1096 List cookie = new ArrayList (2); 1099 cookie.add(lruList); 1100 cookie.add(new Integer (idx)); 1101 1102 addPart(toSelect, cookie); 1103 } 1104 finally { 1105 activationListChange = true; 1106 } 1107 } 1108 } 1109 current = toSelect; 1110 tabFolder.setSelection(indexOf(current)); 1111 current.setVisible(true); 1112 if (isDisposed()) 1118 return; 1119 if (getTab(toSelect) == null) { 1120 selectPart(oldPart); 1121 return; 1122 } 1123 setControlSize(); 1124 if (activationListChange) { 1126 activationList.remove(toSelect); 1127 activationList.add(toSelect); 1128 } 1129 } 1130 1131 if (oldPart != null) { 1132 oldPart.setVisible(false); 1133 } 1134 } 1135 1136 1140 private boolean isVisibleTabs() { 1141 boolean result = false; 1142 for (int i = 0; i < tabFolder.getItemCount(); i++) { 1143 CTabItem item = tabFolder.getItem(i); 1144 if (item.isShowing()) { 1145 result = true; 1146 break; 1147 } 1148 } 1149 return result; 1150 } 1151 1152 public IPresentablePart getCurrentPart() { 1153 Assert.isTrue(!isDisposed()); 1154 1155 return current; 1156 } 1157 1158 1161 public void setBounds(Rectangle bounds) { 1162 Assert.isTrue(!isDisposed()); 1163 1164 tabFolder.aboutToResize(); 1165 tabFolder.getControl().setBounds(bounds); 1166 layout(false); 1167 } 1168 1169 1172 public Point computeMinimumSize() { 1173 return tabFolder.computeMinimumSize(); 1174 } 1175 1176 1179 public void setVisible(boolean isVisible) { 1180 Assert.isTrue(!isDisposed()); 1181 1182 if (current != null) { 1183 current.setVisible(isVisible); 1184 } 1185 tabFolder.getControl().setVisible(isVisible); 1186 } 1187 1188 1191 public void setState(int state) { 1192 Assert.isTrue(!isDisposed()); 1193 1194 tabFolder.setState(state); 1195 } 1196 1197 1200 public IMenuManager getSystemMenuManager() { 1201 Assert.isTrue(!isDisposed()); 1202 1203 return systemMenuManager; 1204 } 1205 1206 1210 protected void showSystemMenu(Point point) { 1211 Assert.isTrue(!isDisposed()); 1212 1213 Menu aMenu = systemMenuManager.createContextMenu(tabFolder.getControl().getParent()); 1214 systemMenuManager.update(true); 1215 aMenu.setLocation(point.x, point.y); 1216 aMenu.setVisible(true); 1217 } 1218 1219 1222 public Control getControl() { 1223 Assert.isTrue(!isDisposed()); 1224 1225 return tabFolder.getControl(); 1226 } 1227 1228 1231 public StackDropResult dragOver(Control currentControl, Point location) { 1232 Assert.isTrue(!isDisposed()); 1233 1234 Point localPos = tabFolder.getControl().toControl(location); 1236 1237 CTabItem tabUnderPointer = tabFolder.getItem(localPos); 1238 1239 if (tabUnderPointer == null) { 1242 Rectangle titleArea = tabFolder.getTitleArea(); 1243 1244 if (titleArea.contains(localPos)) { 1247 int dragOverIndex = tabFolder.getItemCount(); 1248 CTabItem lastTab = tabFolder.getItem(dragOverIndex - 1); 1249 1250 if (!lastTab.isShowing()) { 1252 return null; 1253 } 1254 1255 if (dragStart >= 0) { 1256 dragOverIndex--; 1257 1258 return new StackDropResult(Geometry.toDisplay(tabFolder.getControl(), 1259 lastTab.getBounds()), 1260 new Integer (dragOverIndex)); 1261 } 1262 1263 Rectangle dropRectangle = Geometry.toDisplay(tabFolder.getControl(), titleArea); 1267 1268 dropRectangle.width = 3 * dropRectangle.height; 1269 return new StackDropResult(dropRectangle, new Integer (dragOverIndex)); 1270 1271 } else { 1272 return null; 1273 } 1274 } 1275 1276 if (!tabUnderPointer.isShowing()) { 1277 return null; 1278 } 1279 1280 return new StackDropResult(Geometry.toDisplay(tabFolder.getControl(), tabUnderPointer.getBounds()), 1281 new Integer (tabFolder.indexOf(tabUnderPointer))); 1282 } 1283 1284 1290 protected Control getCurrentToolbar() { 1291 Assert.isTrue(!isDisposed()); 1292 1293 IPresentablePart part = getCurrentPart(); 1294 if (part == null) { 1295 return null; 1296 } 1297 1298 return part.getToolBar(); 1299 } 1300 1301 1304 public void showSystemMenu() { 1305 Assert.isTrue(!isDisposed()); 1306 1307 IPresentablePart part = getCurrentPart(); 1308 if (part != null) { 1309 Rectangle bounds = DragUtil.getDisplayBounds(tabFolder.getControl()); 1310 1311 int idx = tabFolder.getSelectionIndex(); 1312 if (idx > -1) { 1313 CTabItem item = tabFolder.getItem(idx); 1314 Rectangle itemBounds = item.getBounds(); 1315 1316 bounds.x += itemBounds.x; 1317 bounds.y += itemBounds.y; 1318 } 1319 1320 Point location = new Point(bounds.x, bounds.y + tabFolder.getTabHeight()); 1321 showSystemMenu(location); 1322 } 1323 } 1324 1325 1328 public Control[] getTabList(IPresentablePart part) { 1329 Assert.isTrue(!isDisposed()); 1330 1331 ArrayList list = new ArrayList (); 1332 if (tabFolder.getTabPosition() == SWT.BOTTOM) { 1333 if (part.getToolBar() != null) list.add(part.getToolBar()); 1334 if (part.getControl() != null) list.add(part.getControl()); 1335 if (getTabFolder() != null) list.add(getTabFolder().getControl()); 1336 } 1337 else { 1338 if (getTabFolder() != null) list.add(getTabFolder().getControl()); 1339 if (part.getToolBar() != null) list.add(part.getToolBar()); 1340 if (part.getControl() != null) list.add(part.getControl()); 1341 } 1342 return (Control[]) list.toArray(new Control[list.size()]); 1343 } 1344 1345 protected void showList(Shell parentShell, int x, int y) { 1346 Assert.isTrue(!isDisposed()); 1347 1348 final PaneFolder tabFolder = getTabFolder(); 1349 1350 int shellStyle = SWT.RESIZE | SWT.ON_TOP | SWT.NO_TRIM; 1351 int tableStyle = SWT.V_SCROLL | SWT.H_SCROLL; 1352 final BasicStackList editorList = new BasicStackList(tabFolder.getControl().getShell(), 1353 shellStyle, tableStyle); 1354 editorList.setInput(this); 1355 Point size = editorList.computeSizeHint(); 1356 1357 Monitor mon = getTabFolder().getControl().getMonitor(); 1358 Rectangle bounds = mon.getClientArea(); 1359 if (x + size.x > bounds.x + bounds.width) x = bounds.x + bounds.width - size.x; 1360 if (y + size.y > bounds.y + bounds.height) y = bounds.y + bounds.height - size.y; 1361 editorList.setLocation(new Point(x, y)); 1362 editorList.setVisible(true); 1363 editorList.setFocus(); 1364 editorList.getTableViewer().getTable().getShell().addListener( 1365 SWT.Deactivate, new Listener() { 1366 1367 public void handleEvent(Event event) { 1368 editorList.setVisible(false); 1369 } 1370 }); 1371 } 1372 1373 1376 public void showPartList() { 1377 Assert.isTrue(!isDisposed()); 1378 1379 PaneFolder tabFolder = getTabFolder(); 1380 Shell shell = tabFolder.getControl().getShell(); 1381 1382 int numItems = tabFolder.getItemCount(); 1384 CTabItem item = null, tempItem = null; 1385 for (int i = 0; i < numItems; i++) { 1386 tempItem = tabFolder.getItem(i); 1387 if (tempItem.isShowing()) 1388 item = tempItem; 1389 } 1390 1391 if (item == null) 1393 return; 1394 1395 Rectangle itemBounds = item.getBounds(); 1396 int x = itemBounds.x+itemBounds.width; 1397 int y = itemBounds.y + itemBounds.height; 1398 Point location = item.getDisplay().map(tabFolder.getControl(), null, x, y); 1399 showList(shell, location.x, location.y); 1400 } 1401 1402 void setSelection(CTabItem tabItem) { 1403 getSite().selectPart(getPartForTab(tabItem)); 1404 } 1405 1406 void close(IPresentablePart[] presentablePart) { 1407 getSite().close(presentablePart); 1408 } 1409 1410 1413 List getPresentableParts() { 1414 Assert.isTrue(!isDisposed()); 1415 1416 CTabItem[] items = tabFolder.getItems(); 1417 List result = new ArrayList (items.length); 1418 1419 for (int idx = 0; idx < tabFolder.getItemCount(); idx++) { 1420 result.add(getPartForTab(items[idx])); 1421 } 1422 1423 return result; 1424 } 1425 1426 Image getLabelImage(IPresentablePart presentablePart) { 1427 return presentablePart.getTitleImage(); 1428 } 1429 1430 String getLabelText(IPresentablePart presentablePart, 1431 boolean includePath) { 1432 String title = presentablePart.getName(); 1433 return title; 1434 } 1435 1436 1439 protected boolean isShellActive() { 1440 Assert.isTrue(!isDisposed()); 1441 1442 return shellActive; 1443 } 1444} 1445 | Popular Tags |