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.util.Geometry; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.custom.CTabFolder; 20 import org.eclipse.swt.custom.CTabFolder2Adapter; 21 import org.eclipse.swt.custom.CTabFolderEvent; 22 import org.eclipse.swt.custom.CTabItem; 23 import org.eclipse.swt.custom.ViewForm; 24 import org.eclipse.swt.events.ControlEvent; 25 import org.eclipse.swt.events.ControlListener; 26 import org.eclipse.swt.events.DisposeEvent; 27 import org.eclipse.swt.events.DisposeListener; 28 import org.eclipse.swt.events.MouseAdapter; 29 import org.eclipse.swt.events.MouseEvent; 30 import org.eclipse.swt.events.MouseListener; 31 import org.eclipse.swt.graphics.Color; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.graphics.Rectangle; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.ui.internal.dnd.DragUtil; 37 import org.eclipse.ui.internal.dnd.SwtUtil; 38 import org.eclipse.ui.internal.layout.SizeCache; 39 import org.eclipse.ui.internal.presentations.util.ProxyControl; 40 import org.eclipse.ui.internal.tweaklets.TabBehaviour; 41 import org.eclipse.ui.internal.tweaklets.Tweaklets; 42 import org.eclipse.ui.presentations.IStackPresentationSite; 43 44 70 public final class PaneFolder { 71 private CTabFolder tabFolder; 73 74 private Control titleAreaProxy; 75 76 private ViewForm viewForm; 78 79 private ProxyControl contentProxy; 80 81 private ProxyControl viewFormTopLeftProxy; 82 83 private ProxyControl viewFormTopRightProxy; 84 85 private ProxyControl viewFormTopCenterProxy; 86 87 private SizeCache topRightCache = new SizeCache(); 89 90 private SizeCache topCenterCache = new SizeCache(); 91 92 private SizeCache topLeftCache = new SizeCache(); 93 94 private boolean putTrimOnTop = true; 95 96 private boolean topRightResized = false; 101 102 private boolean useTopRightOptimization = false; 103 104 private int lastWidth = 0; 105 106 108 private DisposeListener tabFolderDisposeListener = new DisposeListener() { 109 114 public void widgetDisposed(DisposeEvent e) { 115 PaneFolder.this.widgetDisposed(); 116 } 117 }; 118 119 124 private DisposeListener prematureDisposeListener = new DisposeListener() { 125 126 public void widgetDisposed(DisposeEvent e) { 127 Control disposedControl = (Control) e.widget; 128 129 if (isDisposed()) { 130 return; 131 } 132 133 disposedControl.removeDisposeListener(this); 135 136 if (disposedControl == topLeftCache.getControl()) { 137 setTopLeft(null); 138 } 139 140 if (disposedControl == topRightCache.getControl()) { 141 setTopRight(null); 142 } 143 144 if (disposedControl == topCenterCache.getControl()) { 145 setTopCenter(null); 146 } 147 } 148 149 }; 150 151 154 private List buttonListeners = new ArrayList (1); 155 156 private int state = IStackPresentationSite.STATE_RESTORED; 157 158 163 private int mousedownState = -1; 164 165 private CTabFolder2Adapter expandListener = new CTabFolder2Adapter() { 167 public void minimize(CTabFolderEvent event) { 168 event.doit = false; 169 notifyButtonListeners(IStackPresentationSite.STATE_MINIMIZED); 170 } 171 172 public void restore(CTabFolderEvent event) { 173 event.doit = false; 174 notifyButtonListeners(IStackPresentationSite.STATE_RESTORED); 175 } 176 177 public void maximize(CTabFolderEvent event) { 178 event.doit = false; 179 notifyButtonListeners(IStackPresentationSite.STATE_MAXIMIZED); 180 } 181 182 187 public void close(CTabFolderEvent event) { 188 event.doit = false; 189 notifyCloseListeners((CTabItem) event.item); 190 } 191 192 public void showList(CTabFolderEvent event) { 193 notifyShowListeners(event); 194 } 195 196 }; 197 198 private MouseListener mouseListener = new MouseAdapter() { 199 public void mouseDown(MouseEvent e) { 200 mousedownState = getState(); 201 } 202 203 public void mouseDoubleClick(MouseEvent e) { 204 } 205 }; 206 207 private boolean showButtons = true; 208 private boolean minimizeVisible = false; 209 private boolean maximizeVisible = false; 210 211 214 private boolean inLayout = false; 215 216 private int tabPosition; 217 218 225 public PaneFolder(Composite parent, int flags) { 226 { 228 tabFolder = new CTabFolder(parent, flags); 229 230 tabFolder.setMRUVisible(((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).enableMRUTabVisibility()); 231 232 titleAreaProxy = new Composite(tabFolder, SWT.NO_BACKGROUND); 234 titleAreaProxy.setVisible(false); 235 titleAreaProxy.addControlListener(new ControlListener() { 236 public void controlMoved(ControlEvent e) { 237 topRightResized = true; 238 } 239 240 public void controlResized(ControlEvent e) { 241 topRightResized = true; 242 243 if (!inLayout && !PaneFolder.this.isDisposed() 246 && viewForm!=null && contentProxy!=null) { 247 PaneFolder.this.aboutToResize(); 248 PaneFolder.this.layout(false); 249 } 250 } 251 }); 252 tabFolder.setTopRight(titleAreaProxy, SWT.FILL); 253 254 tabFolder.addCTabFolder2Listener(expandListener); 255 256 tabFolder.addMouseListener(mouseListener); 257 258 tabFolder.addDisposeListener(tabFolderDisposeListener); 259 } 260 261 { 263 viewForm = new ViewForm(tabFolder, SWT.NO_BACKGROUND); 264 265 viewFormTopLeftProxy = new ProxyControl(viewForm); 268 viewFormTopCenterProxy = new ProxyControl(viewForm); 269 viewFormTopRightProxy = new ProxyControl(viewForm); 270 271 contentProxy = new ProxyControl(viewForm); 272 viewForm.setContent(contentProxy.getControl()); 273 } 274 } 275 276 282 public Rectangle getTitleArea() { 283 return titleAreaProxy.getBounds(); 284 } 285 286 291 public Composite getControl() { 292 return tabFolder; 293 } 294 295 public void flushTopCenterSize() { 296 topCenterCache.flush(); 297 viewForm.changed(new Control[] {viewFormTopCenterProxy.getControl()}); 298 } 299 300 307 public void setTopCenter(Control topCenter) { 308 if (topCenter == topCenterCache.getControl()) { 309 return; 310 } 311 312 removeDisposeListener(topCenterCache.getControl()); 313 314 topCenterCache.setControl(topCenter); 315 316 if (putTrimOnTop) { 317 viewFormTopCenterProxy.setTarget(null); 318 } else { 319 viewFormTopCenterProxy.setTarget(topCenterCache); 320 } 321 322 viewForm.changed(new Control[] {viewFormTopCenterProxy.getControl()}); 323 324 if (topCenter != null) { 325 topCenter.addDisposeListener(prematureDisposeListener); 326 327 if (!putTrimOnTop) { 328 if (!viewForm.isDisposed()) { 329 viewForm.setTopCenter(viewFormTopCenterProxy.getControl()); 330 } 331 } 332 } else { 333 if (!putTrimOnTop) { 334 if (!viewForm.isDisposed()) { 335 viewForm.setTopCenter(null); 336 } 337 } 338 } 339 } 340 341 346 public void setTopRight(Control topRight) { 347 if (topRightCache.getControl() == topRight) { 348 return; 349 } 350 351 removeDisposeListener(topRightCache.getControl()); 352 353 topRightCache.setControl(topRight); 354 355 if (putTrimOnTop) { 356 viewFormTopRightProxy.setTarget(null); 357 } else { 358 viewFormTopRightProxy.setTarget(topRightCache); 359 } 360 361 if (topRight != null) { 362 topRight.addDisposeListener(prematureDisposeListener); 363 if (!putTrimOnTop) { 364 365 viewForm.setTopRight(viewFormTopRightProxy.getControl()); 366 } 367 } else { 368 if (!putTrimOnTop) { 369 viewForm.setTopRight(null); 370 } 371 } 372 } 373 374 379 public void setTopLeft(Control topLeft) { 380 if (topLeftCache.getControl() == topLeft) { 381 return; 382 } 383 384 removeDisposeListener(topLeftCache.getControl()); 385 386 topLeftCache.setControl(topLeft); 387 if (topLeft != null) { 389 topLeft.addDisposeListener(prematureDisposeListener); 390 viewFormTopLeftProxy.setTarget(topLeftCache); 391 viewForm.setTopLeft(viewFormTopLeftProxy.getControl()); 392 } else { 393 viewFormTopLeftProxy.setTarget(null); 394 viewForm.setTopLeft(null); 395 } 396 } 397 398 402 public void aboutToResize() { 403 useTopRightOptimization = true; 404 topRightResized = false; 405 lastWidth = getControl().getBounds().width; 406 } 407 408 416 public void showMinMax(boolean show) { 417 showButtons = show; 418 setMaximizeVisible(show); 419 setMinimizeVisible(show); 420 layout(true); 421 } 422 423 public void layout(boolean flushCache) { 424 if (inLayout) { 425 return; 426 } 427 428 inLayout = true; 429 try { 430 431 viewForm.setLayoutDeferred(true); 432 433 tabFolder.setMinimizeVisible(showButtons && minimizeVisible); 434 tabFolder.setMaximizeVisible(showButtons && maximizeVisible); 435 436 if (flushCache) { 438 topLeftCache.flush(); 439 topRightCache.flush(); 440 topCenterCache.flush(); 441 } 442 443 if (!(useTopRightOptimization && (topRightResized || lastWidth == getControl() 446 .getBounds().width))) { 447 tabFolder.setTopRight(titleAreaProxy, SWT.FILL); 450 } 451 useTopRightOptimization = false; 452 454 Rectangle titleArea = DragUtil.getDisplayBounds(titleAreaProxy); 455 456 Point topRightSize = topRightCache 457 .computeSize(SWT.DEFAULT, SWT.DEFAULT); 458 Point topCenterSize = topCenterCache.computeSize(SWT.DEFAULT, 459 SWT.DEFAULT); 460 461 int requiredWidth = topRightSize.x + topCenterSize.x; 463 int requiredHeight = Math.max(topRightSize.y, topCenterSize.y); 464 465 boolean lastTrimOnTop = putTrimOnTop; 466 putTrimOnTop = (titleArea.width >= requiredWidth && titleArea.height >= requiredHeight); 467 468 Control topRight = topRightCache.getControl(); 469 Control topCenter = topCenterCache.getControl(); 470 471 if (putTrimOnTop) { 472 if (!lastTrimOnTop) { 476 viewFormTopCenterProxy.setTarget(null); 478 viewFormTopRightProxy.setTarget(null); 479 viewForm.setTopCenter(null); 480 viewForm.setTopRight(null); 481 } 482 483 Rectangle topRightArea = new Rectangle(titleArea.x 484 + titleArea.width - topRightSize.x, titleArea.y 485 + (titleArea.height - topRightSize.y) / 2, topRightSize.x, 486 topRightSize.y); 487 488 if (topRight != null) { 489 topRight.setBounds(Geometry.toControl(topRight.getParent(), 490 topRightArea)); 491 } 492 493 if (topCenter != null) { 494 Rectangle topCenterArea = new Rectangle(topRightArea.x 495 - topCenterSize.x, titleArea.y 496 + (titleArea.height - topCenterSize.y) / 2, 497 topCenterSize.x, topCenterSize.y); 498 499 Rectangle localCoords = Geometry.toControl(topCenter 500 .getParent(), topCenterArea); 501 502 topCenter.setBounds(localCoords); 503 } 504 } else { 505 if (lastTrimOnTop) { 506 if (topCenter != null) { 507 viewFormTopCenterProxy.setTarget(topCenterCache); 508 viewForm.setTopCenter(viewFormTopCenterProxy.getControl()); 509 } 510 511 if (topRight != null) { 512 viewFormTopRightProxy.setTarget(topRightCache); 513 viewForm.setTopRight(viewFormTopRightProxy.getControl()); 514 } 515 } 516 } 517 518 Rectangle newBounds = tabFolder.getClientArea(); 519 viewForm.setBounds(newBounds); 520 } finally { 521 viewForm.setLayoutDeferred(false); 522 inLayout = false; 523 } 524 525 viewFormTopRightProxy.layout(); 526 viewFormTopLeftProxy.layout(); 527 viewFormTopCenterProxy.layout(); 528 } 529 530 public Composite getContentParent() { 531 return viewForm; 532 } 533 534 public void setContent(Control newContent) { 535 viewForm.setContent(newContent); 536 } 537 538 543 public int getState() { 544 return state; 545 } 546 547 551 protected void notifyButtonListeners(int buttonId) { 552 if (mousedownState == getState()) { 553 Iterator iter = buttonListeners.iterator(); 554 555 while (iter.hasNext()) { 556 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 557 .next(); 558 559 listener.stateButtonPressed(buttonId); 560 } 561 } 562 } 563 564 public Control getContent() { 565 return viewForm.getContent(); 566 } 567 568 573 protected void notifyShowListeners(CTabFolderEvent event) { 574 Iterator iter = buttonListeners.iterator(); 575 576 while (iter.hasNext()) { 577 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 578 .next(); 579 580 listener.showList(event); 581 } 582 } 583 584 589 protected void notifyCloseListeners(CTabItem tabItem) { 590 Iterator iter = buttonListeners.iterator(); 591 592 while (iter.hasNext()) { 593 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 594 .next(); 595 596 listener.closeButtonPressed(tabItem); 597 } 598 } 599 600 606 public void setState(int state) { 607 this.state = state; 608 609 tabFolder.setMinimized(state == IStackPresentationSite.STATE_MINIMIZED); 610 tabFolder.setMaximized(state == IStackPresentationSite.STATE_MAXIMIZED); 611 } 612 613 public void addButtonListener(PaneFolderButtonListener listener) { 614 buttonListeners.add(listener); 615 } 616 617 public void removeButtonListener(PaneFolderButtonListener listener) { 618 buttonListeners.remove(listener); 619 } 620 621 public void setTabPosition(int newTabPosition) { 622 tabPosition = newTabPosition; 623 tabFolder.setTabPosition(tabPosition); 624 } 625 626 public int getTabPosition() { 627 return tabPosition; 628 } 629 630 public boolean isDisposed() { 631 return tabFolder == null || tabFolder.isDisposed(); 632 } 633 634 public CTabItem createItem(int style, int index) { 635 return new CTabItem(tabFolder, style, index); 636 } 637 638 public Point computeMinimumSize() { 639 Point result = Geometry.getSize(tabFolder.computeTrim(0, 0, 0, 0)); 640 641 result.x += 100; 645 return result; 646 } 647 648 655 private void removeDisposeListener(Control oldControl) { 656 if (!SwtUtil.isDisposed(oldControl)) { 657 oldControl.removeDisposeListener(prematureDisposeListener); 658 } 659 } 660 661 private void widgetDisposed() { 662 removeDisposeListener(topCenterCache.getControl()); 663 topCenterCache.setControl(null); 664 removeDisposeListener(topRightCache.getControl()); 665 topRightCache.setControl(null); 666 removeDisposeListener(topLeftCache.getControl()); 667 topLeftCache.setControl(null); 668 } 669 670 public Point getChevronLocation() { 671 int numItems = tabFolder.getItemCount(); 673 CTabItem item = null, tempItem = null; 674 for (int i = 0; i < numItems; i++) { 675 tempItem = tabFolder.getItem(i); 676 if (tempItem.isShowing()) { 677 item = tempItem; 678 } 679 } 680 681 if (item == null) { 683 return new Point(0, 0); 684 } 685 686 Rectangle itemBounds = item.getBounds(); 687 int x = itemBounds.x + itemBounds.width; 688 int y = itemBounds.y + itemBounds.height; 689 return new Point(x, y); 690 } 691 692 696 public void setSelection(int selection) { 697 tabFolder.setSelection(selection); 698 } 699 700 707 public Rectangle computeTrim(int i, int j, int k, int l) { 708 return tabFolder.computeTrim(i, j, k, l); 709 } 710 711 714 public void setUnselectedCloseVisible(boolean b) { 715 tabFolder.setUnselectedCloseVisible(b); 716 } 717 718 721 public void setSelectionForeground(Color fgColor) { 722 tabFolder.setSelectionForeground(fgColor); 723 } 724 725 730 public void setSelectionBackground(Color[] bgColors, int[] percentages, 731 boolean vertical) { 732 tabFolder.setSelectionBackground(bgColors, percentages, vertical); 733 } 734 735 public CTabItem getItem(int idx) { 736 return tabFolder.getItem(idx); 737 } 738 739 public int getSelectionIndex() { 740 return tabFolder.getSelectionIndex(); 741 } 742 743 public int getTabHeight() { 744 return tabFolder.getTabHeight(); 745 } 746 747 public int indexOf(CTabItem toFind) { 748 return tabFolder.indexOf(toFind); 749 } 750 751 public void setTabHeight(int height) { 752 tabFolder.setTabHeight(height); 753 } 754 755 758 public int getItemCount() { 759 return tabFolder.getItemCount(); 760 } 761 762 765 public CTabItem[] getItems() { 766 return tabFolder.getItems(); 767 } 768 769 public CTabItem getItem(Point toGet) { 770 return tabFolder.getItem(toGet); 771 } 772 773 public CTabItem getSelection() { 774 return tabFolder.getSelection(); 775 } 776 777 780 public void setMinimizeVisible(boolean isVisible) { 781 tabFolder.setMinimizeVisible(isVisible); 782 minimizeVisible = isVisible; 783 } 784 785 795 public void setMinimumCharacters(int count) { 796 tabFolder.setMinimumCharacters(count); 797 } 798 799 802 public void setMaximizeVisible(boolean isVisible) { 803 tabFolder.setMaximizeVisible(isVisible); 804 maximizeVisible = isVisible; 805 } 806 807 810 public void setSimpleTab(boolean traditionalTab) { 811 tabFolder.setSimple(traditionalTab); 812 } 813 814 817 public void setUnselectedImageVisible(boolean b) { 818 tabFolder.setUnselectedImageVisible(b); 819 } 820 821 824 public void setSingleTab(boolean b) { 825 tabFolder.setSingle(b); 826 } 827 828 public void hideTitle() { 829 tabFolder.setTabHeight(0); 830 } 831 832 public ViewForm getViewForm() { 833 return viewForm; 834 } 835 836 846 public void setVisible(boolean visible) { 847 contentProxy.setVisible(visible); 848 viewFormTopCenterProxy.setVisible(visible); 849 viewFormTopLeftProxy.setVisible(visible); 850 viewFormTopRightProxy.setVisible(visible); 851 } 852 } 853 | Popular Tags |