home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- import java.util.Hashtable;
-
- public class GridBagLayout implements LayoutManager {
- protected static final int MAXGRIDSIZE = 128;
- protected static final int MINSIZE = 1;
- protected static final int PREFERREDSIZE = 2;
- protected Hashtable comptable = new Hashtable();
- protected GridBagConstraints defaultConstraints = new GridBagConstraints();
- protected GridBagLayoutInfo layoutInfo;
- public int[] columnWidths;
- public int[] rowHeights;
- public double[] columnWeights;
- public double[] rowWeights;
-
- public void setConstraints(Component comp, GridBagConstraints constraints) {
- this.comptable.put(comp, constraints.clone());
- }
-
- public GridBagConstraints getConstraints(Component comp) {
- GridBagConstraints constraints = (GridBagConstraints)this.comptable.get(comp);
- if (constraints == null) {
- this.setConstraints(comp, this.defaultConstraints);
- constraints = (GridBagConstraints)this.comptable.get(comp);
- }
-
- return (GridBagConstraints)constraints.clone();
- }
-
- protected GridBagConstraints lookupConstraints(Component comp) {
- GridBagConstraints constraints = (GridBagConstraints)this.comptable.get(comp);
- if (constraints == null) {
- this.setConstraints(comp, this.defaultConstraints);
- constraints = (GridBagConstraints)this.comptable.get(comp);
- }
-
- return constraints;
- }
-
- public Point getLayoutOrigin() {
- Point origin = new Point(0, 0);
- if (this.layoutInfo != null) {
- origin.x = this.layoutInfo.startx;
- origin.y = this.layoutInfo.starty;
- }
-
- return origin;
- }
-
- public int[][] getLayoutDimensions() {
- if (this.layoutInfo == null) {
- return new int[2][0];
- } else {
- int[][] dim = new int[2][];
- dim[0] = new int[this.layoutInfo.width];
- dim[1] = new int[this.layoutInfo.height];
- System.arraycopy(this.layoutInfo.minWidth, 0, dim[0], 0, this.layoutInfo.width);
- System.arraycopy(this.layoutInfo.minHeight, 0, dim[1], 0, this.layoutInfo.height);
- return dim;
- }
- }
-
- public double[][] getLayoutWeights() {
- if (this.layoutInfo == null) {
- return new double[2][0];
- } else {
- double[][] weights = new double[2][];
- weights[0] = new double[this.layoutInfo.width];
- weights[1] = new double[this.layoutInfo.height];
- System.arraycopy(this.layoutInfo.weightX, 0, weights[0], 0, this.layoutInfo.width);
- System.arraycopy(this.layoutInfo.weightY, 0, weights[1], 0, this.layoutInfo.height);
- return weights;
- }
- }
-
- public Point location(int x, int y) {
- Point loc = new Point(0, 0);
- if (this.layoutInfo == null) {
- return loc;
- } else {
- int d = this.layoutInfo.startx;
-
- int i;
- for(i = 0; i < this.layoutInfo.width; ++i) {
- d += this.layoutInfo.minWidth[i];
- if (d > x) {
- break;
- }
- }
-
- loc.x = i;
- d = this.layoutInfo.starty;
-
- for(i = 0; i < this.layoutInfo.height; ++i) {
- d += this.layoutInfo.minHeight[i];
- if (d > y) {
- break;
- }
- }
-
- loc.y = i;
- return loc;
- }
- }
-
- public void addLayoutComponent(String name, Component comp) {
- }
-
- public void removeLayoutComponent(Component comp) {
- }
-
- public Dimension preferredLayoutSize(Container parent) {
- GridBagLayoutInfo info = this.GetLayoutInfo(parent, 2);
- return this.GetMinSize(parent, info);
- }
-
- public Dimension minimumLayoutSize(Container parent) {
- GridBagLayoutInfo info = this.GetLayoutInfo(parent, 1);
- return this.GetMinSize(parent, info);
- }
-
- public void layoutContainer(Container parent) {
- this.ArrangeGrid(parent);
- }
-
- public String toString() {
- return this.getClass().getName();
- }
-
- protected void DumpLayoutInfo(GridBagLayoutInfo s) {
- System.out.println("Col\tWidth\tWeight");
-
- for(int x = 0; x < s.width; ++x) {
- System.out.println(x + "\t" + s.minWidth[x] + "\t" + s.weightX[x]);
- }
-
- System.out.println("Row\tHeight\tWeight");
-
- for(int var3 = 0; var3 < s.height; ++var3) {
- System.out.println(var3 + "\t" + s.minHeight[var3] + "\t" + s.weightY[var3]);
- }
-
- }
-
- protected void DumpConstraints(GridBagConstraints constraints) {
- System.out.println("wt " + constraints.weightx + " " + constraints.weighty + ", " + "box " + constraints.gridx + " " + constraints.gridy + " " + constraints.gridwidth + " " + constraints.gridheight + ", " + "min " + constraints.minWidth + " " + constraints.minHeight + ", " + "pad " + constraints.insets.bottom + " " + constraints.insets.left + " " + constraints.insets.right + " " + constraints.insets.top + " " + constraints.ipadx + " " + constraints.ipady);
- }
-
- protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
- GridBagLayoutInfo r = new GridBagLayoutInfo();
- int ncomponents = parent.countComponents();
- r.width = r.height = 0;
- int curCol = -1;
- int curRow = -1;
- int[] xMax = new int[128];
- int[] yMax = new int[128];
-
- for(int compindex = 0; compindex < ncomponents; ++compindex) {
- Component comp = parent.getComponent(compindex);
- GridBagConstraints constraints = this.lookupConstraints(comp);
- int curX = constraints.gridx;
- int curY = constraints.gridy;
- int curWidth = constraints.gridwidth;
- if (curWidth <= 0) {
- curWidth = 1;
- }
-
- int curHeight = constraints.gridheight;
- if (curHeight <= 0) {
- curHeight = 1;
- }
-
- if (curX < 0 && curY < 0) {
- if (curRow >= 0) {
- curY = curRow;
- } else if (curCol >= 0) {
- curX = curCol;
- } else {
- curY = 0;
- }
- }
-
- if (curX < 0) {
- int px = 0;
-
- for(int i = curY; i < curY + curHeight; ++i) {
- px = Math.max(px, xMax[i]);
- }
-
- curX = px - curX - 1;
- if (curX < 0) {
- curX = 0;
- }
- } else if (curY < 0) {
- int py = 0;
-
- for(int i = curX; i < curX + curWidth; ++i) {
- py = Math.max(py, yMax[i]);
- }
-
- curY = py - curY - 1;
- if (curY < 0) {
- curY = 0;
- }
- }
-
- int px;
- for(px = curX + curWidth; r.width < px; ++r.width) {
- }
-
- int py;
- for(py = curY + curHeight; r.height < py; ++r.height) {
- }
-
- for(int i = curX; i < curX + curWidth; ++i) {
- yMax[i] = py;
- }
-
- for(int i = curY; i < curY + curHeight; ++i) {
- xMax[i] = px;
- }
-
- Dimension d;
- if (sizeflag == 2) {
- d = comp.preferredSize();
- } else {
- d = comp.minimumSize();
- }
-
- constraints.minWidth = d.width;
- constraints.minHeight = d.height;
- if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
- curCol = -1;
- curRow = -1;
- }
-
- if (constraints.gridheight == 0 && curRow < 0) {
- curCol = curX + curWidth;
- } else if (constraints.gridwidth == 0 && curCol < 0) {
- curRow = curY + curHeight;
- }
- }
-
- if (this.columnWidths != null && r.width < this.columnWidths.length) {
- r.width = this.columnWidths.length;
- }
-
- if (this.rowHeights != null && r.height < this.rowHeights.length) {
- r.height = this.rowHeights.length;
- }
-
- curCol = -1;
- curRow = -1;
- xMax = new int[128];
- yMax = new int[128];
-
- for(int var35 = 0; var35 < ncomponents; ++var35) {
- Component comp = parent.getComponent(var35);
- GridBagConstraints constraints = this.lookupConstraints(comp);
- int curX = constraints.gridx;
- int curY = constraints.gridy;
- int curWidth = constraints.gridwidth;
- int curHeight = constraints.gridheight;
- if (curX < 0 && curY < 0) {
- if (curRow >= 0) {
- curY = curRow;
- } else if (curCol >= 0) {
- curX = curCol;
- } else {
- curY = 0;
- }
- }
-
- if (curX < 0) {
- if (curHeight <= 0) {
- curHeight += r.height - curY;
- if (curHeight < 1) {
- curHeight = 1;
- }
- }
-
- int px = 0;
-
- for(int i = curY; i < curY + curHeight; ++i) {
- px = Math.max(px, xMax[i]);
- }
-
- curX = px - curX - 1;
- if (curX < 0) {
- curX = 0;
- }
- } else if (curY < 0) {
- if (curWidth <= 0) {
- curWidth += r.width - curX;
- if (curWidth < 1) {
- curWidth = 1;
- }
- }
-
- int py = 0;
-
- for(int i = curX; i < curX + curWidth; ++i) {
- py = Math.max(py, yMax[i]);
- }
-
- curY = py - curY - 1;
- if (curY < 0) {
- curY = 0;
- }
- }
-
- if (curWidth <= 0) {
- curWidth += r.width - curX;
- if (curWidth < 1) {
- curWidth = 1;
- }
- }
-
- if (curHeight <= 0) {
- curHeight += r.height - curY;
- if (curHeight < 1) {
- curHeight = 1;
- }
- }
-
- int px = curX + curWidth;
- int py = curY + curHeight;
-
- for(int i = curX; i < curX + curWidth; ++i) {
- yMax[i] = py;
- }
-
- for(int var43 = curY; var43 < curY + curHeight; ++var43) {
- xMax[var43] = px;
- }
-
- if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
- curCol = -1;
- curRow = -1;
- }
-
- if (constraints.gridheight == 0 && curRow < 0) {
- curCol = curX + curWidth;
- } else if (constraints.gridwidth == 0 && curCol < 0) {
- curRow = curY + curHeight;
- }
-
- constraints.tempX = curX;
- constraints.tempY = curY;
- constraints.tempWidth = curWidth;
- constraints.tempHeight = curHeight;
- }
-
- int nextSize = Integer.MAX_VALUE;
-
- for(int i = 1; i != Integer.MAX_VALUE; nextSize = Integer.MAX_VALUE) {
- for(int var36 = 0; var36 < ncomponents; ++var36) {
- Component comp = parent.getComponent(var36);
- GridBagConstraints constraints = this.lookupConstraints(comp);
- if (constraints.tempWidth != i) {
- if (constraints.tempWidth > i && constraints.tempWidth < nextSize) {
- nextSize = constraints.tempWidth;
- }
- } else {
- int px = constraints.tempX + constraints.tempWidth;
- double weight_diff = constraints.weightx;
-
- for(int k = constraints.tempX; k < px; ++k) {
- weight_diff -= r.weightX[k];
- }
-
- if (weight_diff > (double)0.0F) {
- double weight = (double)0.0F;
-
- for(int var49 = constraints.tempX; var49 < px; ++var49) {
- weight += r.weightX[var49];
- }
-
- for(int var50 = constraints.tempX; weight > (double)0.0F; ++var50) {
- double wt = r.weightX[var50];
- double dx = wt * weight_diff / weight;
- double[] var10000 = r.weightX;
- var10000[var50] += dx;
- weight_diff -= dx;
- weight -= wt;
- }
-
- double[] var87 = r.weightX;
- var87[px - 1] += weight_diff;
- }
-
- int pixels_diff = constraints.minWidth + constraints.ipadx + constraints.insets.left + constraints.insets.right;
-
- for(int var51 = constraints.tempX; var51 < px; ++var51) {
- pixels_diff -= r.minWidth[var51];
- }
-
- if (pixels_diff > 0) {
- double weight = (double)0.0F;
-
- for(int var52 = constraints.tempX; var52 < px; ++var52) {
- weight += r.weightX[var52];
- }
-
- for(int var53 = constraints.tempX; weight > (double)0.0F; ++var53) {
- double wt = r.weightX[var53];
- int dx = (int)(wt * (double)pixels_diff / weight);
- int[] var88 = r.minWidth;
- var88[var53] += dx;
- pixels_diff -= dx;
- weight -= wt;
- }
-
- int[] var89 = r.minWidth;
- var89[px - 1] += pixels_diff;
- }
- }
-
- if (constraints.tempHeight != i) {
- if (constraints.tempHeight > i && constraints.tempHeight < nextSize) {
- nextSize = constraints.tempHeight;
- }
- } else {
- int py = constraints.tempY + constraints.tempHeight;
- double weight_diff = constraints.weighty;
-
- for(int k = constraints.tempY; k < py; ++k) {
- weight_diff -= r.weightY[k];
- }
-
- if (weight_diff > (double)0.0F) {
- double weight = (double)0.0F;
-
- for(int var55 = constraints.tempY; var55 < py; ++var55) {
- weight += r.weightY[var55];
- }
-
- for(int var56 = constraints.tempY; weight > (double)0.0F; ++var56) {
- double wt = r.weightY[var56];
- double dy = wt * weight_diff / weight;
- double[] var90 = r.weightY;
- var90[var56] += dy;
- weight_diff -= dy;
- weight -= wt;
- }
-
- double[] var91 = r.weightY;
- var91[py - 1] += weight_diff;
- }
-
- int pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top + constraints.insets.bottom;
-
- for(int var57 = constraints.tempY; var57 < py; ++var57) {
- pixels_diff -= r.minHeight[var57];
- }
-
- if (pixels_diff > 0) {
- double weight = (double)0.0F;
-
- for(int var58 = constraints.tempY; var58 < py; ++var58) {
- weight += r.weightY[var58];
- }
-
- for(int var59 = constraints.tempY; weight > (double)0.0F; ++var59) {
- double wt = r.weightY[var59];
- int dy = (int)(wt * (double)pixels_diff / weight);
- int[] var92 = r.minHeight;
- var92[var59] += dy;
- pixels_diff -= dy;
- weight -= wt;
- }
-
- int[] var93 = r.minHeight;
- var93[py - 1] += pixels_diff;
- }
- }
- }
-
- i = nextSize;
- }
-
- if (this.columnWidths != null) {
- for(int var45 = 0; var45 < this.columnWidths.length; ++var45) {
- if (r.minWidth[var45] < this.columnWidths[var45]) {
- r.minWidth[var45] = this.columnWidths[var45];
- }
- }
- }
-
- if (this.rowHeights != null) {
- for(int var46 = 0; var46 < this.rowHeights.length; ++var46) {
- if (r.minHeight[var46] < this.rowHeights[var46]) {
- r.minHeight[var46] = this.rowHeights[var46];
- }
- }
- }
-
- if (this.columnWeights != null) {
- for(int var47 = 0; var47 < r.width && var47 < this.columnWeights.length; ++var47) {
- if (r.weightX[var47] < this.columnWeights[var47]) {
- r.weightX[var47] = this.columnWeights[var47];
- }
- }
- }
-
- if (this.rowWeights != null) {
- for(int var48 = 0; var48 < r.height && var48 < this.rowWeights.length; ++var48) {
- if (r.weightY[var48] < this.rowWeights[var48]) {
- r.weightY[var48] = this.rowWeights[var48];
- }
- }
- }
-
- return r;
- }
-
- protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r) {
- r.x += constraints.insets.left;
- r.width -= constraints.insets.left + constraints.insets.right;
- r.y += constraints.insets.top;
- r.height -= constraints.insets.top + constraints.insets.bottom;
- int diffx = 0;
- if (constraints.fill != 2 && constraints.fill != 1 && r.width > constraints.minWidth + constraints.ipadx) {
- diffx = r.width - (constraints.minWidth + constraints.ipadx);
- r.width = constraints.minWidth + constraints.ipadx;
- }
-
- int diffy = 0;
- if (constraints.fill != 3 && constraints.fill != 1 && r.height > constraints.minHeight + constraints.ipady) {
- diffy = r.height - (constraints.minHeight + constraints.ipady);
- r.height = constraints.minHeight + constraints.ipady;
- }
-
- switch (constraints.anchor) {
- case 10:
- r.x += diffx / 2;
- r.y += diffy / 2;
- return;
- case 11:
- r.x += diffx / 2;
- return;
- case 12:
- r.x += diffx;
- return;
- case 13:
- r.x += diffx;
- r.y += diffy / 2;
- return;
- case 14:
- r.x += diffx;
- r.y += diffy;
- return;
- case 15:
- r.x += diffx / 2;
- r.y += diffy;
- return;
- case 16:
- r.y += diffy;
- return;
- case 17:
- r.y += diffy / 2;
- return;
- case 18:
- return;
- default:
- throw new IllegalArgumentException("illegal anchor value");
- }
- }
-
- protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) {
- Dimension d = new Dimension();
- Insets insets = parent.insets();
- int t = 0;
-
- for(int i = 0; i < info.width; ++i) {
- t += info.minWidth[i];
- }
-
- d.width = t + insets.left + insets.right;
- t = 0;
-
- for(int var7 = 0; var7 < info.height; ++var7) {
- t += info.minHeight[var7];
- }
-
- d.height = t + insets.top + insets.bottom;
- return d;
- }
-
- protected void ArrangeGrid(Container parent) {
- Insets insets = parent.insets();
- int ncomponents = parent.countComponents();
- Rectangle r = new Rectangle();
- if (ncomponents != 0 || this.columnWidths != null && this.columnWidths.length != 0 || this.rowHeights != null && this.rowHeights.length != 0) {
- GridBagLayoutInfo info = this.GetLayoutInfo(parent, 2);
- Dimension d = this.GetMinSize(parent, info);
- if (d.width < parent.width || d.height < parent.height) {
- info = this.GetLayoutInfo(parent, 1);
- d = this.GetMinSize(parent, info);
- }
-
- this.layoutInfo = info;
- r.width = d.width;
- r.height = d.height;
- int diffw = parent.width - r.width;
- if (diffw != 0) {
- double weight = (double)0.0F;
-
- for(int i = 0; i < info.width; ++i) {
- weight += info.weightX[i];
- }
-
- if (weight > (double)0.0F) {
- for(int i = 0; i < info.width; ++i) {
- int dx = (int)((double)diffw * info.weightX[i] / weight);
- int[] var10000 = info.minWidth;
- var10000[i] += dx;
- r.width += dx;
- if (info.minWidth[i] < 0) {
- r.width -= info.minWidth[i];
- info.minWidth[i] = 0;
- }
- }
- }
-
- diffw = parent.width - r.width;
- } else {
- diffw = 0;
- }
-
- int diffh = parent.height - r.height;
- if (diffh != 0) {
- double weight = (double)0.0F;
-
- for(int i = 0; i < info.height; ++i) {
- weight += info.weightY[i];
- }
-
- if (weight > (double)0.0F) {
- for(int var18 = 0; var18 < info.height; ++var18) {
- int dy = (int)((double)diffh * info.weightY[var18] / weight);
- int[] var27 = info.minHeight;
- var27[var18] += dy;
- r.height += dy;
- if (info.minHeight[var18] < 0) {
- r.height -= info.minHeight[var18];
- info.minHeight[var18] = 0;
- }
- }
- }
-
- diffh = parent.height - r.height;
- } else {
- diffh = 0;
- }
-
- info.startx = diffw / 2 + insets.left;
- info.starty = diffh / 2 + insets.top;
-
- for(int compindex = 0; compindex < ncomponents; ++compindex) {
- Component comp = parent.getComponent(compindex);
- GridBagConstraints constraints = this.lookupConstraints(comp);
- r.x = info.startx;
-
- for(int i = 0; i < constraints.tempX; ++i) {
- r.x += info.minWidth[i];
- }
-
- r.y = info.starty;
-
- for(int var20 = 0; var20 < constraints.tempY; ++var20) {
- r.y += info.minHeight[var20];
- }
-
- r.width = 0;
-
- for(int var21 = constraints.tempX; var21 < constraints.tempX + constraints.tempWidth; ++var21) {
- r.width += info.minWidth[var21];
- }
-
- r.height = 0;
-
- for(int var22 = constraints.tempY; var22 < constraints.tempY + constraints.tempHeight; ++var22) {
- r.height += info.minHeight[var22];
- }
-
- this.AdjustForGravity(constraints, r);
- if (r.width > 0 && r.height > 0) {
- if (comp.x != r.x || comp.y != r.y || comp.width != r.width || comp.height != r.height) {
- comp.reshape(r.x, r.y, r.width, r.height);
- }
-
- comp.show();
- } else {
- comp.hide();
- }
- }
-
- }
- }
- }
-