You are on page 1of 4

/*

Feathers
Copyright 2012-2016 Bowler Hat LLC. All Rights Reserved.

This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.skins
{
import flash.utils.Dictionary;

/**
* Used by themes to create and manage style providers for component classes.
*/
public class StyleProviderRegistry
{
/**
* @private
*/
protected static const GLOBAL_STYLE_PROVIDER_PROPERTY_NAME:String =
"globalStyleProvider";

/**
* @private
*/
protected static function defaultStyleProviderFactory():IStyleProvider
{
return new StyleNameFunctionStyleProvider();
}

/**
* Constructor.
*
* <p>If style providers are to be registered globally, they will be
* passed to the static <code>globalStyleProvider</code> property of
the
* specified class. If the class does not define a
* <code>globalStyleProvider</code> property, an error will be
thrown.</p>
*
* <p>The style provider factory function is expected to have the
following
* signature:</p>
* <pre>function():IStyleProvider</pre>
*
* @param registerGlobally Determines if the registry
sets the static <code>globalStyleProvider</code> property.
* @param styleProviderFactory An optional function that
creates a new style provider. If <code>null</code>, a
<code>StyleNameFunctionStyleProvider</code> will be created.
*/
public function StyleProviderRegistry(registerGlobally:Boolean = true,
styleProviderFactory:Function = null)
{
this._registerGlobally = registerGlobally;
if(styleProviderFactory === null)
{
this._styleProviderFactory = defaultStyleProviderFactory;
}

} } /** * @private */ protected var _registerGlobally:Boolean. } this. } /** * Returns all classes that have been registered with a style provider. but only if they still match the //ones that the theme created. /** * Disposes the theme. a developer could replace the global //style providers with different ones. */ public function hasStyleProvider(forClass:Class):Boolean { if(this. * * @param forClass The class that may have a style provider._classToStyleProvider. } return forClass in this._classToStyleProvider === null) { return false. /** * @private */ protected var _styleProviderFactory:Function. this._styleProviderFactory = styleProviderFactory. */ public function dispose():void { //clear the global style providers. */ ._classToStyleProvider = null. else { this. for(var untypedType:Object in this.clearStyleProvider(type). } /** * Determines if an <code>IStyleProvider</code> for the specified * component class has been created._classToStyleProvider) { var type:Class = Class(untypedType). /** * @private */ protected var _classToStyleProvider:Dictionary = new Dictionary(true).

If the * registry is global. a newly created style * provider will be passed to the static <code>globalStyleProvider</code> * property of the specified class._styleProviderFactory(). or if it was already created. } } return styleProvider. returns the existing registered * style provider. index++. If the registry is global. } /** * Creates an <code>IStyleProvider</code> for the specified component * class. and the static <code>globalStyleProvider</code> * property contains the same value. if(this. } else { result = new <Class>[]._registerGlobally) { forClass[GLOBAL_STYLE_PROVIDER_PROPERTY_NAME] = styleProvider._classToStyleProvider[forClass] = styleProvider. public function getRegisteredClasses(result:Vector. * If it contains a different value. for(var forClass:Object in this. */ public function getStyleProvider(forClass:Class):IStyleProvider { this. } return result. * .length = 0.<Class> = null):Vector. * * @param forClass The style provider is registered for this class. * @param styleProviderFactory A factory used to create the style provider. this._classToStyleProvider[forClass])._classToStyleProvider) { result[index] = forClass as Class. } var index:int = 0.<Class> { if(result !== null) { result.validateComponentClass(forClass). var styleProvider:IStyleProvider = IStyleProvider(this. } /** * Removes the style provider for the specified component class. then it will be left unchanged to * avoid conflicts with other registries or code. if(!styleProvider) { styleProvider = this. it will be set to <code>null</code>.

if(this. so we check if it's equal //before setting to null._classToStyleProvider[forClass]). */ public function clearStyleProvider(forClass:Class):IStyleProvider { this. } throw ArgumentError("Class " + type + " must have a " + GLOBAL_STYLE_PROVIDER_PROPERTY_NAME + " static property to support themes. forClass[GLOBAL_STYLE_PROVIDER_PROPERTY_NAME] = null. delete this. * @param forClass The style provider is registered for this class._registerGlobally && forClass[GLOBAL_STYLE_PROVIDER_PROPERTY_NAME] === styleProvider) { //something else may have changed the global style provider //after this registry set it._classToStyleProvider[forClass].hasOwnProperty(GLOBAL_STYLE_PROVIDER_PROPERTY_NAME)) { return. } return null._registerGlobally || Object(type)._classToStyleProvider) { var styleProvider:IStyleProvider = IStyleProvider(this.validateComponentClass(forClass). if(forClass in this. } return styleProvider."). } } } . } /** * @private */ protected function validateComponentClass(type:Class):void { if(!this.