1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 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.core
13{
14
15import flash.events.IEventDispatcher;
16import flash.events.Event;
17
18[ExcludeClass]
19
20/**
21 *  @private
22 *  Utility class for loading a list of RSLs.
23 *
24 *  <p>A list of cross-domain RSLs and a list of regular RSLs
25 *  can be loaded using this utility.</p>
26 */
27public class RSLListLoader
28{
29    include "../core/Version.as";
30
31	//--------------------------------------------------------------------------
32	//
33	//  Constructor
34	//
35	//--------------------------------------------------------------------------
36
37    /**
38     *  Constructor.
39     *
40     *  @param rslList Array of RSLs to load.
41	 *  Each entry in the array is of type RSLItem or CdRSLItem.
42     *  The RSLs will be loaded from index 0 to the end of the array.
43     */
44    public function RSLListLoader(rslList:Array)
45    {
46    	super();
47        this.rslList = rslList;
48    }
49
50	//--------------------------------------------------------------------------
51	//
52	//  Variables
53	//
54	//--------------------------------------------------------------------------
55
56    /**
57	 *  @private
58	 *  The index of the RSL being loaded.
59	 */
60	private var currentIndex:int = 0;
61
62    /**
63	 *  @private
64	 *  The list of RSLs to load.
65	 *  Each entry is of type RSLNode or CdRSLNode.
66	 */
67	private var rslList:Array = [];
68
69    /**
70	 *  @private
71	 *  Supplied by caller.
72	 */
73	private var chainedProgressHandler:Function;
74
75    /**
76	 *  @private
77	 *  Supplied by caller.
78	 */
79	private var chainedCompleteHandler:Function;
80
81    /**
82	 *  @private
83	 *  Supplied by caller.
84	 */
85	private var chainedIOErrorHandler:Function;
86
87    /**
88	 *  @private
89	 *  Supplied by caller.
90	 */
91	private var chainedSecurityErrorHandler:Function;
92
93    /**
94	 *  @private
95	 *  Supplied by caller.
96	 */
97	private var chainedRSLErrorHandler:Function;
98
99	//--------------------------------------------------------------------------
100	//
101	//  Methods
102	//
103	//--------------------------------------------------------------------------
104
105    /**
106     *  Loads the RSLs in this list object.
107     *
108     *  <p>This function accepts listeners to observe events
109	 *  that result from loading an RSL.
110     *  The function must be of the form required by a listener function
111     *  passed to <code>IEventDispatcher.addEventListener();</code>
112     *
113     *  When an event is received, the RSL that generated
114	 *  this event can be found by calling
115	 *  <code>rslListLoader.getItem(getIndex());</code></p>
116     *
117     *  @param progressHandler Receives ProgressEvent.PROGRESS events.
118	 *  May be null.
119	 *
120     *  @param completeHandler Receives Event.COMPLETE events.
121	 *  May be null.
122	 *
123     *  @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events.
124	 *  May be null.
125	 *
126     *  @param securityErrorHandler
127	 *  Receives SecurityErrorEvent.SECURITY_ERROR events.
128	 *  May be null.
129	 *
130     *  @param rslErrorHandler Receives RSLEvent.RSL_ERROR events.
131	 *  May be null.
132     */
133 	public function load(progressHandler:Function,
134	                     completeHandler:Function,
135	                     ioErrorHandler:Function,
136	                     securityErrorHandler:Function,
137	                     rslErrorHandler:Function):void
138    {
139    	chainedProgressHandler = progressHandler;
140	    chainedCompleteHandler = completeHandler;
141	    chainedIOErrorHandler = ioErrorHandler;
142	    chainedSecurityErrorHandler = securityErrorHandler;
143	    chainedRSLErrorHandler = rslErrorHandler;
144
145	    currentIndex = -1; // so first loaded item will be index 0
146	    loadNext();
147
148    }
149
150    /**
151     *  Increments the current index and loads the next RSL.
152     */
153    private function loadNext():void
154    {
155        if (!isDone())
156        {
157            currentIndex++;
158
159	        // Load the current RSL.
160	        if (currentIndex < rslList.length)
161	        {
162	            // Load rsl and have the RSL loader chain the
163	            // events our internal events handler or the chained
164	            // events handler if we don't care about them.
165	            rslList[currentIndex].load(chainedProgressHandler,
166	                                       listCompleteHandler,
167	                                       listIOErrorHandler,
168	                                       listSecurityErrorHandler,
169	                                       chainedRSLErrorHandler);
170	        }
171        }
172    }
173
174
175    /**
176     *  Gets an RSL from the list of RSLs.
177     *
178     *  @param index A zero-based index into the list of RSLs.
179     *
180     *  @return The current item at <code>index</code> in the list,
181	 *  or <code>null</code> if there is no item at that index.
182     */
183    public function getItem(index:int):RSLItem
184    {
185        if (index < 0 || index >= rslList.length)
186            return null;
187
188        return rslList[index];
189    }
190
191
192    /**
193     *  Gets the index of the currently loading RSL.
194     *
195     *  @return The index of the currently loading RSL.
196     */
197    public function getIndex():int
198    {
199        return currentIndex;
200    }
201
202
203   /**
204    *  Gets the total number of RSLs in this object.
205	*  When the load() method is called the RSLs will be loaded.
206    *
207    *  @return The total number of RSLs in this object
208    */
209    public function getItemCount():int
210    {
211        return rslList.length;
212    }
213
214    /**
215     *  Tests if all the RSLs are done loading.
216     *
217     *  @return <code>true</code> if all the RSLs have been loaded,
218	 *  <code>false</code> otherwise.
219     */
220    public function isDone():Boolean
221    {
222        return currentIndex >= rslList.length;
223    }
224
225	//--------------------------------------------------------------------------
226	//
227	//  Event handlers
228	//
229	//--------------------------------------------------------------------------
230
231    /**
232	 *  @private
233	 */
234	private function listCompleteHandler(event:Event):void
235    {
236        // Pass event to external listener.
237        if (chainedCompleteHandler != null)
238            chainedCompleteHandler(event);
239
240        // Load the next RSL.
241        loadNext();
242    }
243
244    /**
245	 *  @private
246	 */
247    private function listIOErrorHandler(event:Event):void
248    {
249        // Pass event to external listener.
250        if (chainedIOErrorHandler != null)
251            chainedIOErrorHandler(event);
252    }
253
254    /**
255	 *  @private
256	 */
257    private function listSecurityErrorHandler(event:Event):void
258    {
259        // Pass event to external listener.
260        if (chainedSecurityErrorHandler != null)
261            chainedSecurityErrorHandler(event);
262    }
263}
264
265}
266