3 August 2009
Note: The ZIP files are Flex Builder project archives. The starter archive is the completed Part 1 project archive. The solution archive contains the completed code and assets associated with this tutorial. The latter is provided for you to compare against your own completed code.
Welcome to Part 2 of this tutorial series. In this part, you will learn how to use containers to lay out visual elements and style them using the component styles and CSS. You also learn how to embed fonts and an image. This will be the visual foundation of the application.
Note: Parts 1 through 6 focus on teaching Flex development to SAP developers. Part 7 teaches Flex developers how to create a Web Dynpro application. Parts 8 and 9 show both SAP and Flex developers how to integrate the Flex application as a Rich Island into the Web Dynpro application.
In Part 1 you watched a video that explains the functionality of the final Web Dynpro application that you build in this tutorial series. In this part, you will lay out all of the elements of the Wagon Assembly Line application (see Figure 1).
Note: In Parts 8 and 9, when you learn how to integrate the Rich Island into the Web Dynpro application, the Flex DataGrid component pictured at the top of Figure 1 will be replaced with the Web Dynpro table UI element.
Figure 2 outlines the Flex framework controls and containers that you will use in this application. Note that containers (such as Application, VBox, or Canvas) hold other containers or controls and are used to lay out visual elements in an application.
You will learn how to style these controls and containers, which are collectively called components. Specifically, you will find that the Canvas container is useful because it not only acts as a container for layout, but can also be styled for use as a visual element, such as, for example, the blue clock or green inventory bins.
Note: If you wish to continue using the ShopFloorBins project from Part 1, skip the following steps. Otherwise, use the following steps to import the starting project archive for Part 2.
Note: In the dialog box, you may select to delete the project contents or keep them. If you keep the contents the new project will overwrite the contents.
You will create the visual layout of the application using container controls.
layout property in the opening Application tag.layout property to vertical if it is not.A value of vertical means all the visual components you add between the Application container tags will display vertically.
<mx:Application ...>
<mx:DataGrid />
</mx:Application>
<mx:DataGrid />
<mx:Canvas>
</mx:Canvas>
id property with a value of assemblyLineBackground, a width property with a value of 770 and a height property with a value of 500.Note: A Flex framework container lays out other components, which could be other containers or controls. The Canvas container, which is just one of many Flex containers, lays out its children using absolute positioning. Absolute positioning specifies x and y layout properties relative to the upper left corner of the container, which is the 0, 0 position. The id property is the instance name for the component.
<mx:Canvas id="assemblyLineBackground"
width="770" height="500"
backgroundColor="#F3F3F3">
</mx:Canvas>
You should only see the empty DataGrid control in the application. Canvas containers are not visible by default.
backgroundColor with a gray color value (#F3F3F3) to the opening Canvas tag.Your application should appear as shown in Figure 3.
layout property value to horizontal.You will see the DataGrid control and the Canvas container aligned horizontally.
layout property value to absolute.Like a Canvas container, the Application container can be set to use absolute positioning. Because no x and y properties are specified for either the Canvas or DataGrid components, they default to the 0, 0 position of the container, which is, in this case, the Application container. When two components overlap in absolute positioning, the component that is further down in the code will be positioned above. Since the Canvas container is larger than the DataGrid control, you only see the Canvas container.
layout property value back to vertical.In this section, you create the clock layout that will show the time until the assembly line stops. You will use two Canvas containers in this display element because later in this tutorial you will apply styles to them for background color and rounded corners. Figure 4 shows the Flex components that will be used for the clock display.
assemblyLineBackground Canvas container tags, create another Canvas container tag set with the following properties and values:<mx:Canvas id="assemblyLineBackground" ...>
<mx:Canvas id="clockBase"
width="197" height="90"
y="32" x="350"
horizontalCenter="0"
backgroundColor="#0066FF">
</mx:Canvas>
</mx:Canvas>
Note: The horizontalCenter property aligns the clockBase container to the horizontal center of its parent container, the assemblyLineBackground container.
clockBase Canvas tags, create a Label control and add the following properties and values to the Label control:<mx:Label id="clockTitle"
text="TIME UNTIL SHUTDOWN"
width="148"
y="10"
horizontalCenter="0"
textAlign="center"/>
Note: The textAlign property will center the text within the Label control.
<mx:Label id="clockTitle" .../>
<mx:Canvas id="timeLeftBackground"
y="35"
right="10" left="10"
backgroundColor="#000000">
</mx:Canvas>
<mx:Canvas id="timeLeftBackground"...>
<mx:Label id="timeLeft"
text="00" textAlign="center"
verticalCenter="0"
horizontalCenter="0"/>
</mx:Canvas>
Note: The verticalCenter property aligns the Label control to the vertical center of the parent Canvas container.
Your application should appear as shown in Figure 5.
You can see that the timeLeft Label control text is not visible against the background of the timeLeftBackground Canvas container. This will change once you apply the styles later in this tutorial.
In this section, you create the colored bins that will display available inventory for each wagon part. For each bin you will use a VBox container that encompasses a Label control and a Canvas container. The Canvas container, in turn, holds a TextInput control and is the element that has the green background color. Figure 6 shows the Flex components that will be used for the bin display.
assemblyLineBackground Canvas container tags, create a VBox container tag set and add a x property with a value of 47 and a y property with a value of 178.<mx:VBox x="47" y="178"
backgroundColor="#0066FF">
</mx:VBox>
id property set to binTitle0. Flex is a zero-indexed programming language, so you will start numbering at zero. This is merely a convention and is not required.text property with a value of Bin, a textAlign property with a value of center and a width property with a value of 100.Note: The text value is what will be displayed to the user.
<mx:VBox ...>
<mx:Label id="binTitle0"
text="Bin"
textAlign="center"
width="100"/>
</mx:VBox>
<mx:Label id="binTitle0" . . ./>
<mx:Canvas
id="binCanvas0"
width="100" height="100">
</mx:Canvas>
<mx:Canvas id="binCanvas0" ...>
<mx:TextInput id="binData0"
text="amount"
editable="false"
width="60" height="30"
horizontalCenter="0"
verticalCenter="0"
textAlign="center"/>
</mx:Canvas>
Your application should appear as shown in Figure 7.
In this section, you will create the other four bins needed for the wagon inventory.
assemblyLineBackground Canvas tags.x property to 186.id property of the new Label control and change its value to binTitle1.id property of the new Canvas container and change its value to binCanvas1.id property of the new TextInput control and change its value to binData1. <mx:VBox x="186" y="178"
backgroundColor="#0066FF">
<mx:Label id="binTitle1"
text="Bin"
textAlign="center"
width="100"/>
<mx:Canvas id="binCanvas1"
width="100"
height="100">
<mx:TextInput id="binData1"
text="amount"
editable="false"
width="60"
height="30"
horizontalCenter="0"
verticalCenter="0"
textAlign="center"/>
</mx:Canvas>
</mx:VBox>
x property value to 331 and increment the id property values to binTitle2, binCanvas2 and binData2.x property value to 471 and change the id property values to binTitle3, binCanvas3 and binData3.x property value to 614 and change the id property values to binTitle4, binCanvas4 and binData4.Your application should appear as shown in Figure 8.
In this section you will use the component styles to change the display of the element.
backgroundGradientColors property with a value of [0xFFFFFF, 0xFFFFFF]. By default, Flex applications have a gray gradient background. With this code you are setting the application background color to a solid white background.Your application should appear as shown in Figure 9. The main application background is white but the Canvas container for the assembly line is gray.
Note: The timeLeft Label control text is not visible since the text color is the same as the background color.
assemblyLineBackground Canvas container and add the following styles and values to the Canvas tag:<mx:Canvas id="assemblyLineBackground"
width="770"
height="500"
backgroundColor="#F3F3F3"
borderStyle="solid"
borderThickness="1"
cornerRadius="10">
Your application should appear as shown in Figure 10.
The next step corrects the text background in the clock.
clockTitle Label and add the fontSize style with a value of 12, the fontWeight style with a value of bold and color style with a value of #FFFFFF.<mx:Label id="clockTitle"
text="TIME UNTIL SHUTDOWN"
width="148"
y="10"
horizontalCenter="0"
textAlign="center"
color="#FFFFFF"
fontSize="12"
fontWeight="bold"/>
<mx:Canvas id="timeLeftBackground"
y="35"
right="10"
left="10"
backgroundColor="#000000"
cornerRadius="6"
backgroundAlpha=".6"
borderStyle="solid"
borderThickness="1">
<mx:Label id="timeLeft"
text="00"
textAlign="center"
verticalCenter="0"
horizontalCenter="0"
color="#FFFF00" />
binData0 TextInput control and add the fontSize style with a value of 18.fontSize style to binData1, binData2, binData3 and binData4.Your application should appear as shown in Figure 11.
In this section, you create a cascading style sheet (CSS) file and apply it to style the application further. You also use embedded fonts.
The file will be opened in Flex Builder Source mode.
.binStyle using the curly brace notation.Note: In a CSS file, you can use either the camel case or dash format for CSS attribute names.
.binStyle{
background-color: #339933;
corner-radius: 6;
border-style: solid;
border-color:#000000;
border-thickness: 2;
drop-shadow-color: #000000;
drop-shadow-enabled: true;
}
.clockStyle {
background-color: #44A7D4;
drop-shadow-enabled: true;
drop-shadow-color: #000000;
corner-radius: 6;
border-color: #000000;
border-style: solid;
border-thickness: 1;
}
Style tag after the beginning Application tag.source property with a value of assets/ShopFloor.css.<mx:Style source="assets/shopFloor.css"/>
backgroundColor property. (You'll have to delete five isntances of this property).binCanvas0 Canvas and add a styleName property with the value of binStyle.<mx:Canvas id="binCanvas0"
width="100" height="100"
styleName="binStyle">
styleName property and value to binCanvas1, binCanvas2, binCanvas3 and binCanvas4.clockBase Canvas container and delete the backgroundColor property.styleName property with a value of .clockStyle.<mx:Canvas id="clockBase"
width="197" height="90"
y="32" x="350"
horizontalCenter="0"
styleName="clockStyle">
Your application should appear as shown in Figure 12.
In this section, you will embed three fonts. Fonts are normally embedded when there is a good chance the font will not be on the end user's computer. Fonts are copyrighted so you will need to purchase a license in order to use most fonts.
.clockStyle selector, embed a font using the @font-face syntax and curly braces.src with a value of local("Arial"), the fontFamily attribute with a value of fontArialBold and the font-weight attribute with a value of bold.@font-face {
src: local("Arial");
fontFamily:"fontArialBold";
font-weight: bold;
}
@font-face syntax.src attribute with a value of local("Arial")and the fontFamily attribute with a value of fontArialRegular.@font-face {
src: local("Arial");
fontFamily:"fontArialRegular";
}
@font-face syntax.src attribute with a value of local("Times New Roman")and the fontFamily attribute with a value of fontTimes.@font-face {
src: local("Times New Roman");
fontFamily: "fontTimes";
}
.binInputFont using curly brace syntax..binInputFont {
fontFamily: fontArialBold;
fontSize: 24pt;
color: #000000;
font-weight:bold;
}
.titleFont {
fontFamily: fontArialBold;
fontSize: 16pt;
color: #000000;
font-weight:bold;
}
.clockFont using curly brace syntax..clockFont {
fontFamily: fontTimes;
fontSize: 30pt;
}
binTitle0 Label control.styleName property with a value of titleFont.styleName property and value to binTitle1, binTitle2, binTitle3 and binTitle4.binData0 TextInput control.styleName property with a value of binInputFont.styleName property and value to binData1, binData2, binData3 and binData4.clockTitle Label control.styleName property with the value titleFont.timeLeft Label control and add the styleName property with a value of clockFont.Your application should appear as shown in Figure 13.
In this section, you use the @Embed syntax within the Image control to embed an image. By embedding, the image is compiled into the SWF file and allows faster access than if it is loaded at runtime.
assemblyLineBackground Canvas container tag, add an Image control with the following properties and values:<mx:Image x="31" y="310"
source="@Embed
(source='assets/belt.swf')"
width="700"/>
<mx:Label text="Wagon Assembly Line"
bottom="32"
horizontalCenter="0"
styleName="binInputFont"/>
Your application should appear as shown in Figure 14.
In this tutorial you created the layout using the Application, VBox and Canvas containers and some additional controls. You styled the application with a combination of component styles and CSS. You used embedded fonts and an image to start the visual foundation of the application. If you are interested in learning more about these topics, visit the following links:
In Part 3, SAP developers will create data for this Flex application and bind it to the controls.
For your reference, here are all the parts in this series: