1 11 package org.eclipse.ui.internal.presentations.r33; 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 import org.eclipse.ui.presentations.StackPresentation; 44 45 71 public final class PaneFolder { 72 private CTabFolder tabFolder; 74 75 private Control titleAreaProxy; 76 77 private ViewForm viewForm; 79 80 private ProxyControl contentProxy; 81 82 private ProxyControl viewFormTopLeftProxy; 83 84 private ProxyControl viewFormTopRightProxy; 85 86 private ProxyControl viewFormTopCenterProxy; 87 88 private SizeCache topRightCache = new SizeCache(); 90 91 private SizeCache topCenterCache = new SizeCache(); 92 93 private SizeCache topLeftCache = new SizeCache(); 94 95 private boolean putTrimOnTop = true; 96 97 private boolean topRightResized = false; 102 103 private boolean useTopRightOptimization = false; 104 105 private int lastWidth = 0; 106 107 109 private DisposeListener tabFolderDisposeListener = new DisposeListener() { 110 115 public void widgetDisposed(DisposeEvent e) { 116 PaneFolder.this.widgetDisposed(); 117 } 118 }; 119 120 125 private DisposeListener prematureDisposeListener = new DisposeListener() { 126 127 public void widgetDisposed(DisposeEvent e) { 128 Control disposedControl = (Control) e.widget; 129 130 if (isDisposed()) { 131 return; 132 } 133 134 disposedControl.removeDisposeListener(this); 136 137 if (disposedControl == topLeftCache.getControl()) { 138 setTopLeft(null); 139 } 140 141 if (disposedControl == topRightCache.getControl()) { 142 setTopRight(null); 143 } 144 145 if (disposedControl == topCenterCache.getControl()) { 146 setTopCenter(null); 147 } 148 } 149 150 }; 151 152 155 private List buttonListeners = new ArrayList (1); 156 157 private int state = IStackPresentationSite.STATE_RESTORED; 158 159 164 private int mousedownState = -1; 165 166 private CTabFolder2Adapter expandListener = new CTabFolder2Adapter() { 168 public void minimize(CTabFolderEvent event) { 169 event.doit = false; 170 notifyButtonListeners(IStackPresentationSite.STATE_MINIMIZED); 171 } 172 173 public void restore(CTabFolderEvent event) { 174 event.doit = false; 175 notifyButtonListeners(IStackPresentationSite.STATE_RESTORED); 176 } 177 178 public void maximize(CTabFolderEvent event) { 179 event.doit = false; 180 notifyButtonListeners(IStackPresentationSite.STATE_MAXIMIZED); 181 } 182 183 188 public void close(CTabFolderEvent event) { 189 event.doit = false; 190 notifyCloseListeners((CTabItem) event.item); 191 } 192 193 public void showList(CTabFolderEvent event) { 194 notifyShowListeners(event); 195 } 196 197 }; 198 199 private MouseListener mouseListener = new MouseAdapter() { 200 public void mouseDown(MouseEvent e) { 201 mousedownState = getState(); 202 } 203 204 public void mouseDoubleClick(MouseEvent e) { 205 } 206 }; 207 208 private boolean showButtons = true; 209 private boolean minimizeVisible = false; 210 private boolean maximizeVisible = false; 211 212 215 private boolean inLayout = false; 216 217 private int tabPosition; 218 219 226 public PaneFolder(Composite parent, int flags) { 227 { 229 tabFolder = new CTabFolder(parent, flags); 230 231 tabFolder.setMRUVisible(((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).enableMRUTabVisibility()); 232 233 titleAreaProxy = new Composite(tabFolder, SWT.NO_BACKGROUND); 236 titleAreaProxy.setVisible(false); 237 titleAreaProxy.addControlListener(new ControlListener() { 238 public void controlMoved(ControlEvent e) { 239 topRightResized = true; 240 } 241 242 public void controlResized(ControlEvent e) { 243 topRightResized = true; 244 245 if (!inLayout && !PaneFolder.this.isDisposed() 248 && viewForm != null && contentProxy != null) { 249 PaneFolder.this.aboutToResize(); 250 PaneFolder.this.layout(false); 251 } 252 } 253 }); 254 tabFolder.setTopRight(titleAreaProxy, SWT.FILL); 255 256 tabFolder.addCTabFolder2Listener(expandListener); 257 258 tabFolder.addMouseListener(mouseListener); 259 260 tabFolder.addDisposeListener(tabFolderDisposeListener); 261 } 262 263 { 265 viewForm = new ViewForm(tabFolder, SWT.NO_BACKGROUND); 266 267 viewFormTopLeftProxy = new ProxyControl(viewForm); 270 viewFormTopCenterProxy = new ProxyControl(viewForm); 271 viewFormTopRightProxy = new ProxyControl(viewForm); 272 273 contentProxy = new ProxyControl(viewForm); 274 viewForm.setContent(contentProxy.getControl()); 275 } 276 } 277 278 284 public Rectangle getTitleArea() { 285 return titleAreaProxy.getBounds(); 286 } 287 288 293 public Composite getControl() { 294 return tabFolder; 295 } 296 297 public void flushTopCenterSize() { 298 topCenterCache.flush(); 299 viewForm.changed(new Control[] { viewFormTopCenterProxy.getControl() }); 300 } 301 302 309 public void setTopCenter(Control topCenter) { 310 if (topCenter == topCenterCache.getControl()) { 311 return; 312 } 313 314 removeDisposeListener(topCenterCache.getControl()); 315 316 topCenterCache.setControl(topCenter); 317 318 if (putTrimOnTop) { 319 viewFormTopCenterProxy.setTarget(null); 320 } else { 321 viewFormTopCenterProxy.setTarget(topCenterCache); 322 } 323 324 viewForm.changed(new Control[] { viewFormTopCenterProxy.getControl() }); 325 326 if (topCenter != null) { 327 topCenter.addDisposeListener(prematureDisposeListener); 328 329 if (!putTrimOnTop) { 330 if (!viewForm.isDisposed()) { 331 viewForm.setTopCenter(viewFormTopCenterProxy.getControl()); 332 } 333 } 334 } else { 335 if (!putTrimOnTop) { 336 if (!viewForm.isDisposed()) { 337 viewForm.setTopCenter(null); 338 } 339 } 340 } 341 } 342 343 348 public void setTopRight(Control topRight) { 349 if (topRightCache.getControl() == topRight) { 350 return; 351 } 352 353 removeDisposeListener(topRightCache.getControl()); 354 355 topRightCache.setControl(topRight); 356 357 if (putTrimOnTop) { 358 viewFormTopRightProxy.setTarget(null); 359 } else { 360 viewFormTopRightProxy.setTarget(topRightCache); 361 } 362 363 if (topRight != null) { 364 topRight.addDisposeListener(prematureDisposeListener); 365 if (!putTrimOnTop) { 366 367 viewForm.setTopRight(viewFormTopRightProxy.getControl()); 368 } 369 } else { 370 if (!putTrimOnTop) { 371 viewForm.setTopRight(null); 372 } 373 } 374 } 375 376 381 public void setTopLeft(Control topLeft) { 382 if (topLeftCache.getControl() == topLeft) { 383 return; 384 } 385 386 removeDisposeListener(topLeftCache.getControl()); 387 388 topLeftCache.setControl(topLeft); 389 if (topLeft != null) { 391 topLeft.addDisposeListener(prematureDisposeListener); 392 viewFormTopLeftProxy.setTarget(topLeftCache); 393 viewForm.setTopLeft(viewFormTopLeftProxy.getControl()); 394 } else { 395 viewFormTopLeftProxy.setTarget(null); 396 viewForm.setTopLeft(null); 397 } 398 } 399 400 404 public void aboutToResize() { 405 useTopRightOptimization = true; 406 topRightResized = false; 407 lastWidth = getControl().getBounds().width; 408 } 409 410 417 public void showMinMax(boolean show) { 418 showButtons = show; 419 setMaximizeVisible(show); 420 setMinimizeVisible(show); 421 layout(true); 422 } 423 424 public void layout(boolean flushCache) { 425 if (inLayout) { 426 return; 427 } 428 429 inLayout = true; 430 try { 431 432 viewForm.setLayoutDeferred(true); 433 434 tabFolder.setMinimizeVisible(showButtons && minimizeVisible); 435 tabFolder.setMaximizeVisible(showButtons && maximizeVisible); 436 437 if (flushCache) { 439 topLeftCache.flush(); 440 topRightCache.flush(); 441 topCenterCache.flush(); 442 } 443 444 if (!(useTopRightOptimization && (topRightResized || lastWidth == getControl() 448 .getBounds().width))) { 449 tabFolder.setTopRight(titleAreaProxy, SWT.FILL); 453 } 454 useTopRightOptimization = false; 455 457 Rectangle titleArea = DragUtil.getDisplayBounds(titleAreaProxy); 458 459 Point topRightSize = topRightCache.computeSize(SWT.DEFAULT, 460 SWT.DEFAULT); 461 Point topCenterSize = topCenterCache.computeSize(SWT.DEFAULT, 462 SWT.DEFAULT); 463 464 int requiredWidth = topRightSize.x + topCenterSize.x; 466 int requiredHeight = Math.max(topRightSize.y, topCenterSize.y); 467 468 boolean lastTrimOnTop = putTrimOnTop; 469 putTrimOnTop = (titleArea.width >= requiredWidth && titleArea.height >= requiredHeight); 470 471 Control topRight = topRightCache.getControl(); 472 Control topCenter = topCenterCache.getControl(); 473 474 if (putTrimOnTop) { 475 if (!lastTrimOnTop) { 480 viewFormTopCenterProxy.setTarget(null); 482 viewFormTopRightProxy.setTarget(null); 483 viewForm.setTopCenter(null); 484 viewForm.setTopRight(null); 485 } 486 487 Rectangle topRightArea = new Rectangle(titleArea.x 488 + titleArea.width - topRightSize.x, titleArea.y 489 + (titleArea.height - topRightSize.y) / 2, 490 topRightSize.x, topRightSize.y); 491 492 if (topRight != null) { 493 topRight.setBounds(Geometry.toControl(topRight.getParent(), 494 topRightArea)); 495 } 496 497 if (topCenter != null) { 498 Rectangle topCenterArea = new Rectangle(topRightArea.x 499 - topCenterSize.x, titleArea.y 500 + (titleArea.height - topCenterSize.y) / 2, 501 topCenterSize.x, topCenterSize.y); 502 503 Rectangle localCoords = Geometry.toControl(topCenter 504 .getParent(), topCenterArea); 505 506 topCenter.setBounds(localCoords); 507 } 508 } else { 509 if (topCenter != null) { 511 viewFormTopCenterProxy.setTarget(topCenterCache); 512 viewForm.setTopCenter(viewFormTopCenterProxy 513 .getControl()); 514 } 515 516 if (topRight != null) { 517 viewFormTopRightProxy.setTarget(topRightCache); 518 viewForm 519 .setTopRight(viewFormTopRightProxy.getControl()); 520 } 521 } 522 523 Rectangle newBounds = tabFolder.getClientArea(); 524 viewForm.setBounds(newBounds); 525 } finally { 526 viewForm.setLayoutDeferred(false); 527 inLayout = false; 528 } 529 530 viewFormTopRightProxy.layout(); 531 viewFormTopLeftProxy.layout(); 532 viewFormTopCenterProxy.layout(); 533 } 534 535 public Composite getContentParent() { 536 return viewForm; 537 } 538 539 public void setContent(Control newContent) { 540 viewForm.setContent(newContent); 541 } 542 543 548 public int getState() { 549 return state; 550 } 551 552 556 protected void notifyButtonListeners(int buttonId) { 557 if (mousedownState == getState()) { 558 Iterator iter = buttonListeners.iterator(); 559 560 while (iter.hasNext()) { 561 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 562 .next(); 563 564 listener.stateButtonPressed(buttonId); 565 } 566 } 567 } 568 569 public Control getContent() { 570 return viewForm.getContent(); 571 } 572 573 578 protected void notifyShowListeners(CTabFolderEvent event) { 579 Iterator iter = buttonListeners.iterator(); 580 581 while (iter.hasNext()) { 582 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 583 .next(); 584 585 listener.showList(event); 586 } 587 } 588 589 594 protected void notifyCloseListeners(CTabItem tabItem) { 595 Iterator iter = buttonListeners.iterator(); 596 597 while (iter.hasNext()) { 598 PaneFolderButtonListener listener = (PaneFolderButtonListener) iter 599 .next(); 600 601 listener.closeButtonPressed(tabItem); 602 } 603 } 604 605 611 public void setState(int state) { 612 this.state = state; 613 614 tabFolder.setMinimized(state == IStackPresentationSite.STATE_MINIMIZED); 615 tabFolder.setMaximized(state == IStackPresentationSite.STATE_MAXIMIZED); 616 } 617 618 public void addButtonListener(PaneFolderButtonListener listener) { 619 buttonListeners.add(listener); 620 } 621 622 public void removeButtonListener(PaneFolderButtonListener listener) { 623 buttonListeners.remove(listener); 624 } 625 626 public void setTabPosition(int newTabPosition) { 627 tabPosition = newTabPosition; 628 tabFolder.setTabPosition(tabPosition); 629 } 630 631 public int getTabPosition() { 632 return tabPosition; 633 } 634 635 public boolean isDisposed() { 636 return tabFolder == null || tabFolder.isDisposed(); 637 } 638 639 public CTabItem createItem(int style, int index) { 640 return new CTabItem(tabFolder, style, index); 641 } 642 643 public Point computeMinimumSize() { 644 Point result = Geometry.getSize(tabFolder.computeTrim(0, 0, 0, 0)); 645 646 result.x += 100; 650 return result; 651 } 652 653 660 private void removeDisposeListener(Control oldControl) { 661 if (!SwtUtil.isDisposed(oldControl)) { 662 oldControl.removeDisposeListener(prematureDisposeListener); 663 } 664 } 665 666 private void widgetDisposed() { 667 removeDisposeListener(topCenterCache.getControl()); 668 topCenterCache.setControl(null); 669 removeDisposeListener(topRightCache.getControl()); 670 topRightCache.setControl(null); 671 removeDisposeListener(topLeftCache.getControl()); 672 topLeftCache.setControl(null); 673 } 674 675 public Point getChevronLocation() { 676 int numItems = tabFolder.getItemCount(); 678 CTabItem item = null, tempItem = null; 679 for (int i = 0; i < numItems; i++) { 680 tempItem = tabFolder.getItem(i); 681 if (tempItem.isShowing()) { 682 item = tempItem; 683 } 684 } 685 686 if (item == null) { 688 return new Point(0, 0); 689 } 690 691 Rectangle itemBounds = item.getBounds(); 692 int x = itemBounds.x + itemBounds.width; 693 int y = itemBounds.y + itemBounds.height; 694 return new Point(x, y); 695 } 696 697 701 public void setSelection(int selection) { 702 tabFolder.setSelection(selection); 703 } 704 705 712 public Rectangle computeTrim(int i, int j, int k, int l) { 713 return tabFolder.computeTrim(i, j, k, l); 714 } 715 716 719 public void setUnselectedCloseVisible(boolean b) { 720 tabFolder.setUnselectedCloseVisible(b); 721 } 722 723 726 public void setSelectionForeground(Color fgColor) { 727 tabFolder.setSelectionForeground(fgColor); 728 } 729 730 731 738 public void setSelectionBackground(Color[] bgColors, int[] percentages, boolean vertical) { 739 tabFolder.setSelectionBackground(bgColors, percentages, vertical); 740 } 741 742 public CTabItem getItem(int idx) { 743 return tabFolder.getItem(idx); 744 } 745 746 public int getSelectionIndex() { 747 return tabFolder.getSelectionIndex(); 748 } 749 750 public int getTabHeight() { 751 return tabFolder.getTabHeight(); 752 } 753 754 public int indexOf(CTabItem toFind) { 755 return tabFolder.indexOf(toFind); 756 } 757 758 public void setTabHeight(int height) { 759 tabFolder.setTabHeight(height); 760 } 761 762 765 public int getItemCount() { 766 return tabFolder.getItemCount(); 767 } 768 769 772 public CTabItem[] getItems() { 773 return tabFolder.getItems(); 774 } 775 776 public CTabItem getItem(Point toGet) { 777 return tabFolder.getItem(toGet); 778 } 779 780 public CTabItem getSelection() { 781 return tabFolder.getSelection(); 782 } 783 784 787 public void setMinimizeVisible(boolean isVisible) { 788 tabFolder.setMinimizeVisible(isVisible); 789 minimizeVisible = isVisible; 790 } 791 792 802 public void setMinimumCharacters(int count) { 803 tabFolder.setMinimumCharacters(count); 804 } 805 806 809 public void setMaximizeVisible(boolean isVisible) { 810 tabFolder.setMaximizeVisible(isVisible); 811 maximizeVisible = isVisible; 812 } 813 814 817 public void setSimpleTab(boolean traditionalTab) { 818 tabFolder.setSimple(traditionalTab); 819 } 820 821 824 public void setUnselectedImageVisible(boolean b) { 825 tabFolder.setUnselectedImageVisible(b); 826 } 827 828 831 public void setSingleTab(boolean b) { 832 tabFolder.setSingle(b); 833 } 834 835 public void hideTitle() { 836 tabFolder.setTabHeight(0); 837 } 838 839 public ViewForm getViewForm() { 840 return viewForm; 841 } 842 843 853 public void setVisible(boolean visible) { 854 contentProxy.setVisible(visible); 855 viewFormTopCenterProxy.setVisible(visible); 856 viewFormTopLeftProxy.setVisible(visible); 857 viewFormTopRightProxy.setVisible(visible); 858 } 859 } 860 | Popular Tags |