1//////////////////////////////////////////////////////////////////////////////// 2// 3// ADOBE SYSTEMS INCORPORATED 4// Copyright 2005-2007 Adobe Systems Incorporated 5// All Rights Reserved. 6// 7// NOTICE: Adobe permits you to use, modify, and distribute this file 8// in accordance with the terms of the license agreement accompanying it. 9// 10//////////////////////////////////////////////////////////////////////////////// 11 12package mx.collections 13{ 14 15import flash.utils.IDataInput; 16import flash.utils.IDataOutput; 17import flash.utils.IExternalizable; 18import mx.core.mx_internal; 19 20use namespace mx_internal; 21 22[DefaultProperty("source")] 23 24[RemoteClass(alias="flex.messaging.io.ArrayCollection")] 25 26/** 27 * The ArrayCollection class is a wrapper class that exposes an Array as 28 * a collection that can be accessed and manipulated using the methods 29 * and properties of the <code>ICollectionView</code> or <code>IList</code> 30 * interfaces. Operations on a ArrayCollection instance modify the data source; 31 * for example, if you use the <code>removeItemAt()</code> method on an 32 * ArrayCollection, you remove the item from the underlying Array. 33 * 34 * @mxml 35 * 36 * <p>The <code><mx:ArrayCollection></code> tag inherits all the attributes of its 37 * superclass, and adds the following attributes:</p> 38 * 39 * <pre> 40 * <mx:ArrayCollection 41 * <b>Properties</b> 42 * source="null" 43 * /> 44 * </pre> 45 * 46 * @example The following code creates a simple ArrayCollection object that 47 * accesses and manipulates an array with a single Object element. 48 * It retrieves the element using the IList interface <code>getItemAt</code> 49 * method and an IViewCursor object that it obtains using the ICollectionView 50 * <code>createCursor</code> method. 51 * <pre> 52 * var myCollection:ArrayCollection = new ArrayCollection([ { first: 'Matt', last: 'Matthews' } ]); 53 * var myCursor:IViewCursor = myCollection.createCursor(); 54 * var firstItem:Object = myCollection.getItemAt(0); 55 * var firstItemFromCursor:Object = myCursor.current; 56 * if (firstItem == firstItemFromCursor) 57 * doCelebration(); 58 * </pre> 59 */ 60public class ArrayCollection extends ListCollectionView implements IExternalizable 61{ 62 include "../core/Version.as"; 63 64 //-------------------------------------------------------------------------- 65 // 66 // Constructor 67 // 68 //-------------------------------------------------------------------------- 69 70 /** 71 * Constructor. 72 * 73 * <p>Creates a new ArrayCollection using the specified source array. 74 * If no array is specified an empty array will be used.</p> 75 */ 76 public function ArrayCollection(source:Array = null) 77 { 78 super(); 79 80 this.source = source; 81 } 82 83 //-------------------------------------------------------------------------- 84 // 85 // Properties 86 // 87 //-------------------------------------------------------------------------- 88 89 //---------------------------------- 90 // source 91 //---------------------------------- 92 93 [Inspectable(category="General", arrayType="Object")] 94 [Bindable("listChanged")] //superclass will fire this 95 96 /** 97 * The source of data in the ArrayCollection. 98 * The ArrayCollection object does not represent any changes that you make 99 * directly to the source array. Always use 100 * the ICollectionView or IList methods to modify the collection. 101 */ 102 public function get source():Array 103 { 104 if (list && (list is ArrayList)) 105 { 106 return ArrayList(list).source; 107 } 108 return null; 109 } 110 111 /** 112 * @private 113 */ 114 public function set source(s:Array):void 115 { 116 list = new ArrayList(s); 117 } 118 119 //-------------------------------------------------------------------------- 120 // 121 // Methods 122 // 123 //-------------------------------------------------------------------------- 124 125 /** 126 * @private 127 * Ensures that only the source property is serialized. 128 */ 129 public function readExternal(input:IDataInput):void 130 { 131 if (list is IExternalizable) 132 IExternalizable(list).readExternal(input); 133 else 134 source = input.readObject() as Array; 135 } 136 137 /** 138 * @private 139 * Ensures that only the source property is serialized. 140 */ 141 public function writeExternal(output:IDataOutput):void 142 { 143 if (list is IExternalizable) 144 IExternalizable(list).writeExternal(output); 145 else 146 output.writeObject(source); 147 } 148 149} 150 151} 152