Requirements
     
Prerequisite knowledge
Required products Sample files User level
Flash Builder 4.7 Premium (Download trial)

ex5_06_starter.zip

ex5_06_solution.zip

Beginning
In this exercise you will create a custom skin property and reuse it to style a Panel container skin with differently colored highlights (see Figure 1).
 
This is the finished application.

Figure 1. Review your task for this exercise.

In this exercise, you will learn how to:
 

 
Create a custom skin property

In this section, you will change the color of the title bar for each of the Panel containers within the Employee Portal application.
 
  1. Download the ex5_06_starter.zip file if you haven't done so already and extract the ex5_06_starter.fxp file to your computer.
  2. Open Flash Builder.
  3. Import the ex5_06_starter.fxp file.
  4. Open the ex5_06_starter.mxml file.
  5. Locate the four panel instances in the components namespace and comment them out.
  6. Save the file.
  7. In the Package Explorer, right-click the components package and select New > MXML Component.
  8. Name the MXML component EmployeePortalPanel.
  9. Click the Browse button next to the Based on field.
  10. In the Based on dialog box, type Panel and select the Spark Panel container (see Figure 2).
Base the new MXML component on the Spark Panel container.

Figure 2. Base the new MXML component on the Spark Panel container.

  1. Click OK.
  2. In the New MXML Component dialog box, type 250 for the Width value and 475 for the Height value (see Figure 3).
Name the component EmployeePortalPanel, base it on the Spark Panel container, and replace the Width and Height values.

Figure 3. Name the component EmployeePortalPanel, base it on the Spark Panel container, and replace the Width and Height values.

  1. Click Finish.
    The EmployePortalPanel.mxml file will open in the Flash Builder Editor view.
     
  2. Delete the Declaration tag block and create a Script block.
  3. Within the Script block, type panelTitleColor and press CTRL+1 to invoke the quick assist tool. Select the Create instance variable option. This will create a private variable named panelTitleColor data typed to the Object class.
  4. Change the variable's access modifier to public and modify the data type to the uint class.
<fx:Script> <![CDATA[ public var panelTitleColor:uint; ]]> </fx:Script>
  1. Save the file.
  2. Open the ex5_06_starter.mxml file.
  3. After the four Panel instances that you commented out, create an EmployeePortalPanel component tag block.
  4. To the EmployeePortalPanel component opening tag, assign the title property value to EMPLOYEE OF THE MONTH.
  5. Use the content assist tool to add the panelTitleColor property to the EmployeePortalPanel tag and give it a value of #64BC48 (green).
  6. Within the EmployeePortalPanel component tag set, add an instance of the EmployeeOfTheMonth component.
<components:EmployeePortalPanel title="EMPLOYEE OF THE MONTH" panelTitleColor="#64BC48"> <components:EmployeeOfTheMonth/> </components:EmployeePortalPanel>
  1. Save the file and run the application.
    You will see only one panel that is not properly positioned. You will fix that later. However, note that there are two panel title areas for the Employee of the Month panel (see Figure 4). This occurs because each component is based on the Panel container.
     
There are two panels for the Employee of the Month panel.

Figure 4. There are two panels for the Employee of the Month panel.

  1. Return to Flash Builder and open the EmployeeOfTheMonth.mxml component.
  2. Change the component's Panel container to a Group container.
  3. In the opening Group container tag, reassign the width and height properties to a value of 100%.
<s:Group xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s="library://ns.adobe.com/flex/spark" width="100%" height="100%">
  1. Save the file.
  2. Repeat steps 23-26 for the Cafeteria.mxml and MonthlyEvents.mxml components.
  3. Return to the ex5_06_starter file.
  4. From the commented EmployeeOfTheMonth component instance, copy the x, y, and skinClass properties and their values and paste them into the opening EmployeePortalPanel component instance.
  5. To the opening EmployeePortalPanel component instance, add the height property with a value of 295.
<components:EmployeePortalPanel title="EMPLOYEE OF THE MONTH" panelTitleColor="#64BC48" x="0" y="95" skinClass="skins.PanelContainerSkin" height="295">
  1. Save the file and run the application.
    You should see that only the Employee of the Month panel is displayed and the color block of the panel's title bar has not changed (see Figure 5).
     
Only the Employee of the Month panel is displayed.

Figure 5. Only the Employee of the Month panel is displayed.

 
Pass a value to a custom skin property

In this section you will create a function to handle the passing of the panelTitleColor property value from the main application file to the custom component.
 
  1. Return to Flash Builder and open the PanelContainerSkin.mxml file from the skins package.
  2. Locate the nested fill property within the Rect tag block that follows the PanelSkin instance.
  3. From the nested SolidColor tag, remove the color property value:
