0

While adding the [Style ...] metadata tag to my sub-class did make the property showPromptWhenFocused accessible from MXML, the initializeStyles() function does not successfully change the default value to true.

I want the user to be able to set showPromptWhenFocused to false if they want to, but I want the default value to be true.

package com.santacruzsoftware.crafting.controls
{
import mx.core.FlexGlobals;
import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager;

import spark.components.TextInput;

[Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]

public class WatermarkTextInput extends TextInput
{
    private static function initializeStyles() : void
    {
        var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
        if (!style)
            style = new CSSStyleDeclaration();

        style.defaultFactory = function() : void
        {
            this.showPromptWhenFocused = true;
        }

        FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
    }
    //call the static function immediately after the declaration
    initializeStyles();
}
} 

Any ideas?

ketan
  • 19,129
  • 42
  • 60
  • 98
Richard Haven
  • 1,122
  • 16
  • 31

2 Answers2

0

When you call getStyleDeclaration() pass in the name of your class, so you can get the WaterMarkTextInput style declaration:

var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("WatermarkTextInput");

And then do the same thing when you set the style for your class:

FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("WatermarkTextInput", style, false);
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • 1
    Keep in mind that defaultFactory is the one that is intended to contain the "defaults" set in the MXML when you instantiate the Class through MXML. So this factory may not be able to override styles in all instances. I've had better results overriding factory instead of defaultFactory. A declaration could exist that doesn't contain your style property, so you might want to check for a value on the declaration that you do get. Also, all UIComponents in Flex 4+ have a reference to stylemanager. You don't need to use FlexGlobals (and you shouldn't). – Amy Blankenship Apr 22 '12 at 02:02
  • Amy - initializeStyles() is static, so it does not have access to any styleManager reference in the instance, no? – Richard Haven Apr 23 '12 at 14:44
  • How do I allow the ancestor to set all the other styles? Or will it have already done so by the time the descendant's static function executes? Debugging, I find that if (!style) is true as if no style has been set yet. – Richard Haven Apr 23 '12 at 14:44
  • Sunil - I've changed to your code and I've added if (style == null) style = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("TextInput"); but it still does not override the default. I've also tried explicitly calling the parentStyle.defaultFactory(), etc. – Richard Haven Apr 23 '12 at 15:10
0

Try to override notifyStyleChangeInChildren().

package com.santacruzsoftware.crafting.controls {

    import mx.core.FlexGlobals;
    import mx.styles.CSSStyleDeclaration;
    import mx.styles.StyleManager;

    import spark.components.TextInput;

    [Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]
    public class WatermarkTextInput extends TextInput {

        public var inheritStyles:boolean = true;

        private static function initializeStyles():void {
            var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
            if (!style)
                style = new CSSStyleDeclaration();

            style.defaultFactory = function():void {
                this.showPromptWhenFocused = true;
            }

            FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
        }

        //call the static function immediately after the declaration
        initializeStyles();

        public override function notifyStyleChangeInChildren(styleProp:String, recursive:Boolean):void {
            if (!inheritStyles) {
                switch(styleProp) {
                    case 'showPromptWhenFocused':
                        return;
                }
            }
            super.notifyStyleChangeInChildren(styleProp, recursive);
        }

}
Feanix
  • 1
  • 1