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     *  @langversion 3.0
45     *  @playerversion Flash 9
46     *  @playerversion AIR 1.1
47     *  @productversion Flex 3
48     */
49    public function RSLListLoader(rslList:Array)
50    {
51    	super();
52        this.rslList = rslList;
53    }
54
55	//--------------------------------------------------------------------------
56	//
57	//  Variables
58	//
59	//--------------------------------------------------------------------------
60
61    /**
62	 *  @private
63	 *  The index of the RSL being loaded.
64	 */
65	private var currentIndex:int = 0;
66
67    /**
68	 *  @private
69	 *  The list of RSLs to load.
70	 *  Each entry is of type RSLNode or CdRSLNode.
71	 */
72	private var rslList:Array = [];
73
74    /**
75	 *  @private
76	 *  Supplied by caller.
77	 */
78	private var chainedProgressHandler:Function;
79
80    /**
81	 *  @private
82	 *  Supplied by caller.
83	 */
84	private var chainedCompleteHandler:Function;
85
86    /**
87	 *  @private
88	 *  Supplied by caller.
89	 */
90	private var chainedIOErrorHandler:Function;
91
92    /**
93	 *  @private
94	 *  Supplied by caller.
95	 */
96	private var chainedSecurityErrorHandler:Function;
97
98    /**
99	 *  @private
100	 *  Supplied by caller.
101	 */
102	private var chainedRSLErrorHandler:Function;
103
104	//--------------------------------------------------------------------------
105	//
106	//  Methods
107	//
108	//--------------------------------------------------------------------------
109
110    /**
111     *  Loads the RSLs in this list object.
112     *
113     *  <p>This function accepts listeners to observe events
114	 *  that result from loading an RSL.
115     *  The function must be of the form required by a listener function
116     *  passed to <code>IEventDispatcher.addEventListener();</code>
117     *
118     *  When an event is received, the RSL that generated
119	 *  this event can be found by calling
120	 *  <code>rslListLoader.getItem(getIndex());</code></p>
121     *
122     *  @param progressHandler Receives ProgressEvent.PROGRESS events.
123	 *  May be null.
124	 *
125     *  @param completeHandler Receives Event.COMPLETE events.
126	 *  May be null.
127	 *
128     *  @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events.
129	 *  May be null.
130	 *
131     *  @param securityErrorHandler
132	 *  Receives SecurityErrorEvent.SECURITY_ERROR events.
133	 *  May be null.
134	 *
135     *  @param rslErrorHandler Receives RSLEvent.RSL_ERROR events.
136	 *  May be null.
137     *
138     *  @langversion 3.0
139     *  @playerversion Flash 9
140     *  @playerversion AIR 1.1
141     *  @productversion Flex 3
142     */
143 	public function load(progressHandler:Function,
144	                     completeHandler:Function,
145	                     ioErrorHandler:Function,
146	                     securityErrorHandler:Function,
147	                     rslErrorHandler:Function):void
148    {
149    	chainedProgressHandler = progressHandler;
150	    chainedCompleteHandler = completeHandler;
151	    chainedIOErrorHandler = ioErrorHandler;
152	    chainedSecurityErrorHandler = securityErrorHandler;
153	    chainedRSLErrorHandler = rslErrorHandler;
154
155	    currentIndex = -1; // so first loaded item will be index 0
156	    loadNext();
157
158    }
159
160    /**
161     *  Increments the current index and loads the next RSL.
162     *
163     *  @langversion 3.0
164     *  @playerversion Flash 9
165     *  @playerversion AIR 1.1
166     *  @productversion Flex 3
167     */
168    private function loadNext():void
169    {
170        if (!isDone())
171        {
172            currentIndex++;
173
174	        // Load the current RSL.
175	        if (currentIndex < rslList.length)
176	        {
177	            // Load rsl and have the RSL loader chain the
178	            // events our internal events handler or the chained
179	            // events handler if we don't care about them.
180	            rslList[currentIndex].load(chainedProgressHandler,
181	                                       listCompleteHandler,
182	                                       listIOErrorHandler,
183	                                       listSecurityErrorHandler,
184	                                       chainedRSLErrorHandler);
185	        }
186        }
187    }
188
189
190    /**
191     *  Gets an RSL from the list of RSLs.
192     *
193     *  @param index A zero-based index into the list of RSLs.
194     *
195     *  @return The current item at <code>index</code> in the list,
196	 *  or <code>null</code> if there is no item at that index.
197     *
198     *  @langversion 3.0
199     *  @playerversion Flash 9
200     *  @playerversion AIR 1.1
201     *  @productversion Flex 3
202     */
203    public function getItem(index:int):RSLItem
204    {
205        if (index < 0 || index >= rslList.length)
206            return null;
207
208        return rslList[index];
209    }
210
211
212    /**
213     *  Gets the index of the currently loading RSL.
214     *
215     *  @return The index of the currently loading RSL.
216     *
217     *  @langversion 3.0
218     *  @playerversion Flash 9
219     *  @playerversion AIR 1.1
220     *  @productversion Flex 3
221     */
222    public function getIndex():int
223    {
224        return currentIndex;
225    }
226
227
228   /**
229    *  Gets the total number of RSLs in this object.
230	*  When the load() method is called the RSLs will be loaded.
231    *
232    *  @return The total number of RSLs in this object
233    *
234    *  @langversion 3.0
235    *  @playerversion Flash 9
236    *  @playerversion AIR 1.1
237    *  @productversion Flex 3
238    */
239    public function getItemCount():int
240    {
241        return rslList.length;
242    }
243
244    /**
245     *  Tests if all the RSLs are done loading.
246     *
247     *  @return <code>true</code> if all the RSLs have been loaded,
248	 *  <code>false</code> otherwise.
249     *
250     *  @langversion 3.0
251     *  @playerversion Flash 9
252     *  @playerversion AIR 1.1
253     *  @productversion Flex 3
254     */
255    public function isDone():Boolean
256    {
257        return currentIndex >= rslList.length;
258    }
259
260	//--------------------------------------------------------------------------
261	//
262	//  Event handlers
263	//
264	//--------------------------------------------------------------------------
265
266    /**
267	 *  @private
268	 */
269	private function listCompleteHandler(event:Event):void
270    {
271        // Pass event to external listener.
272        if (chainedCompleteHandler != null)
273            chainedCompleteHandler(event);
274
275        // Load the next RSL.
276        loadNext();
277    }
278
279    /**
280	 *  @private
281	 */
282    private function listIOErrorHandler(event:Event):void
283    {
284        // Pass event to external listener.
285        if (chainedIOErrorHandler != null)
286            chainedIOErrorHandler(event);
287    }
288
289    /**
290	 *  @private
291	 */
292    private function listSecurityErrorHandler(event:Event):void
293    {
294        // Pass event to external listener.
295        if (chainedSecurityErrorHandler != null)
296            chainedSecurityErrorHandler(event);
297    }
298}
299
300}
301