<s:fill> <s:SolidColor/> </s:fill>
  1. To the SolidColor tag, assign the id property to headerSkinColor:
<s:fill> <s:SolidColor id="headerSkinColor"/> </s:fill>
  1. Locate the MetaData tags.
  2. Within the HostComponent() function, change the value to reference the EmployeePortalPanel component:
<fx:Metadata> [HostComponent("components.EmployeePortalPanel")] </fx:Metadata>
  1. Locate the Script comment and add a Script block below it:
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <fx:Script> <![CDATA[ ]]> </fx:Script> ...
  1. Within the Script block, create a private function named initTitleSkin that returns a void data type:
<fx:Script> <![CDATA[ private function initTitleSkin():void { } ]]> </fx:Script>
  1. Within the initTitleSkin() function, assign the color property of the headerSkinColor SolidColor fill to the hostComponent.panelTitleColor property value:
private function initTitleSkin():void { headerSkinColor.color = hostComponent.panelTitleColor; }
  1. To the opening SparkSkin tag, assign the creationComplete property value to call the initTitleSkin() function:
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="initTitleSkin()">
  1. Save the file and run the application.
    You should see that the color block in the Employee of the Month panel is now green (see Figure 6).
     
Change the Employee of the Month Panel container's header color block to green.

Figure 6. Change the Employee of the Month Panel container's header color block to green.

  1. Return to the ex5_06_starter.mxml file in Flash Builder.
  2. Copy the EmployeePortalPanel component and paste two instances below the first.
<components:EmployeePortalPanel title="EMPLOYEE OF THE MONTH" panelTitleColor="#64BC48" x="0" y="95" skinClass="skins.PanelContainerSkin" height="295"> <components:EmployeeOfTheMonth/> </components:EmployeePortalPanel> <components:EmployeePortalPanel title="EMPLOYEE OF THE MONTH" panelTitleColor="#64BC48" x="0" y="95" skinClass="skins.PanelContainerSkin" height="295"> <components:EmployeeOfTheMonth/> </components:EmployeePortalPanel> <components:EmployeePortalPanel title="EMPLOYEE OF THE MONTH" panelTitleColor="#64BC48" x="0" y="95" skinClass="skins.PanelContainerSkin" height="295"> <components:EmployeeOfTheMonth/> </components:EmployeePortalPanel>
  1. To the second instance of the EmployeePortalPanel component, reassign the title property to CAFETERIA SPECIAL, the panelTitleColor property value to #F05123 (orange), and the x property value to 275.
  2. Remove the height property and and change the nested component to the Cafeteria component.
<components:EmployeePortalPanel title="CAFETERIA SPECIAL" panelTitleColor="#F05123" x="275" y="95" skinClass="skins.PanelContainerSkin"> <components:Cafeteria/> </components:EmployeePortalPanel>
  1. To the third instance of the EmployeePortalPanel component, reassign the title property to MONTHLY EVENTS, the panelTitleColor property value to #0D86B8 (blue), and the x property value to 550.
  2. Remove the height property and change the nested component to the MonthlyEvents component.
<components:EmployeePortalPanel title="MONTHLY EVENTS" panelTitleColor="#0D86B8" x="550" y="95" skinClass="skins.PanelContainerSkin"> <components:MonthlyEvents/> </components:EmployeePortalPanel>
  1. Locate the commented EmployeeDirectory component instance.
  2. Copy the EmployeeDirectory component instance and paste it below the third instance of the EmployeePortalPanel component.
    Since the EmployeeDirectory component does not use the PanelContainerSkin, you do not need to nest it within an EmployeePortalPanel component instance.
     
  3. Save the file and run the application.
    You should see the Employee of the Month, Cafeteria Special, and MonthlyEvents panels have different colored headers (see Figure 7). Also note that the text in the Monthly Events panel is blue.
     
Pass the EmployeePortalPanel component the header color from the main application.

Figure 7. Pass the EmployeePortalPanel component the header color from the main application.

  1. Return to Flash Builder and open the ex5_06_starter.css file.
  2. Locate the MonthlyEvents selector and comment out the color property.
  3. Save the file and run the application.
You should see that the text within the Monthly Events panel is now black (see Figure 8).
 
The text in the Monthly Events panel is black.

Figure 8. The text in the Monthly Events panel is black.

In this exercise you created a custom skin property and reused it to style a Panel container skin with different colored highlights. In the next exercise you will learn how to use a skin to make the Panel container title bar vertical.