The StyleCollection class lets you create groups of styles that are applied to and automatically updated on subscribing components. It supports both instance and renderer styles (setStyle and setRendererStyle respectively), and uses component introspection to apply only relevant styles to each component. StyleCollection exposes a robust interface, including a static interface that provides global access to named styles.
Here’s a simple demonstration of the class in action (FLA included in the source zip):
Here’s some rough documentation on the class. Note that while the interface may look complex, there are only really 3 methods most people will use besides the constructor: getStyleCollection, setStyle, and addSubscribers.
import com.gskinner.styles.StyleCollection // name - optional name to identify style via the static interface (see addStyleCollection) myStyleCollection = new StyleCollection(name:String=null)
provides global access to named style collections
// Adds a style collection to the global repository, and associates a name with it for subsequent retrieval. StyleCollection.addStyleCollection(name:String, collection:StyleCollection) // Removes a style collection from the global repository StyleCollection.removeStyleCollection(name:String) // Retrieves a style collection from the global repository StyleCollection.getStyleCollection(name:String):StyleCollection
// Sets a style on the collection, and updates all subscribers with the new style. // Note that only subscribing components that use the style (as defined in their getDefaultStyles return) will be updated. setStyle(style:String, value:Object):void // Gets a style value from the collection. getStyle(style:String):Object // Sets a renderer style on the collection, and updates all components with the new style. // Note that all subscribing components that have a setRendererStyle method will be updated. setRendererStyle(style:String, value:Object):void // Gets a renderer style value from the collection. getRendererStyle(style:String):Object // Adds a component as a subscriber to this style collection, and immediately applies the current set of styles to the component. // Subscribers are automatically updated whenever a style is changed in the collection. // Included mostly to provide a type-safe method (vs. addSubscribers). addSubscriber(component:UIComponent):void // Adds a list of components as subscribers to this style collection. addSubscribers(component1, ... componentN):void // Removes a component as a subscriber to this style collection. // Note that this does not clear its styles. removeSubscriber(component:UIComponent):void // Removes a list of components as subscribers to this style collection. removeSubscribers(component1, ... componentN):void // Applies the current set of styles to the component, without adding it as a subscriber. applyStyles(component:UIComponent):void // Applies the specified style from the collection to the component. applyStyle(component:UIComponent, style:String):void // Applies the specified renderer style from the collection to the component. applyRendererStyle(component:UIComponent, style:String):void // Creates a clone of this StyleCollection. clone():StyleCollection
You can download the source code, and a demo file by clicking here.
Hopefully this proves useful for people. It has undergone some testing, but there are likely still some bugs to work out. Please let me know if you have any problems, or find any glitches, in the comments below.