ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: List Display Adapter | 197

TheListDisplayField adapter extends theTextField class, and implements the


IListDisplayinterface. The constructor calls thesuper( )method to invoke the con-


structor in the superclassTextField(line 13). The implementation is straightforward


in that the list of items to display is stored in an array calledaListthat is a property


of the class. TheaddItem(s:String)method appends the passed string value to the


end of theaListarray. ThedeleteItemAt(i:uint)method deletes the item at the


34 public function clear( ):void
35 {
36 aList = [];
37 this.update( );
38 }
39
40 internal function update( )
41 {
42 var listText:String = "";
43 // split the array to create a string separated by returns
44 for (var i:Number = 0; i < aList.length; i++) {
45 listText += aList[i] + "\r";
46 }
47 super.text = listText;
48 }
49
50 override public function set text(s:String):void
51 {
52 throw new Error("Cannot directly set text property - use
addItem( ) method");
53 }
54
55 override public function set htmlText(s:String):void
56 {
57 throw new Error("Cannot directly set htmlText property");
58 }
59
60 override public function appendText(s:String):void
61 {
62 throw new Error("Cannot append text - use addItem( ) method");
63 }
64
65 override public function replaceSelectedText(s:String):void
66 {
67 throw new Error("Cannot replace selected text");
68 }
69
70 override public function replaceText(beginIndex:int, endIndex:int,
newText:String):void
71 {
72 throw new Error("Cannot replace text");
73 }
74 }
75 }

Example 5-14. ListDisplayField.as

Free download pdf