1 7 8 package java.awt; 9 10 import java.util.Hashtable ; 11 12 107 public class BorderLayout implements LayoutManager2 , 108 java.io.Serializable { 109 119 int hgap; 120 121 130 int vgap; 131 132 142 Component north; 143 153 Component west; 154 164 Component east; 165 175 Component south; 176 186 Component center; 187 188 201 Component firstLine; 202 208 Component lastLine; 209 215 Component firstItem; 216 222 Component lastItem; 223 224 227 public static final String NORTH = "North"; 228 229 232 public static final String SOUTH = "South"; 233 234 237 public static final String EAST = "East"; 238 239 242 public static final String WEST = "West"; 243 244 247 public static final String CENTER = "Center"; 248 249 256 public static final String BEFORE_FIRST_LINE = "First"; 257 258 265 public static final String AFTER_LAST_LINE = "Last"; 266 267 274 public static final String BEFORE_LINE_BEGINS = "Before"; 275 276 283 public static final String AFTER_LINE_ENDS = "After"; 284 285 293 public static final String PAGE_START = BEFORE_FIRST_LINE; 294 295 303 public static final String PAGE_END = AFTER_LAST_LINE; 304 305 313 public static final String LINE_START = BEFORE_LINE_BEGINS; 314 315 323 public static final String LINE_END = AFTER_LINE_ENDS; 324 325 328 private static final long serialVersionUID = -8658291919501921765L; 329 330 334 public BorderLayout() { 335 this(0, 0); 336 } 337 338 346 public BorderLayout(int hgap, int vgap) { 347 this.hgap = hgap; 348 this.vgap = vgap; 349 } 350 351 355 public int getHgap() { 356 return hgap; 357 } 358 359 364 public void setHgap(int hgap) { 365 this.hgap = hgap; 366 } 367 368 372 public int getVgap() { 373 return vgap; 374 } 375 376 381 public void setVgap(int vgap) { 382 this.vgap = vgap; 383 } 384 385 404 public void addLayoutComponent(Component comp, Object constraints) { 405 synchronized (comp.getTreeLock()) { 406 if ((constraints == null) || (constraints instanceof String )) { 407 addLayoutComponent((String )constraints, comp); 408 } else { 409 throw new IllegalArgumentException ("cannot add to layout: constraint must be a string (or null)"); 410 } 411 } 412 } 413 414 417 @Deprecated 418 public void addLayoutComponent(String name, Component comp) { 419 synchronized (comp.getTreeLock()) { 420 421 if (name == null) { 422 name = "Center"; 423 } 424 425 427 if ("Center".equals(name)) { 428 center = comp; 429 } else if ("North".equals(name)) { 430 north = comp; 431 } else if ("South".equals(name)) { 432 south = comp; 433 } else if ("East".equals(name)) { 434 east = comp; 435 } else if ("West".equals(name)) { 436 west = comp; 437 } else if (BEFORE_FIRST_LINE.equals(name)) { 438 firstLine = comp; 439 } else if (AFTER_LAST_LINE.equals(name)) { 440 lastLine = comp; 441 } else if (BEFORE_LINE_BEGINS.equals(name)) { 442 firstItem = comp; 443 } else if (AFTER_LINE_ENDS.equals(name)) { 444 lastItem = comp; 445 } else { 446 throw new IllegalArgumentException ("cannot add to layout: unknown constraint: " + name); 447 } 448 } 449 } 450 451 460 public void removeLayoutComponent(Component comp) { 461 synchronized (comp.getTreeLock()) { 462 if (comp == center) { 463 center = null; 464 } else if (comp == north) { 465 north = null; 466 } else if (comp == south) { 467 south = null; 468 } else if (comp == east) { 469 east = null; 470 } else if (comp == west) { 471 west = null; 472 } 473 if (comp == firstLine) { 474 firstLine = null; 475 } else if (comp == lastLine) { 476 lastLine = null; 477 } else if (comp == firstItem) { 478 firstItem = null; 479 } else if (comp == lastItem) { 480 lastItem = null; 481 } 482 } 483 } 484 485 500 public Component getLayoutComponent(Object constraints) { 501 if (CENTER.equals(constraints)) { 502 return center; 503 } else if (NORTH.equals(constraints)) { 504 return north; 505 } else if (SOUTH.equals(constraints)) { 506 return south; 507 } else if (WEST.equals(constraints)) { 508 return west; 509 } else if (EAST.equals(constraints)) { 510 return east; 511 } else if (PAGE_START.equals(constraints)) { 512 return firstLine; 513 } else if (PAGE_END.equals(constraints)) { 514 return lastLine; 515 } else if (LINE_START.equals(constraints)) { 516 return firstItem; 517 } else if (LINE_END.equals(constraints)) { 518 return lastItem; 519 } else { 520 throw new IllegalArgumentException ("cannot get component: unknown constraint: " + constraints); 521 } 522 } 523 524 525 541 public Component getLayoutComponent(Container target, Object constraints) { 542 boolean ltr = target.getComponentOrientation().isLeftToRight(); 543 Component result = null; 544 545 if (NORTH.equals(constraints)) { 546 result = (firstLine != null) ? firstLine : north; 547 } else if (SOUTH.equals(constraints)) { 548 result = (lastLine != null) ? lastLine : south; 549 } else if (WEST.equals(constraints)) { 550 result = ltr ? firstItem : lastItem; 551 if (result == null) { 552 result = west; 553 } 554 } else if (EAST.equals(constraints)) { 555 result = ltr ? lastItem : firstItem; 556 if (result == null) { 557 result = east; 558 } 559 } else if (CENTER.equals(constraints)) { 560 result = center; 561 } else { 562 throw new IllegalArgumentException ("cannot get component: invalid constraint: " + constraints); 563 } 564 565 return result; 566 } 567 568 569 579 public Object getConstraints(Component comp) { 580 if (comp == center) { 581 return CENTER; 582 } else if (comp == north) { 583 return NORTH; 584 } else if (comp == south) { 585 return SOUTH; 586 } else if (comp == west) { 587 return WEST; 588 } else if (comp == east) { 589 return EAST; 590 } else if (comp == firstLine) { 591 return PAGE_START; 592 } else if (comp == lastLine) { 593 return PAGE_END; 594 } else if (comp == firstItem) { 595 return LINE_START; 596 } else if (comp == lastItem) { 597 return LINE_END; 598 } 599 return null; 600 } 601 602 616 public Dimension minimumLayoutSize(Container target) { 617 synchronized (target.getTreeLock()) { 618 Dimension dim = new Dimension (0, 0); 619 620 boolean ltr = target.getComponentOrientation().isLeftToRight(); 621 Component c = null; 622 623 if ((c=getChild(EAST,ltr)) != null) { 624 Dimension d = c.getMinimumSize(); 625 dim.width += d.width + hgap; 626 dim.height = Math.max(d.height, dim.height); 627 } 628 if ((c=getChild(WEST,ltr)) != null) { 629 Dimension d = c.getMinimumSize(); 630 dim.width += d.width + hgap; 631 dim.height = Math.max(d.height, dim.height); 632 } 633 if ((c=getChild(CENTER,ltr)) != null) { 634 Dimension d = c.getMinimumSize(); 635 dim.width += d.width; 636 dim.height = Math.max(d.height, dim.height); 637 } 638 if ((c=getChild(NORTH,ltr)) != null) { 639 Dimension d = c.getMinimumSize(); 640 dim.width = Math.max(d.width, dim.width); 641 dim.height += d.height + vgap; 642 } 643 if ((c=getChild(SOUTH,ltr)) != null) { 644 Dimension d = c.getMinimumSize(); 645 dim.width = Math.max(d.width, dim.width); 646 dim.height += d.height + vgap; 647 } 648 649 Insets insets = target.getInsets(); 650 dim.width += insets.left + insets.right; 651 dim.height += insets.top + insets.bottom; 652 653 return dim; 654 } 655 } 656 657 672 public Dimension preferredLayoutSize(Container target) { 673 synchronized (target.getTreeLock()) { 674 Dimension dim = new Dimension (0, 0); 675 676 boolean ltr = target.getComponentOrientation().isLeftToRight(); 677 Component c = null; 678 679 if ((c=getChild(EAST,ltr)) != null) { 680 Dimension d = c.getPreferredSize(); 681 dim.width += d.width + hgap; 682 dim.height = Math.max(d.height, dim.height); 683 } 684 if ((c=getChild(WEST,ltr)) != null) { 685 Dimension d = c.getPreferredSize(); 686 dim.width += d.width + hgap; 687 dim.height = Math.max(d.height, dim.height); 688 } 689 if ((c=getChild(CENTER,ltr)) != null) { 690 Dimension d = c.getPreferredSize(); 691 dim.width += d.width; 692 dim.height = Math.max(d.height, dim.height); 693 } 694 if ((c=getChild(NORTH,ltr)) != null) { 695 Dimension d = c.getPreferredSize(); 696 dim.width = Math.max(d.width, dim.width); 697 dim.height += d.height + vgap; 698 } 699 if ((c=getChild(SOUTH,ltr)) != null) { 700 Dimension d = c.getPreferredSize(); 701 dim.width = Math.max(d.width, dim.width); 702 dim.height += d.height + vgap; 703 } 704 705 Insets insets = target.getInsets(); 706 dim.width += insets.left + insets.right; 707 dim.height += insets.top + insets.bottom; 708 709 return dim; 710 } 711 } 712 713 721 public Dimension maximumLayoutSize(Container target) { 722 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 723 } 724 725 732 public float getLayoutAlignmentX(Container parent) { 733 return 0.5f; 734 } 735 736 743 public float getLayoutAlignmentY(Container parent) { 744 return 0.5f; 745 } 746 747 751 public void invalidateLayout(Container target) { 752 } 753 754 773 public void layoutContainer(Container target) { 774 synchronized (target.getTreeLock()) { 775 Insets insets = target.getInsets(); 776 int top = insets.top; 777 int bottom = target.height - insets.bottom; 778 int left = insets.left; 779 int right = target.width - insets.right; 780 781 boolean ltr = target.getComponentOrientation().isLeftToRight(); 782 Component c = null; 783 784 if ((c=getChild(NORTH,ltr)) != null) { 785 c.setSize(right - left, c.height); 786 Dimension d = c.getPreferredSize(); 787 c.setBounds(left, top, right - left, d.height); 788 top += d.height + vgap; 789 } 790 if ((c=getChild(SOUTH,ltr)) != null) { 791 c.setSize(right - left, c.height); 792 Dimension d = c.getPreferredSize(); 793 c.setBounds(left, bottom - d.height, right - left, d.height); 794 bottom -= d.height + vgap; 795 } 796 if ((c=getChild(EAST,ltr)) != null) { 797 c.setSize(c.width, bottom - top); 798 Dimension d = c.getPreferredSize(); 799 c.setBounds(right - d.width, top, d.width, bottom - top); 800 right -= d.width + hgap; 801 } 802 if ((c=getChild(WEST,ltr)) != null) { 803 c.setSize(c.width, bottom - top); 804 Dimension d = c.getPreferredSize(); 805 c.setBounds(left, top, d.width, bottom - top); 806 left += d.width + hgap; 807 } 808 if ((c=getChild(CENTER,ltr)) != null) { 809 c.setBounds(left, top, right - left, bottom - top); 810 } 811 } 812 } 813 814 821 private Component getChild(String key, boolean ltr) { 822 Component result = null; 823 824 if (key == NORTH) { 825 result = (firstLine != null) ? firstLine : north; 826 } 827 else if (key == SOUTH) { 828 result = (lastLine != null) ? lastLine : south; 829 } 830 else if (key == WEST) { 831 result = ltr ? firstItem : lastItem; 832 if (result == null) { 833 result = west; 834 } 835 } 836 else if (key == EAST) { 837 result = ltr ? lastItem : firstItem; 838 if (result == null) { 839 result = east; 840 } 841 } 842 else if (key == CENTER) { 843 result = center; 844 } 845 if (result != null && !result.visible) { 846 result = null; 847 } 848 return result; 849 } 850 851 855 public String toString() { 856 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]"; 857 } 858 } 859 | Popular Tags |