6 Replies Latest reply on Aug 12, 2008 4:58 PM by buabco

    Style Inheritance on default custom style

    buabco Level 1
      I've created a custom class that uses some customs styles I've defined (i.e. underlineColor). I used the sample code to create a default style value with a one time classConstruct method.

      I did notice that the sample code provided by adobe doesn't seem to work when you use a style tag to define only some of the style parameter, I was able to fix this by checking each style parameter in the construct and setting only the ones that where non existing.

      But I then hit another wall, this time trying to use style inheritance, according to documentation, inherited styles are check on the parent first and then checked on the class defined styles, but I've noticed that this doesn't seem to work, in fact if I use the classConstruct method to define the style default values then inherited values are not used.

      I was wondering if anyone knows how to create styles default values with inheritance and how the classConstruct method is really used.

      thanks
        • 1. Re: Style Inheritance on default custom style
          Level 7

          "buabco" <webforumsuser@macromedia.com> wrote in message
          news:g7nvs3$m0i$1@forums.macromedia.com...
          > I've created a custom class that uses some customs styles I've defined
          > (i.e.
          > underlineColor). I used the sample code to create a default style value
          > with a
          > one time classConstruct method.
          >
          > I did notice that the sample code provided by adobe doesn't seem to work
          > when
          > you use a style tag to define only some of the style parameter, I was able
          > to
          > fix this by checking each style parameter in the construct and setting
          > only the
          > ones that where non existing.
          >
          > But I then hit another wall, this time trying to use style inheritance,
          > according to documentation, inherited styles are check on the parent first
          > and
          > then checked on the class defined styles, but I've noticed that this
          > doesn't
          > seem to work, in fact if I use the classConstruct method to define the
          > style
          > default values then inherited values are not used.

          I think that presumes that the child does not set defaults that override the
          parent. For instance, if both parent and child have a backgroundColor
          property, then you cannot address backgroundColor in your defaults if you
          want it to be inherited, you just have to accept whatever gets sent to you
          or doesn't. The whole point of defaults is that this is what gets set in
          the absence of specific input _to this class_.

          HTH;

          Amy


          • 2. Re: Style Inheritance on default custom style
            buabco Level 1
            Amy,

            Thanks for your reply, I think you just hit the point of all my problem, that's exactly what I need to know, how do I manage to set defaults with inheritance so as to the defaults wont affect the inherited values. The hole idea is that if you don't have an inherited value coming from the parent then you get the default, but if there is a inherited value then this value is used.
            • 3. Re: Style Inheritance on default custom style
              Level 7

              "buabco" <webforumsuser@macromedia.com> wrote in message
              news:g7qcok$doa$1@forums.macromedia.com...
              > Amy,
              >
              > Thanks for your reply, I think you just hit the point of all my problem,
              > that's exactly what I need to know, how do I manage to set defaults with
              > inheritance so as to the defaults wont affect the inherited values. The
              > hole
              > idea is that if you don't have an inherited value coming from the parent
              > then
              > you get the default, but if there is a inherited value then this value is
              > used.

              You could look and see what is in getStyle for each property.

              HTH;

              Amy


              • 4. Re: Style Inheritance on default custom style
                buabco Level 1
                OK I did that to fix the problem when you have an incomplete CSS definition, but it doesn't work when you have inheritance, and it's wierd anyway since the code get to run only once that the inheritance get compromised. I've simplified the code to show what I'm doing:
                • 5. Re: Style Inheritance on default custom style
                  Level 7

                  "buabco" <webforumsuser@macromedia.com> wrote in message
                  news:g7s6bp$elb$1@forums.macromedia.com...
                  > OK I did that to fix the problem when you have an incomplete CSS
                  > definition,
                  > but it doesn't work when you have inheritance, and it's wierd anyway since
                  > the
                  > code get to run only once that the inheritance get compromised. I've
                  > simplified
                  > the code to show what I'm doing:
                  >
                  >
                  > Adobe Example is like this:
                  >
                  > [Style(name="customStyle",type="Number",format="length",inherit="no")]
                  > public class testClass extends UIComponent {
                  > // Define a static variable.
                  > private static var classConstructed:Boolean = classConstruct();
                  >
                  > // Define a static method.
                  > private static function classConstruct():Boolean {
                  > if (!StyleManager.getStyleDeclaration("testClass"))
                  > {
                  > // If there is no CSS definition for StyledRectangle,
                  > // then create one and set the default value.
                  > var testStyle:CSSStyleDeclaration = new CSSStyleDeclaration();
                  > testStyle.defaultFactory = function():void
                  > {
                  > this.customStyle = 5;
                  > }
                  > StyleManager.setStyleDeclaration("testClass", testStyle, true);
                  >
                  > }
                  > return true;
                  > }
                  >
                  > // Constructor
                  > public function testClass() {
                  > super();
                  > }
                  >
                  > }
                  >
                  > But this code fails when you create a style declaration for testClass that
                  > doesn't include customStyle, in that case customStyle is not set to
                  > default.
                  >
                  > So I change it to:
                  >
                  > [Style(name="customStyle",type="Number",format="length",inherit="no")]
                  > public class testClass extends UIComponent {
                  > // Define a static variable.
                  > private static var classConstructed:Boolean = classConstruct();
                  >
                  > // Define a static method.
                  > private static function classConstruct():Boolean {
                  > var nsd:CSSStyleDeclaration;
                  > if (!StyleManager.getStyleDeclaration("testClass")) {
                  > // If there is no CSS definition for StyledRectangle,
                  > // then create one and set the default value.
                  > nsd = new CSSStyleDeclaration();
                  > } else {
                  > nsd = StyleManager.getStyleDeclaration("testClass");
                  > }
                  >
                  > //Definamos los estilos personalizados
                  > //Para el titulo
                  >
                  > if (!StyleManager.isValidStyleValue(nsd.getStyle("customStyle")))
                  > nsd.setStyle("customStyle", "5");
                  >
                  > StyleManager.setStyleDeclaration("testClass", nsd, true);
                  >
                  > return true;
                  > }
                  >
                  > // Constructor
                  > public function testClass() {
                  > super();
                  > }
                  >
                  > }
                  >
                  > now the code checks for the parameter before setting it. Still if I set
                  > the
                  > inheritance to true this code doesn't work. The strange part is that It
                  > should
                  > since this code it's supose to be executed only once and it doesn't set
                  > the
                  > class parameter directly but it fills the StyleManagers data instead,
                  > according
                  > to inheritance order the parents that should be used before the class
                  > specific
                  > data, or am I wrong?

                  No, the closer the tag is to being your actual class, the more likely its
                  style is to be used.

                  http://www.adobe.com/livedocs/flex/3/html/help.html?content=styles_04.html

                  You might find this useful

                  http://blog.flexexamples.com/2007/12/24/displaying-a-combobox-controls-inherited-styles-in -flex/


                  • 6. Re: Style Inheritance on default custom style
                    buabco Level 1
                    Amy,

                    Thanks for your help, the link you provided proves what I was suspecting the class selector gets precedence over all other type selectors, witch means that if you use it to set defaults you simple break the inheritance style system. This means also that the example provided by Adobe is totally wrong :( (Hope someone from Adobe could confirm this)

                    Any ways I give up with this and tried something else, I'm not sure what this will do to performance but it did work nicely with inheritance, what I did is to override the getStyle method so it will return the default values in case the super.getStyle method returns undefined.