ActionScript 3.0 Design Patterns

(Chris Devlin) #1

198 | Chapter 5: Adapter Pattern


requested location inaList(list inde xstarts at 1). This method checks to see if the


delete location is within array bounds, and returns a Boolean value accordingly. Note


the use of the qualifiersuperto set the text in theupdate( )method (line 47). This


allows the method to access the superclass property,listText, directly. This is neces-


sary as the setter method for the text property is overridden (lines 50–53). In fact,


several public methods in theTextFieldclass that allow direct setting of field con-


tents are overridden to prevent clients from breakingIListDisplayfunctionality. The


overridden methods are: setter methods to thetextandhtmlTextproperties includ-


ingappendText( ),replaceSelectedText( ), andreplaceText( ).


The Client


Clients would use theListDisplayFieldadapter class just as they would aTextField


class. This is the big advantage to implementing class adapters in ActionScript 3.0, as


the public methods and properties of the existing class behave as expected (unless


overridden). The following client,Main.asshown in Example 5-15, uses aTextFormat


object to set the font and font size of the display object. In addition, theborderprop-


erty of the text field is set to show the field boundary.


Example 5-15. Main.as (document class)


package
{
import flash.display.MovieClip;
import flash.text.*;


public class Main extends MovieClip
{
public function Main( )
{
// create ListDisplayField
var shoppingListField:ListDisplayField = new ListDisplayField( );


// develop field formatting
var format:TextFormat = new TextFormat( );
format.size = 18;
format.font = "Arial";


// set field location and format
shoppingListField.x = 20;
shoppingListField.y = 20;
shoppingListField.border = true;
shoppingListField.defaultTextFormat = format;


// create list
shoppingListField.addItem("Bread");
shoppingListField.addItem("Butter");
shoppingListField.addItem("Broccoli");
shoppingListField.addItem("Cheese");
// changed mind about Broccoli

Free download pdf