classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Fix for BoxLayout


From: Roman Kennke
Subject: [cp-patches] FYI: Fix for BoxLayout
Date: Fri, 24 Jun 2005 16:44:37 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

I have found a layout bug in an application that uses BoxLayout. The problem was that this app had components that are invisible. The layoutmanager did not recognize this and laid out the components anyway. This leads to a garbled layout.

I added a class gnu.java.awt.AWTUtilities (no package private class in java.awt or javax.swing because this utility method will be used by LayoutManagers in both AWT and Swing) with a utility method getVisibleChildren() that can be called as a substitute for Container.getComponents() in LayoutManagers.

2005-06-24  Roman Kennke  <address@hidden>

       * gnu/java/awt/AWTUtilities.java:
       Added new utility class. This provides a method for fetching
       the visible children of a Container.
       * javax/swing/BoxLayout.java:
       Use AWTUtilities.getVisibleChildren() instead of
       Container.getComponents(). LayoutManagers must not layout invisible
       children.

/Roman

Index: gnu/java/awt/AWTUtilities.java
===================================================================
RCS file: gnu/java/awt/AWTUtilities.java
diff -N gnu/java/awt/AWTUtilities.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ gnu/java/awt/AWTUtilities.java      24 Jun 2005 14:38:33 -0000
@@ -0,0 +1,69 @@
+/* AWTUtilities.java -- Common utility methods for AWT and Swing.
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.java.awt;
+
+import java.awt.Component;
+import java.awt.Container;
+import java.util.Vector;
+
+/**
+ * This class provides utility methods that are commonly used in AWT
+ * (and Swing).
+ */
+public class AWTUtilities
+{
+
+  /**
+   * Returns the visible children of a address@hidden Container}. This method 
is
+   * commonly needed in LayoutManagers, because they only have to layout
+   * the visible children of a Container.
+   *
+   * @param c the Container from which to extract the visible children
+   *
+   * @return the visible children of <code>c</code>
+   */
+  public static Component[] getVisibleChildren(Container c)
+  {
+    Component[] children = c.getComponents();
+    Vector visible = new Vector();
+    for (int i = 0; i < children.length; i++)
+      if (children[i].isVisible())
+        visible.add(children[i]);
+    return (Component[]) visible.toArray(new Container[visible.size()]);
+  }
+}
Index: javax/swing/BoxLayout.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/BoxLayout.java,v
retrieving revision 1.11
diff -u -r1.11 BoxLayout.java
--- javax/swing/BoxLayout.java  22 May 2005 20:35:05 -0000      1.11
+++ javax/swing/BoxLayout.java  24 Jun 2005 14:38:33 -0000
@@ -46,6 +46,8 @@
 import java.awt.LayoutManager2;
 import java.io.Serializable;
 
+import gnu.java.awt.AWTUtilities;
+
 /**
  * A layout for swing components.
  *
@@ -151,7 +153,7 @@
     int x = 0;
     int y = 0;
 
-    Component[] children = parent.getComponents();
+    Component[] children = AWTUtilities.getVisibleChildren(parent);
 
     if (isHorizontalIn(parent))
       {        
@@ -201,7 +203,7 @@
     int x = insets.left + insets.right;
     int y = insets.bottom + insets.top;
 
-    Component[] children = parent.getComponents();
+    Component[] children = AWTUtilities.getVisibleChildren(parent);
 
     if (isHorizontalIn(parent))
       {
@@ -246,7 +248,7 @@
     Dimension innerSize = new Dimension(size.width - insets.left
                                         - insets.right, size.height
                                         - insets.bottom - insets.top);
-    Component[] children = parent.getComponents();
+    Component[] children = AWTUtilities.getVisibleChildren(parent);
     boolean[] laidOut = new boolean[children.length];
     for (int index = 0; index < laidOut.length; index++)
       laidOut[index] = false;
@@ -465,7 +467,7 @@
     int x = insets.left + insets.right;
     int y = insets.top + insets.bottom;
 
-    Component[] children = parent.getComponents();
+    Component[] children = AWTUtilities.getVisibleChildren(parent);
 
     if (isHorizontalIn(parent))
       {

reply via email to

[Prev in Thread] Current Thread [Next in Thread]