jdk/src/share/classes/javax/swing/plaf/nimbus/Defaults.template
changeset 4841 ae658e3b0f27
parent 3932 c9cd7ff79037
child 5506 202f599c92aa
equal deleted inserted replaced
4840:74d64c277d62 4841:ae658e3b0f27
    88      * prefix in a very fast manner. This is used in the "matches" method of
    88      * prefix in a very fast manner. This is used in the "matches" method of
    89      * LazyStyle.
    89      * LazyStyle.
    90      */
    90      */
    91     private Map<String, Region> registeredRegions =
    91     private Map<String, Region> registeredRegions =
    92             new HashMap<String, Region>();
    92             new HashMap<String, Region>();
       
    93 
       
    94     private Map<JComponent, Map<Region, SynthStyle>> overridesCache =
       
    95             new WeakHashMap<JComponent, Map<Region, SynthStyle>>();
       
    96     
    93     /**
    97     /**
    94      * Our fallback style to avoid NPEs if the proper style cannot be found in
    98      * Our fallback style to avoid NPEs if the proper style cannot be found in
    95      * this class. Not sure if relying on DefaultSynthStyle is the best choice.
    99      * this class. Not sure if relying on DefaultSynthStyle is the best choice.
    96      */
   100      */
    97     private DefaultSynthStyle defaultStyle;
   101     private DefaultSynthStyle defaultStyle;
   249                 }
   253                 }
   250             }
   254             }
   251         }
   255         }
   252 
   256 
   253         //return the style, if found, or the default style if not found
   257         //return the style, if found, or the default style if not found
   254         return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp);
   258         return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp, r);
       
   259     }
       
   260 
       
   261     public void clearOverridesCache(JComponent c) {
       
   262         overridesCache.remove(c);
   255     }
   263     }
   256 
   264 
   257     /*
   265     /*
   258         Various public helper classes.
   266         Various public helper classes.
   259         These may be used to register 3rd party values into UIDefaults
   267         These may be used to register 3rd party values into UIDefaults
   455         private Part[] parts;
   463         private Part[] parts;
   456         /**
   464         /**
   457          * Cached shared style.
   465          * Cached shared style.
   458          */
   466          */
   459         private NimbusStyle style;
   467         private NimbusStyle style;
   460         /**
       
   461          * A weakly referenced hash map such that if the reference JComponent
       
   462          * key is garbage collected then the entry is removed from the map.
       
   463          * This cache exists so that when a JComponent has nimbus overrides
       
   464          * in its client map, a unique style will be created and returned
       
   465          * for that JComponent instance, always. In such a situation each
       
   466          * JComponent instance must have its own instance of NimbusStyle.
       
   467          */
       
   468         private WeakHashMap<JComponent, WeakReference<NimbusStyle>> overridesCache;
       
   469 
   468 
   470         /**
   469         /**
   471          * Create a new LazyStyle.
   470          * Create a new LazyStyle.
   472          *
   471          *
   473          * @param prefix The prefix associated with this style. Cannot be null.
   472          * @param prefix The prefix associated with this style. Cannot be null.
   511 
   510 
   512         /**
   511         /**
   513          * Gets the style. Creates it if necessary.
   512          * Gets the style. Creates it if necessary.
   514          * @return the style
   513          * @return the style
   515          */
   514          */
   516         SynthStyle getStyle(JComponent c) {
   515         SynthStyle getStyle(JComponent c, Region r) {
   517             // if the component has overrides, it gets its own unique style
   516             // if the component has overrides, it gets its own unique style
   518             // instead of the shared style.
   517             // instead of the shared style.
   519             if (c.getClientProperty("Nimbus.Overrides") != null) {
   518             if (c.getClientProperty("Nimbus.Overrides") != null) {
   520                 if (overridesCache == null)
   519                 Map<Region, SynthStyle> map = overridesCache.get(c);
   521                     overridesCache = new WeakHashMap<JComponent, WeakReference<NimbusStyle>>();
   520                 SynthStyle s = null;
   522                 WeakReference<NimbusStyle> ref = overridesCache.get(c);
   521                 if (map == null) {
   523                 NimbusStyle s = ref == null ? null : ref.get();
   522                     map = new HashMap<Region, SynthStyle>();
       
   523                     overridesCache.put(c, map);
       
   524                 } else {
       
   525                     s = map.get(r);
       
   526                 }
   524                 if (s == null) {
   527                 if (s == null) {
   525                     s = new NimbusStyle(prefix, c);
   528                     s = new NimbusStyle(prefix, c);
   526                     overridesCache.put(c, new WeakReference<NimbusStyle>(s));
   529                     map.put(r, s);
   527                 }
   530                 }
   528                 return s;
   531                 return s;
   529             }
   532             }
   530             
   533             
   531             // lazily create the style if necessary
   534             // lazily create the style if necessary