6 Replies Latest reply on Jan 31, 2010 9:16 AM by looseleaf

    conditional attribute in RichFaces CDK template (custom component)

      Hi everybody!

       

        We're implementing a cusomt RichFaces component with the CDK. The template contains some HTML input elements:

      <input id="..." type="text" ... />

      What's the best way to implement the HTML attribut "disabled" for those elements?

       

      We exposed the disabled attribute via the component configuration and the most meaningful way to disable the component would be to mark each single <input> element with the HTML disabled attriubute.

       

      Now what I am doing now ist this (shortened):

      {code:xml}

          <jsp:scriptlet>
          <![CDATA[
          boolean disabled = getUtils().isBooleanAttribute(component, "disabled");
          if (disabled) {
          ]]>
          </jsp:scriptlet>
            <input id="#{clientId}T" name="#{clientId}T" type="text"
                 value="#{component.attributes['valueT']}"
                 disabled="disabled"/>

       

            <input id="#{clientId}N" name="#{clientId}N" type="text"
                 value="#{component.attributes['valueN']}"
                 disabled="disabled"/>

          <jsp:scriptlet>
          <![CDATA[
          } else {
          ]]>
          </jsp:scriptlet>

            <input id="#{clientId}T" name="#{clientId}T" type="text"
                 value="#{component.attributes['valueT']}"/>

       

            <input id="#{clientId}N" name="#{clientId}N" type="text"
                 value="#{component.attributes['valueN']}"/>

          <jsp:scriptlet>
          <![CDATA[
          }
          ]]>
          </jsp:scriptlet>

      {code}

       

       

      It would be nice, if I could have the markup just once and the disabled attribute only written if the component is disabled.
      Can I somehow define an entity &isDisabled; that is empty or not? I think, that's the only way to do this in valid in XML markup, right?

        • 1. Re: conditional attribute in RichFaces CDK template (custom component)
          nbelaevski

          Hi Stefan,

           

          You can do the following:

           

          1) define method in abstract renderer class:

          public boolean isComponentDisabled(UIComponent component) {
             return getUtils().isBooleanAttribute(component, "disabled");
          }
          

           

          2) create variable in scope:

          <c:object var="disabled" type="boolean" value="#{this:isComponentDisabled(component)}" />
          

           

          3) use this variable in template:

          <input ... disabled="#{disabled}" />
          
          • 2. Re: conditional attribute in RichFaces CDK template (custom component)

            Servus Nick!

             

              I'm afraid, I can't. Try this in your favourite browser:

             

              <input type="text" disabled="false">
              <input type="text" disabled="true">

             

            You'll see that both of them are disabled. I can't recall or find the spot in the HTML specification, but the value of the disabled attribute is irrelevant. If the attribute is there, the control is disabled. So this is not viable.

            Although your posting offers some good hints as to using other methods for other problems

             

            Is there a way to dynamically define an HTML entity?

             

            Edit:

            I guess, I could introduce a method that returns the whole disabled-String, like this

             

            {code:Java}

            public boolean getComponentDisabledString(UIComponent component) {
               if (getUtils().isBooleanAttribute(component, "disabled")) {

                 return "disabled='disabled'";

               } else {

                 return "";

               }

            {code}

             

            Edit #2:

            Nope, also gives an XML parsing error when using

            <input type="text" #{disabledstring} value="..." />

             

            Of course.

             

            Thanks so far,

            Stefan

             

            p.s.: How can I post here with Opera? The AJAX-Editor does not provide any "unsupported browser" or non-JS fallbacks.


            • 3. Re: conditional attribute in RichFaces CDK template (custom component)
              nbelaevski

              Right, I see now that CDK correctly handles boolean properties only in encoding of pass-through attributes.

              Then the following changes should be done:

               

              1) <cdk:object> should be declared with java.lang.String type

              2) method code:

               

              public String getComponentDisabledString(UIComponent component) {   
                 if (getUtils().isBooleanAttribute(component, "disabled")) {      return "disabled";    } else {      return null;    }
              }

               

              Attributes with empty strings/null values are not rendered.

               

              P.S. About Opera: https://jira.jboss.org/jira/browse/ORG-373

              • 4. Re: conditional attribute in RichFaces CDK template (custom component)

                Naah, doesn't work, not een with returning null if the component is not disabled.

                 

                  [cdk:generate]

                  Parse config file ...\src\main\config\component\inputTN[cdk:generate]
                  [Fatal Error] :24:14: Element type "input" must be followed by either attribute specifications, ">" or "/>".

                 

                So the XML parser does not accept the #{disabledstring} text within the <input> element, like I wrote in the second edit for my last posting.

                 

                Any other ideas?

                • 5. Re: conditional attribute in RichFaces CDK template (custom component)
                  nbelaevski

                  Try:

                  <input type="text" disabled="#{disabled}" />
                  

                  with the method code I've posted.

                  • 6. Re: conditional attribute in RichFaces CDK template (custom component)

                    Ah, I missed that point completely.

                     

                    Thanks very much for your help!

                     

                    Stefan