This patch (committed) fixes a minor bug (a null component should result in a null return value, not a NullPointerException) and also contains an optimization - the call to getLocalBounds() always creates a new Rectangle with b.x == 0 and b.y == 0, so the same result can be achieved by calling getBounds(r) and then updating r.x and r.y appropriately, avoiding an unnecessary instance creation.

2006-03-13  David Gilbert  <[EMAIL PROTECTED]>

        * javax/swing/SwingUtilities.java
        (calculateInnerArea): handle null component, and replace
        getLocalBounds() with getBounds(Rectangle) to avoid unnecessary object
        creation.

A Mauve test to cover this change has been added to Mauve CVS.

Regards,

Dave
Index: javax/swing/SwingUtilities.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/SwingUtilities.java,v
retrieving revision 1.47
diff -u -r1.47 SwingUtilities.java
--- javax/swing/SwingUtilities.java     13 Mar 2006 20:51:58 -0000      1.47
+++ javax/swing/SwingUtilities.java     13 Mar 2006 21:29:38 -0000
@@ -1,5 +1,5 @@
 /* SwingUtilities.java --
-   Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2005, 2006,  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -91,23 +91,23 @@
    * of the <em>component's</em> coordinate system, where (0,0) is the
    * upper left corner of the component's bounds.
    *
-   * @param c The component to measure the bounds of
-   * @param r A Rectangle to store the return value in, or
-   * <code>null</code>
+   * @param c  the component to measure the bounds of (if <code>null</code>, 
+   *     this method returns <code>null</code>).
+   * @param r  a carrier to store the return value in (if <code>null</code>, a
+   *     new <code>Rectangle</code> instance is created).
    *
-   * @return The calculated area inside the component and its border
-   * insets
+   * @return The calculated area inside the component and its border insets.
    */
   public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
   {
-    Rectangle b = getLocalBounds(c);
-    if (r == null)
-      r = new Rectangle();
+    if (c == null)
+      return null;
+    r = c.getBounds(r);
     Insets i = c.getInsets();
-    r.x = b.x + i.left;
-    r.width = b.width - i.left - i.right;
-    r.y = b.y + i.top;
-    r.height = b.height - i.top - i.bottom;
+    r.x = i.left;
+    r.width = r.width - i.left - i.right;
+    r.y = i.top;
+    r.height = r.height - i.top - i.bottom;
     return r;
   }
 

Reply via email to