SUGGESTED FIX
------- GridBagLayout.java -------
*** /tmp/geta21425 Fri Nov 14 11:51:21 2003
--- /tmp/getb21425 Fri Nov 14 11:51:21 2003
***************
*** 8,13 ****
--- 8,14 ----
import java.util.Hashtable;
import java.util.Vector;
+ import java.util.ArrayList;
class GridBagLayoutInfo implements java.io.Serializable {
int width, height; /* number of cells horizontally, vertically */
***************
*** 18,28 ****
double weightY[]; /* largest weight in each row */
GridBagLayoutInfo () {
! minWidth = new int[GridBagLayout.MAXGRIDSIZE];
! minHeight = new int[GridBagLayout.MAXGRIDSIZE];
! weightX = new double[GridBagLayout.MAXGRIDSIZE];
! weightY = new double[GridBagLayout.MAXGRIDSIZE];
}
}
/**
--- 19,35 ----
double weightY[]; /* largest weight in each row */
GridBagLayoutInfo () {
! this(GridBagLayout.MAXGRIDSIZE, GridBagLayout.MAXGRIDSIZE);
}
+ GridBagLayoutInfo(int width, int height) {
+ this.width = width;
+ this.height = height;
+
+ minWidth = new int[width];
+ minHeight = new int[height];
+ weightX = new double[width];
+ weightY = new double[height];
+ }
}
/**
***************
*** 269,275 ****
* }
* </pre></blockquote><hr>
* <p>
! * @version 1.5, 16 Nov 1995
* @author Doug Stein
* @see java.awt.GridBagConstraints
* @see java.awt.ComponentOrientation
--- 276,282 ----
* }
* </pre></blockquote><hr>
* <p>
! * @version %I%, %G%
* @author Doug Stein
* @see java.awt.GridBagConstraints
* @see java.awt.ComponentOrientation
***************
*** 279,286 ****
java.io.Serializable {
/**
! * The maximum number of grid positions (both horizontally and
! * vertically) that can be laid out by the grid bag layout.
*/
protected static final int MAXGRIDSIZE = 512;
--- 286,294 ----
java.io.Serializable {
/**
! * As of 1.5, this field is no longer used. Previously, this was
! * the maximum number of grid positions (both horizontal and
! * vertical) that could be laid out by the grid bag layout.
*/
protected static final int MAXGRIDSIZE = 512;
***************
*** 819,824 ****
--- 827,840 ----
return GetLayoutInfo(parent, sizeflag);
}
+ private static final Integer ZERO = new Integer(0);
+ private static void ensureSize(ArrayList<Integer> arrayList, int size) {
+ arrayList.ensureCapacity(size);
+ while (arrayList.size() < size) {
+ arrayList.add(ZERO);
+ }
+ }
+
/**
* This method is obsolete and supplied for backwards
* compatability only; new code should call {@link
***************
*** 829,844 ****
*/
protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
synchronized (parent.getTreeLock()) {
! GridBagLayoutInfo r = new GridBagLayoutInfo();
Component comp;
GridBagConstraints constraints;
Dimension d;
Component components[] = parent.getComponents();
int compindex, i, j, k, px, py, pixels_diff, nextSize;
int curX, curY, curWidth, curHeight, curRow, curCol;
double weight_diff, weight, start, size;
int xMax[], yMax[];
/*
* Pass #1
--- 845,863 ----
*/
protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
synchronized (parent.getTreeLock()) {
! GridBagLayoutInfo r;
Component comp;
GridBagConstraints constraints;
Dimension d;
Component components[] = parent.getComponents();
+ int layoutWidth, layoutHeight;
int compindex, i, j, k, px, py, pixels_diff, nextSize;
int curX, curY, curWidth, curHeight, curRow, curCol;
double weight_diff, weight, start, size;
int xMax[], yMax[];
+ ArrayList<Integer> xMaxArray = new ArrayList<Integer>(components.length);
+ ArrayList<Integer> yMaxArray = new ArrayList<Integer>(components.length);
/*
* Pass #1
***************
*** 847,856 ****
* zero or negative widths and heights).
*/
! r.width = r.height = 0;
curRow = curCol = -1;
- xMax = new int[MAXGRIDSIZE];
- yMax = new int[MAXGRIDSIZE];
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
--- 866,873 ----
* zero or negative widths and heights).
*/
! layoutWidth = layoutHeight = 0;
curRow = curCol = -1;
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
***************
*** 878,885 ****
}
if (curX < 0) {
px = 0;
! for (i = curY; i < (curY + curHeight); i++)
! px = Math.max(px, xMax[i]);
curX = px - curX - 1;
if(curX < 0)
--- 895,904 ----
}
if (curX < 0) {
px = 0;
! ensureSize(xMaxArray, curY + curHeight);
! for (i = curY; i < (curY + curHeight); i++) {
! px = Math.max(px, xMaxArray.get(i).intValue());
! }
curX = px - curX - 1;
if(curX < 0)
***************
*** 887,894 ****
}
else if (curY < 0) {
py = 0;
! for (i = curX; i < (curX + curWidth); i++)
! py = Math.max(py, yMax[i]);
curY = py - curY - 1;
if(curY < 0)
--- 906,915 ----
}
else if (curY < 0) {
py = 0;
! ensureSize(yMaxArray, curX + curWidth);
! for (i = curX; i < (curX + curWidth); i++) {
! py = Math.max(py, yMaxArray.get(i).intValue());
! }
curY = py - curY - 1;
if(curY < 0)
***************
*** 896,907 ****
}
/* Adjust the grid width and height */
! for (px = curX + curWidth; r.width < px; r.width++);
! for (py = curY + curHeight; r.height < py; r.height++);
! /* Adjust the xMax and yMax arrays */
! for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
! for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
/* Cache the current slave's size. */
if (sizeflag == PREFERREDSIZE)
--- 917,934 ----
}
/* Adjust the grid width and height */
! for (px = curX + curWidth; layoutWidth < px; layoutWidth++);
! for (py = curY + curHeight; layoutHeight < py; layoutHeight++);
! /* Adjust xMaxArray and yMaxArray */
! ensureSize(yMaxArray, curX + curWidth);
! for (i = curX; i < (curX + curWidth); i++) {
! yMaxArray.set(i, new Integer(py));
! }
! ensureSize(xMaxArray, curY + curHeight);
! for (i = curY; i < (curY + curHeight); i++) {
! xMaxArray.set(i, new Integer(px));
! }
/* Cache the current slave's size. */
if (sizeflag == PREFERREDSIZE)
***************
*** 928,938 ****
/*
* Apply minimum row/column dimensions
*/
! if (columnWidths != null && r.width < columnWidths.length)
! r.width = columnWidths.length;
! if (rowHeights != null && r.height < rowHeights.length)
! r.height = rowHeights.length;
/*
* Pass #2
*
--- 955,970 ----
/*
* Apply minimum row/column dimensions
*/
! if (columnWidths != null && layoutWidth < columnWidths.length)
! layoutWidth = columnWidths.length;
! if (rowHeights != null && layoutHeight < rowHeights.length)
! layoutHeight = rowHeights.length;
+ // Now that we know the grid's width and height, create the
+ // GridBagLayoutInfo.
+ r = new GridBagLayoutInfo(layoutWidth, layoutHeight);
+
+
/*
* Pass #2
*
***************
*** 943,950 ****
*/
curRow = curCol = -1;
! xMax = new int[MAXGRIDSIZE];
! yMax = new int[MAXGRIDSIZE];
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
--- 975,982 ----
*/
curRow = curCol = -1;
! xMax = new int[layoutHeight];
! yMax = new int[layoutWidth];
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
###@###.### 2003-11-14
*** /tmp/geta17605 14 11:44:43 2005
--- GridBagLayout.java 10 15:35:07 2005
***************
*** 840,846 ****
* maximumArrayXIndex and maximumArrayYIndex.
*/
! private Point preInitMaximumArraySizes(Container parent){
Component components[] = parent.getComponents();
Component comp;
GridBagConstraints constraints;
--- 840,846 ----
* maximumArrayXIndex and maximumArrayYIndex.
*/
! private long[] preInitMaximumArraySizes(Container parent){
Component components[] = parent.getComponents();
Component comp;
GridBagConstraints constraints;
***************
*** 848,854 ****
int curWidth, curHeight;
int preMaximumArrayXIndex = 0;
int preMaximumArrayYIndex = 0;
!
for (int compId = 0 ; compId < components.length ; compId++) {
comp = components[compId];
if (!comp.isVisible()) {
--- 848,855 ----
int curWidth, curHeight;
int preMaximumArrayXIndex = 0;
int preMaximumArrayYIndex = 0;
! long [] returnArray = new long[2];
!
for (int compId = 0 ; compId < components.length ; compId++) {
comp = components[compId];
if (!comp.isVisible()) {
***************
*** 886,892 ****
preMaximumArrayYIndex = Math.max(curX + curWidth, preMaximumArrayYIndex);
} //for (components) loop
// Must specify index++ to allocate well-working arrays.
! return new Point(preMaximumArrayXIndex, preMaximumArrayYIndex);
} //PreInitMaximumSizes
/**
--- 887,898 ----
preMaximumArrayYIndex = Math.max(curX + curWidth, preMaximumArrayYIndex);
} //for (components) loop
// Must specify index++ to allocate well-working arrays.
! /* fix for 4623196.
! * now return long array instead of Point
! */
! returnArray[0] = preMaximumArrayXIndex;
! returnArray[1] = preMaximumArrayYIndex;
! return returnArray;
} //PreInitMaximumSizes
/**
***************
*** 933,942 ****
layoutWidth = layoutHeight = 0;
curRow = curCol = -1;
! Point p = preInitMaximumArraySizes(parent);
! maximumArrayXIndex = EMPIRICMULTIPLIER*p.x;
! maximumArrayYIndex = EMPIRICMULTIPLIER*p.y;
xMaxArray = new int[maximumArrayXIndex];
yMaxArray = new int[maximumArrayYIndex];
--- 939,955 ----
layoutWidth = layoutHeight = 0;
curRow = curCol = -1;
! long [] arraySizes = preInitMaximumArraySizes(parent);
+ /* fix for 4623196.
+ * If user try to create a very big grid we can
+ * get NegativeArraySizeException because of integer value
+ * overflow (EMPIRICMULTIPLIER*gridSize might be more then Integer.MAX_VALUE).
+ * We need to detect this situation and try to create a
+ * grid with Integer.MAX_VALUE size instead.
+ */
+ maximumArrayXIndex = (EMPIRICMULTIPLIER * arraySizes[0] > Integer.MAX_VALUE )? Integer.MAX_VALUE : EMPIRICMULTIPLIER*(int)arraySizes[0];
+ maximumArrayYIndex = (EMPIRICMULTIPLIER * arraySizes[1] > Integer.MAX_VALUE )? Integer.MAX_VALUE : EMPIRICMULTIPLIER*(int)arraySizes[1];
xMaxArray = new int[maximumArrayXIndex];
yMaxArray = new int[maximumArrayYIndex];
###@###.### 2005-03-14 09:02:49 GMT
|