Hi,

I made the BoxLayout class thread-safe. I could find no better way to
protect the BoxLayout inner state from getting inconsistent when
multiple threads access this class like pointed out in Bug#24359.

2005-10-24  Roman Kennke  <[EMAIL PROTECTED]>

        Fixes Bug #24359
        * javax/swing/BoxLayout.java:
        Made class thread safe.

/Roman
Index: javax/swing/BoxLayout.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/BoxLayout.java,v
retrieving revision 1.23
diff -u -r1.23 BoxLayout.java
--- javax/swing/BoxLayout.java	19 Oct 2005 15:45:03 -0000	1.23
+++ javax/swing/BoxLayout.java	24 Oct 2005 08:57:28 -0000
@@ -86,7 +86,7 @@
    */
   private Container container;
   
-  /*
+  /**
    * Current type of component layouting. Defaults to X_AXIS.
    */
   private int way = X_AXIS;
@@ -189,13 +189,16 @@
    */
   public Dimension preferredLayoutSize(Container parent)
   {
-    if (container != parent)
-      throw new AWTError("BoxLayout can't be shared");
-
-    checkTotalRequirements();
-    Insets i = container.getInsets();
-    return new Dimension(xTotal.preferred + i.left + i.right,
-                         yTotal.preferred + i.top + i.bottom);
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
+
+        checkTotalRequirements();
+        Insets i = container.getInsets();
+        return new Dimension(xTotal.preferred + i.left + i.right,
+                             yTotal.preferred + i.top + i.bottom);
+      }
   }
 
   /**
@@ -207,11 +210,14 @@
    */
   public Dimension minimumLayoutSize(Container parent)
   {
-    if (container != parent)
-      throw new AWTError("BoxLayout can't be shared");
-
-    checkTotalRequirements();
-    return new Dimension(xTotal.minimum, yTotal.minimum);
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
+
+        checkTotalRequirements();
+        return new Dimension(xTotal.minimum, yTotal.minimum);
+      }
   }
 
   /**
@@ -221,18 +227,18 @@
    */
   public void layoutContainer(Container parent)
   {
-    synchronized(container.getTreeLock())
-    {
-      if (container != parent)
-        throw new AWTError("BoxLayout can't be shared");
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
       
-      checkLayout();
-      Component[] children = container.getComponents();
-      Insets in = container.getInsets();
-      for (int i = 0; i < children.length; i++)
-        children[i].setBounds(offsetsX[i] + in.left, offsetsY[i] + in.top,
-                              spansX[i], spansY[i]);
-    }
+        checkLayout();
+        Component[] children = container.getComponents();
+        Insets in = container.getInsets();
+        for (int i = 0; i < children.length; i++)
+          children[i].setBounds(offsetsX[i] + in.left, offsetsY[i] + in.top,
+                                spansX[i], spansY[i]);
+      }
   }
 
   /**
@@ -255,11 +261,14 @@
    */
   public float getLayoutAlignmentX(Container parent)
   {
-    if (container != parent)
-      throw new AWTError("BoxLayout can't be shared");
-
-    checkTotalRequirements();
-    return xTotal.alignment;
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
+
+        checkTotalRequirements();
+        return xTotal.alignment;
+      }
   }
 
   /**
@@ -271,11 +280,14 @@
    */
   public float getLayoutAlignmentY(Container parent)
   {
-    if (container != parent)
-      throw new AWTError("BoxLayout can't be shared");
-
-    checkTotalRequirements();
-    return yTotal.alignment;
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
+
+        checkTotalRequirements();
+        return yTotal.alignment;
+      }
   }
 
   /**
@@ -285,14 +297,17 @@
    */
   public void invalidateLayout(Container parent)
   {
-    xChildren = null;
-    yChildren = null;
-    xTotal = null;
-    yTotal = null;
-    offsetsX = null;
-    offsetsY = null;
-    spansX = null;
-    spansY = null;
+    synchronized (container.getTreeLock())
+      {
+        xChildren = null;
+        yChildren = null;
+        xTotal = null;
+        yTotal = null;
+        offsetsX = null;
+        offsetsY = null;
+        spansX = null;
+        spansY = null;
+      }
   }
 
   /**
@@ -305,11 +320,14 @@
    */
   public Dimension maximumLayoutSize(Container parent)
   {
-    if (container != parent)
-      throw new AWTError("BoxLayout can't be shared");
-
-    checkTotalRequirements();
-    return new Dimension(xTotal.maximum, yTotal.maximum);
+    synchronized (container.getTreeLock())
+      {
+        if (container != parent)
+          throw new AWTError("BoxLayout can't be shared");
+
+        checkTotalRequirements();
+        return new Dimension(xTotal.maximum, yTotal.maximum);
+      }
   }
 
   /**
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to