Commit 924af605 by Tom Tromey Committed by Tom Tromey

GridLayout.java (layoutContainer): Use tree lock.

	* java/awt/GridLayout.java (layoutContainer): Use tree lock.
	(getSize): Likewise.
	* java/awt/FlowLayout.java (layoutContainer): Use tree lock.
	(getSize): Likewise.
	* java/awt/BorderLayout.java (layoutContainer): Use tree lock.
	(calcSize): Likewise.
	* java/awt/CardLayout.java (getSize): Use tree lock.
	(gotoComponent): Likewise.
	(layoutContainer): Likewise.

From-SVN: r58998
parent a6b5bd3b
2002-11-10 Tom Tromey <tromey@redhat.com> 2002-11-10 Tom Tromey <tromey@redhat.com>
* java/awt/GridLayout.java (layoutContainer): Use tree lock.
(getSize): Likewise.
* java/awt/FlowLayout.java (layoutContainer): Use tree lock.
(getSize): Likewise.
* java/awt/BorderLayout.java (layoutContainer): Use tree lock.
(calcSize): Likewise.
* java/awt/CardLayout.java (getSize): Use tree lock.
(gotoComponent): Likewise.
(layoutContainer): Likewise.
* java/io/natFileDescriptorWin32.cc (read): Handle case where * java/io/natFileDescriptorWin32.cc (read): Handle case where
count is 0. count is 0.
* java/io/natFileDescriptorPosix.cc (read): Handle case where * java/io/natFileDescriptorPosix.cc (read): Handle case where
......
...@@ -529,80 +529,83 @@ invalidateLayout(Container parent) ...@@ -529,80 +529,83 @@ invalidateLayout(Container parent)
public void public void
layoutContainer(Container target) layoutContainer(Container target)
{ {
Insets i = target.getInsets(); synchronized (target.getTreeLock ())
ComponentOrientation orient = target.getComponentOrientation ();
boolean left_to_right = orient.isLeftToRight ();
Component my_north = north;
Component my_east = east;
Component my_south = south;
Component my_west = west;
// Note that we currently don't handle vertical layouts. Neither
// does JDK 1.3.
if (firstLine != null)
my_north = firstLine;
if (lastLine != null)
my_south = lastLine;
if (firstItem != null)
{ {
if (left_to_right) Insets i = target.getInsets();
my_west = firstItem;
else ComponentOrientation orient = target.getComponentOrientation ();
my_east = firstItem; boolean left_to_right = orient.isLeftToRight ();
Component my_north = north;
Component my_east = east;
Component my_south = south;
Component my_west = west;
// Note that we currently don't handle vertical layouts. Neither
// does JDK 1.3.
if (firstLine != null)
my_north = firstLine;
if (lastLine != null)
my_south = lastLine;
if (firstItem != null)
{
if (left_to_right)
my_west = firstItem;
else
my_east = firstItem;
}
if (lastItem != null)
{
if (left_to_right)
my_east = lastItem;
else
my_west = lastItem;
}
Dimension c = calcCompSize(center, PREF);
Dimension n = calcCompSize(my_north, PREF);
Dimension s = calcCompSize(my_south, PREF);
Dimension e = calcCompSize(my_east, PREF);
Dimension w = calcCompSize(my_west, PREF);
Dimension t = target.getSize();
/*
<-> hgap <-> hgap
+----------------------------+ }
|t | } i.top
| +----------------------+ | --- y1 }
| |n | |
| +----------------------+ | } vgap
| +---+ +----------+ +---+ | --- y2 } }
| |w | |c | |e | | } hh
| +---+ +----------+ +---+ | } vgap }
| +----------------------+ | --- y3 }
| |s | |
| +----------------------+ | }
| | } i.bottom
+----------------------------+ }
|x1 |x2 |x3
<---------------------->
<--> ww <-->
i.left i.right
*/
int x1 = i.left;
int x2 = x1 + w.width + hgap;
int x3 = t.width - i.right - e.width;
int ww = t.width - i.right - i.left;
int y1 = i.top;
int y2 = y1 + n.height + vgap;
int y3 = t.height - i.bottom - s.height;
int hh = y3-y2-vgap;
setBounds(center, x2, y2, x3-x2-hgap, hh);
setBounds(my_north, x1, y1, ww, n.height);
setBounds(my_south, x1, y3, ww, s.height);
setBounds(my_west, x1, y2, w.width, hh);
setBounds(my_east, x3, y2, e.width, hh);
} }
if (lastItem != null)
{
if (left_to_right)
my_east = lastItem;
else
my_west = lastItem;
}
Dimension c = calcCompSize(center, PREF);
Dimension n = calcCompSize(my_north, PREF);
Dimension s = calcCompSize(my_south, PREF);
Dimension e = calcCompSize(my_east, PREF);
Dimension w = calcCompSize(my_west, PREF);
Dimension t = target.getSize();
/*
<-> hgap <-> hgap
+----------------------------+ }
|t | } i.top
| +----------------------+ | --- y1 }
| |n | |
| +----------------------+ | } vgap
| +---+ +----------+ +---+ | --- y2 } }
| |w | |c | |e | | } hh
| +---+ +----------+ +---+ | } vgap }
| +----------------------+ | --- y3 }
| |s | |
| +----------------------+ | }
| | } i.bottom
+----------------------------+ }
|x1 |x2 |x3
<---------------------->
<--> ww <-->
i.left i.right
*/
int x1 = i.left;
int x2 = x1 + w.width + hgap;
int x3 = t.width - i.right - e.width;
int ww = t.width - i.right - i.left;
int y1 = i.top;
int y2 = y1 + n.height + vgap;
int y3 = t.height - i.bottom - s.height;
int hh = y3-y2-vgap;
setBounds(center, x2, y2, x3-x2-hgap, hh);
setBounds(my_north, x1, y1, ww, n.height);
setBounds(my_south, x1, y3, ww, s.height);
setBounds(my_west, x1, y2, w.width, hh);
setBounds(my_east, x3, y2, e.width, hh);
} }
/*************************************************************************/ /*************************************************************************/
...@@ -648,59 +651,62 @@ calcCompSize(Component comp, int what) ...@@ -648,59 +651,62 @@ calcCompSize(Component comp, int what)
private Dimension private Dimension
calcSize(Container target, int what) calcSize(Container target, int what)
{ {
Insets ins = target.getInsets(); synchronized (target.getTreeLock ())
ComponentOrientation orient = target.getComponentOrientation ();
boolean left_to_right = orient.isLeftToRight ();
Component my_north = north;
Component my_east = east;
Component my_south = south;
Component my_west = west;
// Note that we currently don't handle vertical layouts. Neither
// does JDK 1.3.
if (firstLine != null)
my_north = firstLine;
if (lastLine != null)
my_south = lastLine;
if (firstItem != null)
{ {
if (left_to_right) Insets ins = target.getInsets();
my_west = firstItem;
else ComponentOrientation orient = target.getComponentOrientation ();
my_east = firstItem; boolean left_to_right = orient.isLeftToRight ();
}
if (lastItem != null) Component my_north = north;
{ Component my_east = east;
if (left_to_right) Component my_south = south;
my_east = lastItem; Component my_west = west;
else
my_west = lastItem; // Note that we currently don't handle vertical layouts. Neither
} // does JDK 1.3.
if (firstLine != null)
my_north = firstLine;
if (lastLine != null)
my_south = lastLine;
if (firstItem != null)
{
if (left_to_right)
my_west = firstItem;
else
my_east = firstItem;
}
if (lastItem != null)
{
if (left_to_right)
my_east = lastItem;
else
my_west = lastItem;
}
Dimension ndim = calcCompSize(my_north, what); Dimension ndim = calcCompSize(my_north, what);
Dimension sdim = calcCompSize(my_south, what); Dimension sdim = calcCompSize(my_south, what);
Dimension edim = calcCompSize(my_east, what); Dimension edim = calcCompSize(my_east, what);
Dimension wdim = calcCompSize(my_west, what); Dimension wdim = calcCompSize(my_west, what);
Dimension cdim = calcCompSize(center, what); Dimension cdim = calcCompSize(center, what);
int width = edim.width + cdim.width + wdim.width + (hgap * 2); int width = edim.width + cdim.width + wdim.width + (hgap * 2);
if (ndim.width > width) if (ndim.width > width)
width = ndim.width; width = ndim.width;
if (sdim.width > width) if (sdim.width > width)
width = sdim.width; width = sdim.width;
width += (ins.left + ins.right); width += (ins.left + ins.right);
int height = edim.height; int height = edim.height;
if (cdim.height > height) if (cdim.height > height)
height = cdim.height; height = cdim.height;
if (wdim.height > height) if (wdim.height > height)
height = wdim.height; height = wdim.height;
height += (ndim.height + sdim.height + (vgap * 2) + ins.top + ins.bottom); height += (ndim.height + sdim.height + (vgap * 2) + ins.top + ins.bottom);
return(new Dimension(width, height)); return(new Dimension(width, height));
}
} }
} // class BorderLayout } // class BorderLayout
...@@ -165,21 +165,24 @@ public class CardLayout implements LayoutManager2, Serializable ...@@ -165,21 +165,24 @@ public class CardLayout implements LayoutManager2, Serializable
*/ */
public void layoutContainer (Container parent) public void layoutContainer (Container parent)
{ {
int width = parent.width; synchronized (parent.getTreeLock ())
int height = parent.height; {
int width = parent.width;
int height = parent.height;
Insets ins = parent.getInsets (); Insets ins = parent.getInsets ();
int num = parent.ncomponents; int num = parent.ncomponents;
Component[] comps = parent.component; Component[] comps = parent.component;
int x = ins.left + hgap; int x = ins.left + hgap;
int y = ins.top + vgap; int y = ins.top + vgap;
width = width - 2 * hgap - ins.left - ins.right; width = width - 2 * hgap - ins.left - ins.right;
height = height - 2 * vgap - ins.top - ins.bottom; height = height - 2 * vgap - ins.top - ins.bottom;
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
comps[i].setBounds (x, y, width, height); comps[i].setBounds (x, y, width, height);
}
} }
/** Get the maximum layout size of the container. /** Get the maximum layout size of the container.
...@@ -287,91 +290,97 @@ public class CardLayout implements LayoutManager2, Serializable ...@@ -287,91 +290,97 @@ public class CardLayout implements LayoutManager2, Serializable
private void gotoComponent (Container parent, int what, private void gotoComponent (Container parent, int what,
Component target) Component target)
{ {
int num = parent.ncomponents; synchronized (parent.getTreeLock ())
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
int choice = -1;
if (what == FIRST)
choice = 0;
else if (what == LAST)
choice = num - 1;
else if (what >= 0)
choice = what;
for (int i = 0; i < num; ++i)
{ {
// If TARGET is set then we are looking for a specific int num = parent.ncomponents;
// component. // This is more efficient than calling getComponents().
if (target != null) Component[] comps = parent.component;
{ int choice = -1;
if (target == comps[i])
choice = i; if (what == FIRST)
} choice = 0;
else if (what == LAST)
if (comps[i].isVisible ()) choice = num - 1;
else if (what >= 0)
choice = what;
for (int i = 0; i < num; ++i)
{ {
if (what == NEXT) // If TARGET is set then we are looking for a specific
// component.
if (target != null)
{ {
choice = i + 1; if (target == comps[i])
if (choice == num) choice = i;
choice = 0;
} }
else if (what == PREV)
{ if (comps[i].isVisible ())
choice = i - 1;
if (choice < 0)
choice = num - 1;
}
else if (choice == i)
{ {
// Do nothing if we're already looking at the right if (what == NEXT)
// component. {
return; choice = i + 1;
if (choice == num)
choice = 0;
}
else if (what == PREV)
{
choice = i - 1;
if (choice < 0)
choice = num - 1;
}
else if (choice == i)
{
// Do nothing if we're already looking at the right
// component.
return;
}
comps[i].setVisible (false);
if (choice >= 0)
break;
} }
comps[i].setVisible (false);
if (choice >= 0)
break;
} }
}
if (choice >= 0 && choice < num) if (choice >= 0 && choice < num)
comps[choice].setVisible (true); comps[choice].setVisible (true);
}
} }
// Compute the size according to WHAT. // Compute the size according to WHAT.
private Dimension getSize (Container parent, int what) private Dimension getSize (Container parent, int what)
{ {
int w = 0, h = 0, num = parent.ncomponents; synchronized (parent.getTreeLock ())
Component[] comps = parent.component;
for (int i = 0; i < num; ++i)
{ {
Dimension d; int w = 0, h = 0, num = parent.ncomponents;
Component[] comps = parent.component;
for (int i = 0; i < num; ++i)
{
Dimension d;
if (what == MIN) if (what == MIN)
d = comps[i].getMinimumSize (); d = comps[i].getMinimumSize ();
else if (what == MAX) else if (what == MAX)
d = comps[i].getMaximumSize (); d = comps[i].getMaximumSize ();
else else
d = comps[i].getPreferredSize (); d = comps[i].getPreferredSize ();
w = Math.max (d.width, w); w = Math.max (d.width, w);
h = Math.max (d.height, h); h = Math.max (d.height, h);
} }
Insets i = parent.getInsets (); Insets i = parent.getInsets ();
w += 2 * hgap + i.right + i.left; w += 2 * hgap + i.right + i.left;
h += 2 * vgap + i.bottom + i.top; h += 2 * vgap + i.bottom + i.top;
// Handle overflow. // Handle overflow.
if (w < 0) if (w < 0)
w = Integer.MAX_VALUE; w = Integer.MAX_VALUE;
if (h < 0) if (h < 0)
h = Integer.MAX_VALUE; h = Integer.MAX_VALUE;
return new Dimension (w, h); return new Dimension (w, h);
}
} }
/** /**
......
...@@ -150,76 +150,79 @@ public class FlowLayout implements LayoutManager, Serializable ...@@ -150,76 +150,79 @@ public class FlowLayout implements LayoutManager, Serializable
*/ */
public void layoutContainer (Container parent) public void layoutContainer (Container parent)
{ {
int num = parent.getComponentCount (); synchronized (parent.getTreeLock ())
// This is more efficient than calling getComponents(). {
Component[] comps = parent.component; int num = parent.getComponentCount ();
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
Dimension d = parent.getSize (); Dimension d = parent.getSize ();
Insets ins = parent.getInsets (); Insets ins = parent.getInsets ();
ComponentOrientation orient = parent.getComponentOrientation (); ComponentOrientation orient = parent.getComponentOrientation ();
boolean left_to_right = orient.isLeftToRight (); boolean left_to_right = orient.isLeftToRight ();
int y = ins.top + vgap; int y = ins.top + vgap;
int i = 0; int i = 0;
while (i < num) while (i < num)
{
// Find the components which go in the current row.
int new_w = ins.left + hgap + ins.right;
int new_h = 0;
int j;
boolean found_one = false;
for (j = i; j < num && ! found_one; ++j)
{ {
// Skip invisible items. // Find the components which go in the current row.
if (! comps[i].visible) int new_w = ins.left + hgap + ins.right;
continue; int new_h = 0;
int j;
Dimension c = comps[i].getPreferredSize (); boolean found_one = false;
for (j = i; j < num && ! found_one; ++j)
int next_w = new_w + hgap + c.width;
if (next_w <= d.width || ! found_one)
{ {
new_w = next_w; // Skip invisible items.
new_h = Math.max (new_h, c.height); if (! comps[i].visible)
found_one = true; continue;
Dimension c = comps[i].getPreferredSize ();
int next_w = new_w + hgap + c.width;
if (next_w <= d.width || ! found_one)
{
new_w = next_w;
new_h = Math.max (new_h, c.height);
found_one = true;
}
else
{
// Must start a new row, and we already found an item
break;
}
} }
else
{
// Must start a new row, and we already found an item
break;
}
}
// Set the location of each component for this row. // Set the location of each component for this row.
int x; int x;
int myalign = align; int myalign = align;
if (align == LEADING) if (align == LEADING)
myalign = left_to_right ? LEFT : RIGHT; myalign = left_to_right ? LEFT : RIGHT;
else if (align == TRAILING) else if (align == TRAILING)
myalign = left_to_right ? RIGHT : LEFT; myalign = left_to_right ? RIGHT : LEFT;
if (myalign == LEFT) if (myalign == LEFT)
x = ins.left + hgap; x = ins.left + hgap;
else if (myalign == CENTER) else if (myalign == CENTER)
x = (d.width - new_w) / 2; x = (d.width - new_w) / 2;
else else
x = d.width - new_w; x = d.width - new_w;
for (int k = i; k < j; ++k) for (int k = i; k < j; ++k)
{
if (comps[k].visible)
{ {
Dimension c = comps[k].getPreferredSize (); if (comps[k].visible)
comps[k].setBounds (x, y, c.width, new_h); {
x += c.width + hgap; Dimension c = comps[k].getPreferredSize ();
comps[k].setBounds (x, y, c.width, new_h);
x += c.width + hgap;
}
} }
}
// Advance to next row. // Advance to next row.
i = j; i = j;
y += new_h + vgap; y += new_h + vgap;
}
} }
} }
...@@ -304,36 +307,39 @@ public class FlowLayout implements LayoutManager, Serializable ...@@ -304,36 +307,39 @@ public class FlowLayout implements LayoutManager, Serializable
// This method is used to compute the various sizes. // This method is used to compute the various sizes.
private Dimension getSize (Container parent, boolean is_min) private Dimension getSize (Container parent, boolean is_min)
{ {
int w, h, num = parent.getComponentCount (); synchronized (parent.getTreeLock ())
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
w = 0;
h = 0;
for (int i = 0; i < num; ++i)
{ {
if (! comps[i].visible) int w, h, num = parent.getComponentCount ();
continue; // This is more efficient than calling getComponents().
Component[] comps = parent.component;
// FIXME: can we just directly read the fields in Component? w = 0;
// Or will that not work with subclassing? h = 0;
Dimension d; for (int i = 0; i < num; ++i)
{
if (! comps[i].visible)
continue;
if (is_min) // FIXME: can we just directly read the fields in Component?
d = comps[i].getMinimumSize (); // Or will that not work with subclassing?
else Dimension d;
d = comps[i].getPreferredSize ();
w += d.width; if (is_min)
h = Math.max (d.height, h); d = comps[i].getMinimumSize ();
} else
d = comps[i].getPreferredSize ();
Insets ins = parent.getInsets (); w += d.width;
h = Math.max (d.height, h);
}
w += (num + 1) * hgap + ins.left + ins.right; Insets ins = parent.getInsets ();
h += 2 * vgap + ins.top + ins.bottom;
return new Dimension (w, h); w += (num + 1) * hgap + ins.left + ins.right;
h += 2 * vgap + ins.top + ins.bottom;
return new Dimension (w, h);
}
} }
/** /**
......
...@@ -153,62 +153,65 @@ public class GridLayout implements LayoutManager, Serializable ...@@ -153,62 +153,65 @@ public class GridLayout implements LayoutManager, Serializable
*/ */
public void layoutContainer (Container parent) public void layoutContainer (Container parent)
{ {
int num = parent.ncomponents; synchronized (parent.getTreeLock ())
// There's no point, and handling this would mean adding special
// cases.
if (num == 0)
return;
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
int real_rows = rows;
int real_cols = cols;
if (real_rows == 0)
real_rows = (num + real_cols - 1) / real_cols;
else
real_cols = (num + real_rows - 1) / real_rows;
// We might have less than a single row. In this case we expand
// to fill.
if (num < real_cols)
real_cols = num;
Dimension d = parent.getSize ();
Insets ins = parent.getInsets ();
// Compute width and height of each cell in the grid.
int tw = d.width - ins.left - ins.right;
tw = (tw - (real_cols - 1) * hgap) / real_cols;
int th = d.height - ins.top - ins.bottom;
th = (th - (real_rows - 1) * vgap) / real_rows;
// If the cells are too small, still try to do something.
if (tw < 0)
tw = 1;
if (th < 0)
th = 1;
int x = ins.left;
int y = ins.top;
int i = 0;
int recount = 0;
while (i < num)
{ {
comps[i].setBounds (x, y, tw, th); int num = parent.ncomponents;
++i; // There's no point, and handling this would mean adding special
++recount; // cases.
if (recount == real_cols) if (num == 0)
return;
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
int real_rows = rows;
int real_cols = cols;
if (real_rows == 0)
real_rows = (num + real_cols - 1) / real_cols;
else
real_cols = (num + real_rows - 1) / real_rows;
// We might have less than a single row. In this case we expand
// to fill.
if (num < real_cols)
real_cols = num;
Dimension d = parent.getSize ();
Insets ins = parent.getInsets ();
// Compute width and height of each cell in the grid.
int tw = d.width - ins.left - ins.right;
tw = (tw - (real_cols - 1) * hgap) / real_cols;
int th = d.height - ins.top - ins.bottom;
th = (th - (real_rows - 1) * vgap) / real_rows;
// If the cells are too small, still try to do something.
if (tw < 0)
tw = 1;
if (th < 0)
th = 1;
int x = ins.left;
int y = ins.top;
int i = 0;
int recount = 0;
while (i < num)
{ {
recount = 0; comps[i].setBounds (x, y, tw, th);
y += vgap + th;
x = ins.left; ++i;
++recount;
if (recount == real_cols)
{
recount = 0;
y += vgap + th;
x = ins.left;
}
else
x += hgap + tw;
} }
else
x += hgap + tw;
} }
} }
...@@ -301,36 +304,39 @@ public class GridLayout implements LayoutManager, Serializable ...@@ -301,36 +304,39 @@ public class GridLayout implements LayoutManager, Serializable
// This method is used to compute the various sizes. // This method is used to compute the various sizes.
private Dimension getSize (Container parent, boolean is_min) private Dimension getSize (Container parent, boolean is_min)
{ {
int w = 0, h = 0, num = parent.ncomponents; synchronized (parent.getTreeLock ())
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
for (int i = 0; i < num; ++i)
{ {
Dimension d; int w = 0, h = 0, num = parent.ncomponents;
// This is more efficient than calling getComponents().
Component[] comps = parent.component;
if (is_min) for (int i = 0; i < num; ++i)
d = comps[i].getMinimumSize (); {
else Dimension d;
d = comps[i].getPreferredSize ();
w = Math.max (d.width, w); if (is_min)
h = Math.max (d.height, h); d = comps[i].getMinimumSize ();
} else
d = comps[i].getPreferredSize ();
w = Math.max (d.width, w);
h = Math.max (d.height, h);
}
int real_rows = rows; int real_rows = rows;
int real_cols = cols; int real_cols = cols;
if (real_rows == 0) if (real_rows == 0)
real_rows = (num + real_cols - 1) / real_cols; real_rows = (num + real_cols - 1) / real_cols;
else else
real_cols = (num + real_rows - 1) / real_rows; real_cols = (num + real_rows - 1) / real_rows;
Insets ins = parent.getInsets (); Insets ins = parent.getInsets ();
// We subtract out an extra gap here because the gaps are only // We subtract out an extra gap here because the gaps are only
// between cells. // between cells.
w = ins.left + ins.right + real_cols * (w + hgap) - hgap; w = ins.left + ins.right + real_cols * (w + hgap) - hgap;
h = ins.top + ins.bottom + real_rows * (h + vgap) - vgap; h = ins.top + ins.bottom + real_rows * (h + vgap) - vgap;
return new Dimension (w, h); return new Dimension (w, h);
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment