classpath-patches
[Top][All Lists]
Advanced

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

Re: [cp-patches] Proposed: javax.swing.UIManager patch


From: David Gilbert
Subject: Re: [cp-patches] Proposed: javax.swing.UIManager patch
Date: Tue, 23 Aug 2005 15:58:35 +0000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050728)

David Gilbert wrote:

Roman Kennke wrote:

Am Dienstag, den 23.08.2005, 13:27 +0000 schrieb David Gilbert:
This patch does two things primarily:

(1) corrects a problem where every call to "get" a default value ends up calling the getDefaults() method in the current LookAndFeel, which *creates* a new table of defaults each time it is called (the API docs say it should only be called once - a better name for the method would be createDefaults()). The solution is to retain a reference to the UIDefaults for the current look and feel, and use that reference in getLookAndFeelDefaults() (and change all the get methods to use getLookAndFeelDefaults());


Sounds reasonable.

(2) implements the property change mechanism in UIManager using the SwingPropertyChangeSupport class.


Dito.

I've also added API doc comments. If the patch is too messy, I'll take some more time to split it into the relevant pieces.


I think many people prefer to have documentation changes and code
changes separate. It would be nice to have this split up. Besides that,
please go ahead and commit.


/Roman

OK, I've committed the first part that implements the property change listener mechanism:

2005-08-23  David Gilbert  <address@hidden>

   * javax/swing/UIManager.java:
   (addPropertyChangeListener): implemented,
   (removePropertyChangeListener): likewise,
   (getPropertyChangeListeners): likewise,
   (setLookAndFeel): fire a property change event.

More to follow.

Regards,

Dave Gilbert


Here is the second part (committed) that caches the UIDefaults from the current look and feel when it is first set:

2005-08-23  David Gilbert  <address@hidden>

   * javax/swing/UIManager.java:
   (look_and_feel): renamed currentLookAndFeel,
   (currentUIDefaults): new field,
   (get(Object)): access cached UIDefaults,
   (get(Object, Locale)): likewise,
   (getBoolean(Object)): likewise,
   (getBoolean(Object, Locale)): likewise,
   (getBorder(Object)): likewise,
   (getBorder(Object, Locale)): likewise,
   (getColor(Object)): likewise,
   (getColor(Object, Locale)): likewise,
       (getDefaults): return reference to UIDefaults from current look and
   feel rather than recreating them every time,
   (getDimension(Object)): access local defaults,
   (getDimension(Object, Locale)): likewise,
   (getFont(Object)): likewise,
   (getFont(Object, Locale)): likewise,
   (getIcon(Object)): likewise,
   (getIcon(Object, Locale)): likewise,
   (getInsets(Object)): likewise,
   (getInsets(Object, Locale)): likewise,
   (getInt(Object)): likewise,
   (getInt(Object, Locale)): likewise,
       (getLookAndFeel): renamed attribute,
   (getLookAndFeelDefaults): return reference to UIDefaults from current
   look and feel rather than recreating them every time,
   (getString(Object)): access local defaults,
   (getString(Object, Locale)): likewise,
   (getUI(JComponent)): likewise,
   (installLookAndFeel(String, String)): implemented by delegation,
   (put(Object, Object)): update local defaults,
   (setLookAndFeel): create and retain reference to UIDefaults.

Regards,

Dave



Index: javax/swing/UIManager.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/UIManager.java,v
retrieving revision 1.23
diff -u -r1.23 UIManager.java
--- javax/swing/UIManager.java  23 Aug 2005 14:30:05 -0000      1.23
+++ javax/swing/UIManager.java  23 Aug 2005 14:48:26 -0000
@@ -100,7 +100,9 @@
 
   static LookAndFeel[] aux_installed;
   
-  static LookAndFeel look_and_feel = new MetalLookAndFeel();
+  static LookAndFeel currentLookAndFeel;
+  
+  static UIDefaults currentUIDefaults;
 
   /** Property change listener mechanism. */
   static SwingPropertyChangeSupport listeners 
@@ -123,8 +125,12 @@
         System.err.println("error: " + ex.getMessage());
         System.err.println("falling back to Metal Look and Feel");
       }
-  }
+    currentLookAndFeel = new MetalLookAndFeel();
+    currentLookAndFeel.initialize();
+    currentUIDefaults = currentLookAndFeel.getDefaults();
 
+  }
+  
   public UIManager()
   {
     // Do nothing here.
@@ -205,14 +211,14 @@
     return aux_installed;
   }
 
-  public static  Object get(Object key)
+  public static Object get(Object key)
   {
-    return getLookAndFeel().getDefaults().get(key);
+    return getLookAndFeelDefaults().get(key);
   }
 
-  public static  Object get(Object key, Locale locale)
+  public static Object get(Object key, Locale locale)
   {
-    return getLookAndFeel().getDefaults().get(key ,locale);
+    return getLookAndFeelDefaults().get(key ,locale);
   }
 
   /**
@@ -223,7 +229,7 @@
    */
   public static boolean getBoolean(Object key)
   {
-    Boolean value = (Boolean) getLookAndFeel().getDefaults().get(key);
+    Boolean value = (Boolean) getLookAndFeelDefaults().get(key);
     return value != null ? value.booleanValue() : false;
   }
   
@@ -235,7 +241,7 @@
    */
   public static boolean getBoolean(Object key, Locale locale)
   {
-    Boolean value = (Boolean) getLookAndFeel().getDefaults().get(key, locale);
+    Boolean value = (Boolean) getLookAndFeelDefaults().get(key, locale);
     return value != null ? value.booleanValue() : false;
   }
     
@@ -244,7 +250,7 @@
    */
   public static Border getBorder(Object key)
   {
-    return (Border) getLookAndFeel().getDefaults().get(key);
+    return (Border) getLookAndFeelDefaults().get(key);
   }
     
   /**
@@ -254,29 +260,29 @@
    */
   public static Border getBorder(Object key, Locale locale)
   {
-    return (Border) getLookAndFeel().getDefaults().get(key, locale);
+    return (Border) getLookAndFeelDefaults().get(key, locale);
   }
     
   /**
    * Returns a drawing color from the defaults table. 
    */
-  public static  Color getColor(Object key)
+  public static Color getColor(Object key)
   {
-    return (Color) getLookAndFeel().getDefaults().get(key);
+    return (Color) getLookAndFeelDefaults().get(key);
   }
 
   /**
    * Returns a drawing color from the defaults table. 
    */
-  public static  Color getColor(Object key, Locale locale)
+  public static Color getColor(Object key, Locale locale)
   {
-    return (Color) getLookAndFeel().getDefaults().get(key);
+    return (Color) getLookAndFeelDefaults().get(key);
   }
 
   /**
    * this string can be passed to Class.forName()
    */
-  public static  String getCrossPlatformLookAndFeelClassName()
+  public static String getCrossPlatformLookAndFeelClassName()
   {    
     return "javax.swing.plaf.metal.MetalLookAndFeel";
   }
@@ -286,7 +292,7 @@
    */
   public static UIDefaults getDefaults()
   {
-    return getLookAndFeel().getDefaults();
+    return currentUIDefaults;
   }
 
   /**
@@ -294,7 +300,7 @@
    */
   public static Dimension getDimension(Object key)
   {
-    return (Dimension) getLookAndFeel().getDefaults().get(key);
+    return (Dimension) getLookAndFeelDefaults().get(key);
   }
 
   /**
@@ -302,7 +308,7 @@
    */
   public static Dimension getDimension(Object key, Locale locale)
   {
-    return (Dimension) getLookAndFeel().getDefaults().get(key, locale);
+    return (Dimension) getLookAndFeelDefaults().get(key, locale);
   }
 
   /**
@@ -315,7 +321,7 @@
    */
   public static Font getFont(Object key)
   {
-    return (Font) getLookAndFeel().getDefaults().get(key);
+    return (Font) getLookAndFeelDefaults().get(key);
   }
 
   /**
@@ -328,7 +334,7 @@
    */
   public static Font getFont(Object key, Locale locale)
   {
-    return (Font) getLookAndFeel().getDefaults().get(key ,locale);
+    return (Font) getLookAndFeelDefaults().get(key ,locale);
   }
 
   /**
@@ -336,7 +342,7 @@
    */
   public static Icon getIcon(Object key)
   {
-    return (Icon) getLookAndFeel().getDefaults().get(key);
+    return (Icon) getLookAndFeelDefaults().get(key);
   }
   
   /**
@@ -344,7 +350,7 @@
    */
   public static Icon getIcon(Object key, Locale locale)
   {
-    return (Icon) getLookAndFeel().getDefaults().get(key, locale);
+    return (Icon) getLookAndFeelDefaults().get(key, locale);
   }
   
   /**
@@ -352,7 +358,7 @@
    */
   public static Insets getInsets(Object key)
   {
-    return (Insets) getLookAndFeel().getDefaults().getInsets(key);
+    return getLookAndFeelDefaults().getInsets(key);
   }
 
   /**
@@ -360,7 +366,7 @@
    */
   public static Insets getInsets(Object key, Locale locale)
   {
-    return (Insets) getLookAndFeel().getDefaults().getInsets(key, locale);
+    return getLookAndFeelDefaults().getInsets(key, locale);
   }
 
   public static LookAndFeelInfo[] getInstalledLookAndFeels()
@@ -370,7 +376,7 @@
 
   public static int getInt(Object key)
   {
-    Integer x = (Integer) getLookAndFeel().getDefaults().get(key);
+    Integer x = (Integer) getLookAndFeelDefaults().get(key);
     if (x == null)
       return 0;
     return x.intValue();
@@ -378,7 +384,7 @@
 
   public static int getInt(Object key, Locale locale)
   {
-    Integer x = (Integer) getLookAndFeel().getDefaults().get(key, locale);
+    Integer x = (Integer) getLookAndFeelDefaults().get(key, locale);
     if (x == null)
       return 0;
     return x.intValue();
@@ -386,7 +392,7 @@
 
   public static LookAndFeel getLookAndFeel()
   {
-    return look_and_feel;
+    return currentLookAndFeel;
   }
 
   /**
@@ -395,7 +401,7 @@
    */
   public static UIDefaults getLookAndFeelDefaults()
   {
-    return getLookAndFeel().getDefaults();
+    return currentUIDefaults;
   }
 
   /**
@@ -403,7 +409,7 @@
    */
   public static String getString(Object key)
   {
-    return (String) getLookAndFeel().getDefaults().get(key);
+    return (String) getLookAndFeelDefaults().get(key);
   }
   
   /**
@@ -411,7 +417,7 @@
    */
   public static String getString(Object key, Locale locale)
   {
-    return (String) getLookAndFeel().getDefaults().get(key, locale);
+    return (String) getLookAndFeelDefaults().get(key, locale);
   }
   
   /**
@@ -429,7 +435,7 @@
    */
   public static ComponentUI getUI(JComponent target)
   {
-    return getDefaults().getUI(target);
+    return getLookAndFeelDefaults().getUI(target);
   }
 
   /**
@@ -452,7 +458,7 @@
    */
   public static Object put(Object key, Object value)
   {
-    return getLookAndFeel().getDefaults().put(key,value);
+    return getLookAndFeelDefaults().put(key,value);
   }
 
   /**
@@ -471,14 +477,21 @@
     if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
       throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
     
-    LookAndFeel oldLookAndFeel = look_and_feel;
+    LookAndFeel oldLookAndFeel = currentLookAndFeel;
     if (oldLookAndFeel != null)
       oldLookAndFeel.uninitialize();
 
     // Set the current default look and feel using a LookAndFeel object. 
-    look_and_feel = newLookAndFeel;
-    look_and_feel.initialize();
-       
+    currentLookAndFeel = newLookAndFeel;
+    if (newLookAndFeel != null)
+      {
+        newLookAndFeel.initialize();
+        currentUIDefaults = newLookAndFeel.getDefaults();
+      }
+    else
+      {
+        currentUIDefaults = null;    
+      }
     listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, 
newLookAndFeel);
     //revalidate();
     //repaint();

reply via email to

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