1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.CBanner; 19 import org.eclipse.swt.events.ControlEvent; 20 import org.eclipse.swt.events.ControlListener; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.CoolBar; 26 import org.eclipse.swt.widgets.CoolItem; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Layout; 29 import org.eclipse.ui.internal.layout.TrimCommonUIHandle; 30 31 43 public class WorkbenchLayout extends Layout { 44 private static int defaultMargin = 5; 45 46 53 private class TrimLine { 54 57 List controls = new ArrayList (); 58 59 62 List computedSizes = new ArrayList (); 63 64 67 int minorMax = 0; 68 69 74 int resizableCount = 0; 75 76 79 int extraSpace = 0; 80 } 81 82 94 private class LeftBannerLayout extends Layout { 95 96 102 protected Point computeSize(Composite composite, int wHint, int hHint, 103 boolean flushCache) { 104 return new Point(wHint, WorkbenchLayout.this.topMax); 107 } 108 109 115 protected void layout(Composite composite, boolean flushCache) { 116 } 117 118 } 119 120 public static final String TRIMID_CMD_PRIMARY = "Command Primary"; 123 public static final String TRIMID_CMD_SECONDARY = "Command Secondary"; 125 public static final String TRIMID_VERTICAL1 = "vertical1"; 127 public static final String TRIMID_VERTICAL2 = "vertical2"; 129 public static final String TRIMID_STATUS = "Status"; 131 public static final String TRIMID_CENTER = "Center"; 133 public CBanner banner; 135 136 private int topMax; 137 138 public Composite centerComposite; 140 141 private int spacing = 0; 143 144 private TrimArea cmdPrimaryTrimArea; 146 147 private TrimArea cmdSecondaryTrimArea; 148 149 private TrimArea leftTrimArea; 150 151 private TrimArea rightTrimArea; 152 153 private TrimArea bottomTrimArea; 154 155 private int horizontalHandleSize = -1; 157 158 private int verticalHandleSize = -1; 159 160 private List dragHandles; 161 162 private static Composite layoutComposite; 164 165 private static Rectangle clientRect; 166 167 171 public WorkbenchLayout() { 172 super(); 173 174 cmdPrimaryTrimArea = new TrimArea(TRIMID_CMD_PRIMARY, SWT.TOP, 176 defaultMargin); 177 cmdSecondaryTrimArea = new TrimArea(TRIMID_CMD_SECONDARY, SWT.TOP, 178 defaultMargin); 179 leftTrimArea = new TrimArea(TRIMID_VERTICAL1, SWT.LEFT, defaultMargin); 180 rightTrimArea = new TrimArea(TRIMID_VERTICAL2, SWT.RIGHT, defaultMargin); 181 bottomTrimArea = new TrimArea(TRIMID_STATUS, SWT.BOTTOM, defaultMargin); 182 183 dragHandles = new ArrayList (); 185 } 186 187 194 public void createCBanner(Composite workbenchComposite) { 195 banner = new CBanner(workbenchComposite, SWT.NONE); 196 banner.setSimple(false); 197 banner.setRightWidth(175); 198 banner.setLocation(0, 0); 199 200 Composite bannerLeft = new Composite(banner, SWT.NONE) { 203 209 public Point computeSize(int wHint, int hHint, boolean changed) { 210 if (WorkbenchLayout.layoutComposite != null) { 213 return WorkbenchLayout.this.computeSize(TRIMID_CMD_PRIMARY, 214 wHint); 215 } 216 217 return super.computeSize(wHint, hHint, changed); 218 } 219 }; 220 bannerLeft.setLayout(new LeftBannerLayout()); 221 bannerLeft.setBackground(workbenchComposite.getDisplay() 222 .getSystemColor(SWT.COLOR_DARK_BLUE)); 223 banner.setLeft(bannerLeft); 224 225 Composite bannerRight = new Composite(banner, SWT.NONE) { 227 233 public Point computeSize(int wHint, int hHint, boolean changed) { 234 if (WorkbenchLayout.layoutComposite != null) { 237 return WorkbenchLayout.this.computeSize( 238 TRIMID_CMD_SECONDARY, wHint); 239 } 240 241 return super.computeSize(wHint, hHint, changed); 242 } 243 }; 244 bannerRight.setBackground(workbenchComposite.getDisplay() 245 .getSystemColor(SWT.COLOR_DARK_BLUE)); 246 banner.setRight(bannerRight); 247 248 bannerRight.addControlListener(new ControlListener() { 251 public void controlMoved(ControlEvent e) { 252 } 253 254 public void controlResized(ControlEvent e) { 255 Composite leftComp = (Composite) e.widget; 256 leftComp.getShell().layout(true); 257 } 258 }); 259 260 banner.moveBelow(null); 262 } 263 264 273 protected Point computeSize(Composite composite, int wHint, int hHint, 274 boolean flushCache) { 275 Point size = new Point(wHint, hHint); 276 if (size.x == SWT.DEFAULT) { 277 size.x = 300; 278 } 279 if (size.y == SWT.DEFAULT) { 280 size.y = 300; 281 } 282 return size; 283 } 284 285 293 protected void layout(Composite composite, boolean flushCache) { 294 layoutComposite = composite; 295 clientRect = composite.getClientArea(); 296 297 resetDragHandles(); 299 300 if (useCBanner()) { 303 banner.moveBelow(null); 304 305 Point bannerSize = banner 309 .computeSize(clientRect.width, SWT.DEFAULT); 310 311 topMax = bannerSize.y; 313 314 banner.setSize(bannerSize); 315 } else { 316 Point c1Size = computeSize(TRIMID_CMD_PRIMARY, clientRect.width); 317 Point c2Size = computeSize(TRIMID_CMD_SECONDARY, clientRect.width); 318 319 topMax = c1Size.y + c2Size.y; 321 } 322 323 Point v1Size = computeSize(TRIMID_VERTICAL1, clientRect.height - topMax); 326 Point v2Size = computeSize(TRIMID_VERTICAL2, clientRect.height - topMax); 327 328 computeSize(TRIMID_STATUS, clientRect.width - (v1Size.x + v2Size.x)); 331 332 if (useCBanner()) { 335 Point leftLoc = banner.getLeft().getLocation(); 336 cmdPrimaryTrimArea.areaBounds.x = leftLoc.x; 337 cmdPrimaryTrimArea.areaBounds.y = leftLoc.y; 338 } else { 339 cmdPrimaryTrimArea.areaBounds.x = 0; 340 cmdPrimaryTrimArea.areaBounds.y = 0; 341 } 342 layoutTrim(cmdPrimaryTrimArea, cmdPrimaryTrimArea.areaBounds); 343 344 if (useCBanner()) { 346 Point rightLoc = banner.getRight().getLocation(); 347 cmdSecondaryTrimArea.areaBounds.x = rightLoc.x; 348 cmdSecondaryTrimArea.areaBounds.y = rightLoc.y; 349 } else { 350 cmdSecondaryTrimArea.areaBounds.x = 0; 351 cmdSecondaryTrimArea.areaBounds.y = cmdPrimaryTrimArea.areaBounds.height; 352 } 353 layoutTrim(cmdSecondaryTrimArea, cmdSecondaryTrimArea.areaBounds); 354 355 leftTrimArea.areaBounds.x = 0; 356 leftTrimArea.areaBounds.y = topMax; 357 layoutTrim(leftTrimArea, leftTrimArea.areaBounds); 358 359 rightTrimArea.areaBounds.x = clientRect.width 360 - rightTrimArea.areaBounds.width; 361 rightTrimArea.areaBounds.y = topMax; 362 layoutTrim(rightTrimArea, rightTrimArea.areaBounds); 363 364 bottomTrimArea.areaBounds.x = leftTrimArea.areaBounds.width; 365 bottomTrimArea.areaBounds.y = clientRect.height 366 - bottomTrimArea.areaBounds.height; 367 layoutTrim(bottomTrimArea, bottomTrimArea.areaBounds); 368 369 layoutCenter(); 372 } 373 374 380 private boolean useCBanner() { 381 return (banner != null && banner.getVisible()); 382 } 383 384 389 private TrimArea getTrimArea(String areaId) { 390 if (TRIMID_CMD_PRIMARY.equals(areaId)) { 391 return cmdPrimaryTrimArea; 392 } 393 if (TRIMID_CMD_SECONDARY.equals(areaId)) { 394 return cmdSecondaryTrimArea; 395 } 396 if (TRIMID_VERTICAL1.equals(areaId)) { 397 return leftTrimArea; 398 } 399 if (TRIMID_VERTICAL2.equals(areaId)) { 400 return rightTrimArea; 401 } 402 if (TRIMID_STATUS.equals(areaId)) { 403 return bottomTrimArea; 404 } 405 406 return null; 407 } 408 409 415 private List getTrimContents(String areaId) { 416 List trimContents = new ArrayList (); 417 Control[] children = layoutComposite.getChildren(); 418 for (int i = 0; i < children.length; i++) { 419 if (children[i].isDisposed() || !children[i].getVisible()) { 421 continue; 422 } 423 424 if (children[i].getLayoutData() instanceof TrimLayoutData) { 427 TrimLayoutData tlData = (TrimLayoutData) children[i] 428 .getLayoutData(); 429 if (tlData.areaId.equals(areaId)) { 430 trimContents.add(children[i]); 431 } 432 } 433 } 434 435 return trimContents; 436 } 437 438 442 private void layoutCenter() { 443 if (centerComposite != null) { 444 Rectangle areaBounds = new Rectangle( 446 leftTrimArea.areaBounds.x + leftTrimArea.areaBounds.width, 447 topMax, 448 clientRect.width 449 - (leftTrimArea.areaBounds.width + rightTrimArea.areaBounds.width), 450 clientRect.height 451 - (topMax + bottomTrimArea.areaBounds.height)); 452 453 centerComposite.setBounds(areaBounds); 454 } 455 } 456 457 475 private Point computeSize(String areaId, int majorHint) { 476 TrimArea trimArea = getTrimArea(areaId); 477 boolean horizontal = trimArea.orientation == SWT.TOP 478 || trimArea.orientation == SWT.BOTTOM; 479 480 trimArea.trimContents = getTrimContents(trimArea.areaId); 482 483 trimArea.trimLines = computeWrappedTrim(trimArea, majorHint); 486 487 int minorMax = 0; 489 for (Iterator iter = trimArea.trimLines.iterator(); iter.hasNext();) { 490 TrimLine curLine = (TrimLine) iter.next(); 491 minorMax += curLine.minorMax; 492 } 493 minorMax = Math.max(minorMax, trimArea.defaultMinor); 494 495 Point computedSize = new Point(0, 0); 497 if (horizontal) { 498 computedSize.x = trimArea.areaBounds.width = majorHint; 499 computedSize.y = trimArea.areaBounds.height = minorMax; 500 } else { 501 computedSize.x = trimArea.areaBounds.width = minorMax; 502 computedSize.y = trimArea.areaBounds.height = majorHint; 503 } 504 505 return computedSize; 507 } 508 509 524 private List computeWrappedTrim(TrimArea trimArea, int majorHint) { 525 boolean horizontal = trimArea.orientation == SWT.TOP 526 || trimArea.orientation == SWT.BOTTOM; 527 List lines = new ArrayList (); 529 TrimLine curLine = new TrimLine(); 530 lines.add(curLine); 531 curLine.minorMax = trimArea.defaultMinor; 532 533 int tilePos = 0; 535 for (Iterator iter = trimArea.trimContents.iterator(); iter.hasNext();) { 536 Control control = (Control) iter.next(); 537 TrimLayoutData td = (TrimLayoutData) control.getLayoutData(); 538 539 Point prefSize; 540 if (horizontal) { 541 prefSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT); 543 } else { 544 prefSize = control.computeSize(SWT.DEFAULT, majorHint); 545 } 546 547 int curTileSize = horizontal ? prefSize.x : prefSize.y; 549 550 if (curLine.controls.size() > 0) { 553 curTileSize += spacing; 554 } 555 556 if (td.listener != null) { 560 curTileSize += horizontal ? horizontalHandleSize 561 : verticalHandleSize; 562 } 563 564 if ((tilePos + curTileSize) <= majorHint 570 || curLine.controls.size() == 0) { 571 curLine.controls.add(control); 572 573 int minorSize = horizontal ? prefSize.y : prefSize.x; 575 if (minorSize > curLine.minorMax) { 576 curLine.minorMax = minorSize; 577 } 578 579 tilePos += curTileSize; 580 } else { 581 curLine.extraSpace = majorHint - tilePos; 583 584 curLine = new TrimLine(); 586 lines.add(curLine); 587 588 curLine.controls.add(control); 589 590 int minorSize = horizontal ? prefSize.y : prefSize.x; 592 if (minorSize > curLine.minorMax) { 593 curLine.minorMax = minorSize; 594 } 595 596 tilePos = curTileSize; 597 } 598 599 if ((td.flags & TrimLayoutData.GROWABLE) != 0) { 601 curLine.resizableCount++; 602 } 603 } 604 605 curLine.extraSpace = majorHint - tilePos; 607 608 return lines; 609 } 610 611 private void layoutTrim(TrimArea trimArea, Rectangle areaBounds) { 612 boolean horizontal = trimArea.orientation == SWT.TOP 613 || trimArea.orientation == SWT.BOTTOM; 614 615 int areaMajor = horizontal ? areaBounds.width : areaBounds.height; 617 618 List tileAreas = trimArea.trimLines; 620 621 int tilePosMinor = horizontal ? areaBounds.y : areaBounds.x; 623 for (Iterator areaIter = tileAreas.iterator(); areaIter.hasNext();) { 624 TrimLine curLine = (TrimLine) areaIter.next(); 625 626 int resizePadding = 0; 632 if (curLine.resizableCount > 0 && curLine.extraSpace > 0) { 633 resizePadding = curLine.extraSpace / curLine.resizableCount; 634 } 635 636 int tilePosMajor = horizontal ? areaBounds.x : areaBounds.y; 638 for (Iterator iter = curLine.controls.iterator(); iter.hasNext();) { 639 Control control = (Control) iter.next(); 640 TrimLayoutData td = (TrimLayoutData) control.getLayoutData(); 641 Point prefSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT); 642 643 int major = horizontal ? prefSize.x : prefSize.y; 644 int minor = horizontal ? prefSize.y : prefSize.x; 645 646 if (major > areaMajor) { 649 major = areaMajor; 650 } 651 652 if ((td.flags & TrimLayoutData.GRAB_EXCESS_MINOR) != 0) { 655 minor = curLine.minorMax; 656 } 657 658 if ((td.flags & TrimLayoutData.GROWABLE) != 0) { 661 major += resizePadding; 662 } 663 664 if (td.listener != null && createHandles()) { 666 TrimCommonUIHandle handle = getDragHandle(trimArea.orientation); 667 669 if (horizontal) { 670 handle.setBounds(tilePosMajor, tilePosMinor, 671 getHandleSize(true), curLine.minorMax); 672 } else { 673 handle.setBounds(tilePosMinor, tilePosMajor, 674 curLine.minorMax, getHandleSize(false)); 675 } 676 677 tilePosMajor += horizontal ? getHandleSize(true) 678 : getHandleSize(false); 679 } 680 681 switch (trimArea.orientation) { 683 case SWT.TOP: 684 control.setBounds(tilePosMajor, tilePosMinor, major, minor); 685 break; 686 case SWT.LEFT: 687 control.setBounds(tilePosMinor, tilePosMajor, minor, major); 688 break; 689 case SWT.RIGHT: 690 int rightEdge = tilePosMinor + curLine.minorMax; 692 control.setBounds(rightEdge - minor, tilePosMajor, minor, 693 major); 694 break; 695 case SWT.BOTTOM: 696 int bottomEdge = tilePosMinor + curLine.minorMax; 698 control.setBounds(tilePosMajor, bottomEdge - minor, major, 699 minor); 700 break; 701 } 702 703 tilePosMajor += major + spacing; 705 } 706 tilePosMinor += curLine.minorMax; 707 tilePosMajor = horizontal ? areaBounds.x : areaBounds.y; 708 } 709 } 710 711 715 private boolean createHandles() { 716 return false; 717 } 718 719 private void resetDragHandles() { 720 for (Iterator iter = dragHandles.iterator(); iter.hasNext();) { 721 } 724 } 725 726 private TrimCommonUIHandle getDragHandle(int orientation) { 727 730 for (Iterator iter = dragHandles.iterator(); iter.hasNext();) { 731 TrimCommonUIHandle handle = (TrimCommonUIHandle) iter.next(); 732 return handle; 734 } 735 736 System.out.println("new Handle"); return null; 744 } 745 746 753 public static int getOrientation(String areaId) { 754 if (TRIMID_CMD_PRIMARY.equals(areaId)) { 755 return SWT.TOP; 756 } 757 if (TRIMID_CMD_SECONDARY.equals(areaId)) { 758 return SWT.TOP; 759 } 760 if (TRIMID_VERTICAL1.equals(areaId)) { 761 return SWT.LEFT; 762 } 763 if (TRIMID_VERTICAL2.equals(areaId)) { 764 return SWT.RIGHT; 765 } 766 if (TRIMID_STATUS.equals(areaId)) { 767 return SWT.BOTTOM; 768 } 769 770 return SWT.NONE; 771 } 772 773 779 private int getHandleSize(boolean horizontal) { 780 if (horizontal && horizontalHandleSize != -1) { 782 return horizontalHandleSize; 783 } 784 785 if (!horizontal && verticalHandleSize != -1) { 786 return verticalHandleSize; 787 } 788 789 CoolBar bar = new CoolBar(layoutComposite, horizontal ? SWT.HORIZONTAL 791 : SWT.VERTICAL); 792 793 CoolItem item = new CoolItem(bar, SWT.NONE); 794 795 Label ctrl = new Label(bar, SWT.PUSH); 796 ctrl.setText("Button 1"); Point size = ctrl.computeSize(SWT.DEFAULT, SWT.DEFAULT); 798 799 Point ps = item.computeSize(size.x, size.y); 800 item.setPreferredSize(ps); 801 item.setControl(ctrl); 802 803 bar.pack(); 804 805 Point bl = ctrl.getLocation(); 808 Point cl = bar.getLocation(); 809 810 ctrl.dispose(); 812 item.dispose(); 813 bar.dispose(); 814 815 int length; 818 if (horizontal) { 819 length = bl.x - cl.x; 820 horizontalHandleSize = length; 821 } else { 822 length = bl.y - cl.y; 823 verticalHandleSize = length; 824 } 825 826 return length; 827 } 828 829 } 830 | Popular Tags |