4 Replies Latest reply on Nov 7, 2011 2:34 PM by Flex harUI

    setting control parameters programmatically - using variables...

    LesBerg

      Please excue typos - I lost my glasses the other day and I'm waiting on a new pair to come in.

       

      I need to be able to change the 'editable' and 'enabled' properties on a variety of controls programmatically. I'm adding 'after the fact' Role support to an application and the customer has asked for a way to change the visibility and editability on specific controls.

       

      I've created a role management system where the controls are stored in an XML file and the application administrator has the ability to define roles by changing what a user (belonging to a Role) can see and change.

       

      based on:

      <filename canAccess="false" fileName="canSearch.mxml" friendlyName="Claim Search">
        <controls>
          <control controlID="control ID" controlLabel="control ID" controlType="type" editable="false" visible="false"/>
          <control controlID="control ID" controlLabel="control ID" controlType="type" editable="false" visible="false"/>
        </controls>
      </filename>

       

      I could:

       

      if(controlType == whatever)

         {

           display based on xml information

         }

       

      but I'd have to add new code for each additional control type that we may add later..

       

      what I'd like to do is:

       

      var controlID:controlType;

      controlID.editable = controls.control.@editable;

       

      but of course,

       

      "Type was not found or type was not a compile-time constant: controlType"

       

      Is there a way to do this entirely programmatically?

        • 1. Re: setting control parameters programmatically - using variables...
          LesBerg Level 1

          testing a solution:

           

           

          var tempControlID:String = control.@controlID;

          this[tempControlID].enabled = control.@enabled;

           

           

           

          • 2. Re: setting control parameters programmatically - using variables...
            Flex harUI Adobe Employee

            I’m not sure I understood the question.  Typically, if you have MXML like:

             

                <s:Button id=”foo” />
                <s:Button id=”bar”></s:Button>
            

             

            And XML like

             

            Var xml:XML = <controls>
                <control controlID=”foo” controlLabel=”Foo” enabled=”false”></control>
                <control controlID=”bar” controlLabel=”Bar” enabled=”true”></control>
            </controls>
            

             

            A loop might look like this:

             

                var xmllist:XMLList = xml.control;
                var n:int = xmllist.length();
                for (var i:int = 0; I < n; i++)
                 {
                   var controlData:XML = xmllist[i];
                    var control:UIComponent = document[controlData.@controlD];
                    control.label =controlData.@controlLabel;
                    control.enabled = controlData.@enabled == “true”;
                 }
            

             

            Note the special conversion for boolean values.

            • 3. Re: setting control parameters programmatically - using variables...
              LesBerg Level 1

              This is the code I'm testing:

               

              for each(var control:XML in roleInfo.role.(name == rolesA[0]).filename.(@fileName == calledFileName).controls.control)

                  {

                   var tempControlID:String = control.@controlID;

                   this[tempControlID].enabled = control.@enabled;

                   this[tempControlID].editable = control.@visible;

                  }

               

              against the following XML structure:

              <roles>

                ...

              <role>

                  <name>Claims Viewer</name>

                  <description>This User can only view claims.</description>

                  <filename canAccess="false" fileName="canSearch.mxml" friendlyName="Claim Search">

                    <controls>       

                      <control controlID="txtTIN" controlLabel="TIN:" controlType="Text Input" editable="true" visible="true"/>

                      <control controlID="txtNPI" controlLabel="NPI:" controlType="Text Input" editable="true" visible="true"/>

                    </controls>

                  </filename> 

                  <filename canAccess="true" fileName="canNewClaim.mxml" friendlyName="New Claim">

                    <controls>

                       <control controlID="txtPatientID" controlLabel="Patient ID:" controlType="Text Input" editable="true" visible="true"/>

                       <control controlID="txtSubscribe" controlLabel="Subscriber ID:" controlType="Text Input" editable="true" visible="true"/>

                       <control controlID="txiDOB" controlLabel="Patient DOB:" controlType="Text Input" editable="true" visible="true"/>

                    </controls>

                  </filename>

                </role>

              ...

              </roles>

               

              The idea is to be able to change editable, enabled, and other properties such as 'includeInLayout' based on the role restrictions contained in the XML file.

               

              I am hoping to be able to do this without having to resort to:

               

              if(controlType == mxLabel)

              {

                apply settings

              }

              else ig(controlType == someOtherControl)

              {

              apply settings

              }

              (repeat until all control types are covered)

               

               

              The problem is that not all controls use all, or even the same, properties e.g; <mx:Label> doesn't support 'editble' so evein this method may not be optimal.  I was thinking to use a try-catch block with it to contain errors, but that sounds a little too 'brute-force'.

               

              for each(var control:XML in roleInfo.role.(name == rolesA[0]).filename.(@fileName == calledFileName).controls.control)

                  {

                   var tempControlID:String = control.@controlID;

                   this[tempControlID].enabled = control.@enabled;

                   try

                   {

                     this[tempControlID].editable = control.@visible;

                   }

                   catch(e:ErrorMessage)

                   {

                        //handle 'editable' differently

                   }

                  }

               

              Where rolesA[0] is the Claims role from the User database that we want to match to the applicable role in the XML file, and calledFileName is the name of the file we want to manage the controls for.

               

              I am using an XSD to support the XML file, so I may change the XML structure to have appropriate controls for each control type, however that would make the page that supports modifying the XML very difficult to code (for me , at least)

               

              On the boolean conversions, could I do this?

               

              this[tempControlID].enabled = control.@enabled as Boolean;

               

              Does this make any sense, or am I overcomplicating things?

              • 4. Re: setting control parameters programmatically - using variables...
                Flex harUI Adobe Employee

                You have to decide on a strategy for dealing with properties that don’t match up.  You can simply catch errors, or have a map, or an interface, or subclass, or even something else.  It is up to you.  I don’t really think there is a recommended practice for that.

                 

                Regarding boolean, the special code I recommended was recommended for a reason.  It is the simplest form that works.  Your other attempts will not work.

                1 person found this helpful