| 1 7 package java.awt; 8 9 import java.util.Hashtable ; 10 import java.util.Vector ; 11 12 class GridBagLayoutInfo implements java.io.Serializable { 13 int width, height; 14 int startx, starty; 15 int minWidth[]; 16 int minHeight[]; 17 double weightX[]; 18 double weightY[]; 19 20 GridBagLayoutInfo () { 21 22 minWidth = new int[GridBagLayout.MAXGRIDSIZE]; 23 minHeight = new int[GridBagLayout.MAXGRIDSIZE]; 24 weightX = new double[GridBagLayout.MAXGRIDSIZE]; 25 weightY = new double[GridBagLayout.MAXGRIDSIZE]; 26 } 27 } 28 29 278 public class GridBagLayout implements LayoutManager2 , 279 java.io.Serializable { 280 281 282 protected static final int MAXGRIDSIZE = 512; 283 284 287 protected static final int MINSIZE = 1; 288 291 protected static final int PREFERREDSIZE = 2; 292 293 302 protected Hashtable <Component ,GridBagConstraints > comptable; 303 304 316 protected GridBagConstraints defaultConstraints; 317 318 331 protected GridBagLayoutInfo layoutInfo; 332 333 345 public int columnWidths[]; 346 347 359 public int rowHeights[]; 360 361 374 public double columnWeights[]; 375 376 389 public double rowWeights[]; 390 391 394 public GridBagLayout () { 395 comptable = new Hashtable <Component ,GridBagConstraints >(); 396 defaultConstraints = new GridBagConstraints (); 397 } 398 399 404 public void setConstraints(Component comp, GridBagConstraints constraints) { 405 comptable.put(comp, (GridBagConstraints )constraints.clone()); 406 } 407 408 416 public GridBagConstraints getConstraints(Component comp) { 417 GridBagConstraints constraints = comptable.get(comp); 418 if (constraints == null) { 419 setConstraints(comp, defaultConstraints); 420 constraints = comptable.get(comp); 421 } 422 return (GridBagConstraints )constraints.clone(); 423 } 424 425 438 protected GridBagConstraints lookupConstraints(Component comp) { 439 GridBagConstraints constraints = comptable.get(comp); 440 if (constraints == null) { 441 setConstraints(comp, defaultConstraints); 442 constraints = comptable.get(comp); 443 } 444 return constraints; 445 } 446 447 451 private void removeConstraints(Component comp) { 452 comptable.remove(comp); 453 } 454 455 467 public Point getLayoutOrigin () { 468 Point origin = new Point (0,0); 469 if (layoutInfo != null) { 470 origin.x = layoutInfo.startx; 471 origin.y = layoutInfo.starty; 472 } 473 return origin; 474 } 475 476 485 public int [][] getLayoutDimensions () { 486 if (layoutInfo == null) 487 return new int[2][0]; 488 489 int dim[][] = new int [2][]; 490 dim[0] = new int[layoutInfo.width]; 491 dim[1] = new int[layoutInfo.height]; 492 493 System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width); 494 System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height); 495 496 return dim; 497 } 498 499 511 public double [][] getLayoutWeights () { 512 if (layoutInfo == null) 513 return new double[2][0]; 514 515 double weights[][] = new double [2][]; 516 weights[0] = new double[layoutInfo.width]; 517 weights[1] = new double[layoutInfo.height]; 518 519 System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width); 520 System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height); 521 522 return weights; 523 } 524 525 552 public Point location(int x, int y) { 553 Point loc = new Point (0,0); 554 int i, d; 555 556 if (layoutInfo == null) 557 return loc; 558 559 d = layoutInfo.startx; 560 if (!rightToLeft) { 561 for (i=0; i<layoutInfo.width; i++) { 562 d += layoutInfo.minWidth[i]; 563 if (d > x) 564 break; 565 } 566 } else { 567 for (i=layoutInfo.width-1; i>=0; i--) { 568 if (d > x) 569 break; 570 d += layoutInfo.minWidth[i]; 571 } 572 i++; 573 } 574 loc.x = i; 575 576 d = layoutInfo.starty; 577 for (i=0; i<layoutInfo.height; i++) { 578 d += layoutInfo.minHeight[i]; 579 if (d > y) 580 break; 581 } 582 loc.y = i; 583 584 return loc; 585 } 586 587 590 public void addLayoutComponent(String name, Component comp) { 591 } 592 593 604 public void addLayoutComponent(Component comp, Object constraints) { 605 if (constraints instanceof GridBagConstraints ) { 606 setConstraints(comp, (GridBagConstraints )constraints); 607 } else if (constraints != null) { 608 throw new IllegalArgumentException ("cannot add to layout: constraints must be a GridBagConstraint"); 609 } 610 } 611 612 620 public void removeLayoutComponent(Component comp) { 621 removeConstraints(comp); 622 } 623 624 635 public Dimension preferredLayoutSize(Container parent) { 636 GridBagLayoutInfo info = getLayoutInfo(parent, PREFERREDSIZE); 637 return getMinSize(parent, info); 638 } 639 640 649 public Dimension minimumLayoutSize(Container parent) { 650 GridBagLayoutInfo info = getLayoutInfo(parent, MINSIZE); 651 return getMinSize(parent, info); 652 } 653 654 663 public Dimension maximumLayoutSize(Container target) { 664 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 665 } 666 667 676 public float getLayoutAlignmentX(Container parent) { 677 return 0.5f; 678 } 679 680 689 public float getLayoutAlignmentY(Container parent) { 690 return 0.5f; 691 } 692 693 697 public void invalidateLayout(Container target) { 698 } 699 700 711 public void layoutContainer(Container parent) { 712 arrangeGrid(parent); 713 } 714 715 719 public String toString() { 720 return getClass().getName(); 721 } 722 723 726 727 746 747 750 751 791 792 815 protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) { 816 return GetLayoutInfo(parent, sizeflag); 817 } 818 819 827 protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) { 828 synchronized (parent.getTreeLock()) { 829 GridBagLayoutInfo r = new GridBagLayoutInfo (); 830 Component comp; 831 GridBagConstraints constraints; 832 Dimension d; 833 Component components[] = parent.getComponents(); 834 835 int compindex, i, j, k, px, py, pixels_diff, nextSize; 836 int curX, curY, curWidth, curHeight, curRow, curCol; 837 double weight_diff, weight, start, size; 838 int xMax[], yMax[]; 839 840 846 847 r.width = r.height = 0; 848 curRow = curCol = -1; 849 xMax = new int[MAXGRIDSIZE]; 850 yMax = new int[MAXGRIDSIZE]; 851 852 for (compindex = 0 ; compindex < components.length ; compindex++) { 853 comp = components[compindex]; 854 if (!comp.isVisible()) 855 continue; 856 constraints = lookupConstraints(comp); 857 858 curX = constraints.gridx; 859 curY = constraints.gridy; 860 curWidth = constraints.gridwidth; 861 if (curWidth <= 0) 862 curWidth = 1; 863 curHeight = constraints.gridheight; 864 if (curHeight <= 0) 865 curHeight = 1; 866 867 868 if (curX < 0 && curY < 0) { 869 if (curRow >= 0) 870 curY = curRow; 871 else if (curCol >= 0) 872 curX = curCol; 873 else 874 curY = 0; 875 } 876 if (curX < 0) { 877 px = 0; 878 for (i = curY; i < (curY + curHeight); i++) { 879 px = Math.max(px, xMax[i]); 880 } 881 882 curX = px - curX - 1; 883 if(curX < 0) 884 curX = 0; 885 } 886 else if (curY < 0) { 887 py = 0; 888 for (i = curX; i < (curX + curWidth); i++) { 889 py = Math.max(py, yMax[i]); 890 } 891 892 curY = py - curY - 1; 893 if(curY < 0) 894 curY = 0; 895 } 896 897 898 for (px = curX + curWidth; r.width < px; r.width++); 899 for (py = curY + curHeight; r.height < py; r.height++); 900 901 902 for (i = curX; i < (curX + curWidth); i++) { 903 yMax[i] = py; 904 } 905 for (i = curY; i < (curY + curHeight); i++) { 906 xMax[i] = px; 907 } 908 909 910 if (sizeflag == PREFERREDSIZE) 911 d = comp.getPreferredSize(); 912 else 913 d = comp.getMinimumSize(); 914 constraints.minWidth = d.width; 915 constraints.minHeight = d.height; 916 917 919 if (constraints.gridheight == 0 && constraints.gridwidth == 0) 920 curRow = curCol = -1; 921 922 923 if (constraints.gridheight == 0 && curRow < 0) 924 curCol = curX + curWidth; 925 926 927 else if (constraints.gridwidth == 0 && curCol < 0) 928 curRow = curY + curHeight; 929 } 930 931 934 if (columnWidths != null && r.width < columnWidths.length) 935 r.width = columnWidths.length; 936 if (rowHeights != null && r.height < rowHeights.length) 937 r.height = rowHeights.length; 938 939 940 948 949 curRow = curCol = -1; 950 xMax = new int[MAXGRIDSIZE]; 951 yMax = new int[MAXGRIDSIZE]; 952 953 for (compindex = 0 ; compindex < components.length ; compindex++) { 954 comp = components[compindex]; 955 if (!comp.isVisible()) 956 continue; 957 constraints = lookupConstraints(comp); 958 959 curX = constraints.gridx; 960 curY = constraints.gridy; 961 curWidth = constraints.gridwidth; 962 curHeight = constraints.gridheight; 963 964 965 if (curX < 0 && curY < 0) { 966 if(curRow >= 0) 967 curY = curRow; 968 else if(curCol >= 0) 969 curX = curCol; 970 else 971 curY = 0; 972 } 973 974 if (curX < 0) { 975 if (curHeight <= 0) { 976 curHeight += r.height - curY; 977 if (curHeight < 1) 978 curHeight = 1; 979 } 980 981 px = 0; 982 for (i = curY; i < (curY + curHeight); i++) 983 px = Math.max(px, xMax[i]); 984 985 curX = px - curX - 1; 986 if(curX < 0) 987 curX = 0; 988 } 989 else if (curY < 0) { 990 if (curWidth <= 0) { 991 curWidth += r.width - curX; 992 if (curWidth < 1) 993 curWidth = 1; 994 } 995 996 py = 0; 997 for (i = curX; i < (curX + curWidth); i++) 998 py = Math.max(py, yMax[i]); 999 1000 curY = py - curY - 1; 1001 if(curY < 0) 1002 curY = 0; 1003 } 1004 1005 if (curWidth <= 0) { 1006 curWidth += r.width - curX; 1007 if (curWidth < 1) 1008 curWidth = 1; 1009 } 1010 1011 if (curHeight <= 0) { 1012 curHeight += r.height - curY; 1013 if (curHeight < 1) 1014 curHeight = 1; 1015 } 1016 1017 px = curX + curWidth; 1018 py = curY + curHeight; 1019 1020 for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; } 1021 for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; } 1022 1023 1024 if (constraints.gridheight == 0 && constraints.gridwidth == 0) 1025 curRow = curCol = -1; 1026 if (constraints.gridheight == 0 && curRow < 0) 1027 curCol = curX + curWidth; 1028 else if (constraints.gridwidth == 0 && curCol < 0) 1029 curRow = curY + curHeight; 1030 1031 1032 constraints.tempX = curX; 1033 constraints.tempY = curY; 1034 constraints.tempWidth = curWidth; 1035 constraints.tempHeight = curHeight; 1036 } 1037 1038 1041 if (columnWidths != null) 1042 System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length); 1043 if (rowHeights != null) 1044 System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length); 1045 if (columnWeights != null) 1046 System.arraycopy(columnWeights, 0, r.weightX, 0, Math.min(r.weightX.length, columnWeights.length)); 1047 if (rowWeights != null) 1048 System.arraycopy(rowWeights, 0, r.weightY, 0, Math.min(r.weightY.length, rowWeights.length)); 1049 1050 1055 1056 nextSize = Integer.MAX_VALUE; 1057 1058 for (i = 1; 1059 i != Integer.MAX_VALUE; 1060 i = nextSize, nextSize = Integer.MAX_VALUE) { 1061 for (compindex = 0 ; compindex < components.length ; compindex++) { 1062 comp = components[compindex]; 1063 if (!comp.isVisible()) 1064 continue; 1065 constraints = lookupConstraints(comp); 1066 1067 if (constraints.tempWidth ==
|