This patch (committed) fixes a couple of minor details in BasicPanelUI.java:

2006-05-12  David Gilbert  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicPanelUI.java
        (sharedUI): New field,
        (createUI): Return a shared instance rather than a new instance,
        (installUI): Reformatted and added API docs,
        (installDefaults): Install border if one is defined,
        (uninstallDefaults): Uninstall border.

I planned to just add API docs to this class but when I wrote a small test to determine the behaviour in the reference implementation of a null argument in uninstallDefaults() I saw that it throws a NullPointerException in LookAndFeel.uninstallBorder(). That's strange, because BasicLookAndFeel doesn't define a border in the UIDefaults table. But another test shows that it will install "Panel.border" if this is added to the UIDefaults table. My guess is that another look and feel needs this (perhaps the Windows or Motif look-and-feel?).

One other change: I noticed the UI delegate is stateless, so a single instance can be shared by all JPanel components. A quick test shows that the reference implementation does this, so now we do as well.

Regards,

Dave
Index: javax/swing/plaf/basic/BasicPanelUI.java
===================================================================
RCS file: 
/sources/classpath/classpath/javax/swing/plaf/basic/BasicPanelUI.java,v
retrieving revision 1.10
diff -u -r1.10 BasicPanelUI.java
--- javax/swing/plaf/basic/BasicPanelUI.java    13 Mar 2006 22:06:47 -0000      
1.10
+++ javax/swing/plaf/basic/BasicPanelUI.java    12 May 2006 14:25:28 -0000
@@ -1,5 +1,5 @@
 /* BasicPanelUI.java
-   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -44,33 +44,68 @@
 import javax.swing.plaf.ComponentUI;
 import javax.swing.plaf.PanelUI;
 
+/**
+ * A UI delegate for the [EMAIL PROTECTED] JPanel} component.
+ */
 public class BasicPanelUI extends PanelUI
 {
-  public static ComponentUI createUI(JComponent x) 
+  /**
+   * A UI delegate that can be shared by all panels (because the delegate is
+   * stateless).
+   */
+  static BasicPanelUI sharedUI;
+  
+  /**
+   * Returns a UI delegate for the specified component.
+   * 
+   * @param panel  the panel.
+   */
+  public static ComponentUI createUI(JComponent panel) 
   {
-    return new BasicPanelUI();
+    if (sharedUI == null)
+      sharedUI = new BasicPanelUI();
+    return sharedUI;
   }
 
+  /**
+   * Installs this UI delegate in the specified component.
+   * 
+   * @param c  the component (should be a [EMAIL PROTECTED] JPanel}, 
<code>null</code> not
+   *     permitted).
+   */
   public void installUI(JComponent c)
   {
     super.installUI(c);
     if (c instanceof JPanel)
       {
-       JPanel p = (JPanel) c;
-       installDefaults(p);
+        JPanel p = (JPanel) c;
+        installDefaults(p);
       }
   }
 
+  /**
+   * Installs the defaults for this UI delegate in the specified panel.
+   * 
+   * @param p  the panel (<code>null</code> not permitted).
+   */
   protected void installDefaults(JPanel p)
   {
     LookAndFeel.installColorsAndFont(p, "Panel.background", "Panel.foreground",
                                      "Panel.font");
+    
+    // A test against the reference implementation shows that this method will
+    // install a border if one is defined in the UIDefaults table (even though
+    // the BasicLookAndFeel doesn't actually define a "Panel.border").  This
+    // test was written after discovering that a null argument to 
+    // uninstallDefaults throws a NullPointerException in 
+    // LookAndFeel.uninstallBorder()...
+    LookAndFeel.installBorder(p, "Panel.border");
   }
 
   /**
-   * Uninstalls this UI from the JPanel.
+   * Uninstalls this UI delegate from the specified component.
    *
-   * @param c the JPanel from which to uninstall this UI
+   * @param c the component (<code>null</code> not permitted).
    */
   public void uninstallUI(JComponent c)
   {
@@ -78,13 +113,20 @@
   }
 
   /**
-   * Uninstalls the UI defaults that have been install through
-   * [EMAIL PROTECTED] #installDefaults}.
+   * Uninstalls the UI defaults for the specified panel.
    *
-   * @param p the panel from which to uninstall the UI defaults
+   * @param p  the panel (<code>null</code> not permitted).
    */
   protected void uninstallDefaults(JPanel p)
   {
-    // Nothing to do here.
+    // Tests on the reference implementation showed this method:
+    // (1) doesn't actually remove the installed colors and font installed
+    //     by installDefaults(), it isn't necessary;
+    // (2) throws a NullPointerException in LookAndFeel.uninstallBorder() if
+    //     p is null.  Strangely, no border is installed by the 
+    //     BasicLookAndFeel - perhaps this is needed by another LAF?
+    
+    LookAndFeel.uninstallBorder(p);
   }
+  
 }

Reply via email to