root/as3/SiOPM/trunk/src/org/si/utils/AbstructLoader.as

リビジョン 4264, 4.7 kB (コミッタ: keim, コミット時期: 2 年 前)

SiON ver0.61 updated

Line 
1 //----------------------------------------------------------------------------------------------------
2 // Loader basic class
3 //  Copyright (c) 2008 keim All rights reserved.
4 //  Distributed under BSD-style license (see org.si.license.txt).
5 //----------------------------------------------------------------------------------------------------
6
7
8
9
10 package org.si.utils {
11     import flash.events.*;
12     import flash.net.*;
13     import flash.utils.ByteArray;
14    
15    
16     /** Loader basic class. */
17     public class AbstructLoader extends EventDispatcher
18     {
19     // valiables
20     //------------------------------------------------------------
21         /** loader */
22         protected var _loader:URLLoader;
23         /** total bytes */
24         protected var _bytesTotal:Number;
25         /** loaded bytes */
26         protected var _bytesLoaded:Number;
27         /** flag complete loading */
28         protected var _isLoadCompleted:Boolean;
29         /** child loaders */
30         protected var _childLoaders:Array;
31         /** event priority */
32         protected var _eventPriority:int;
33        
34        
35        
36        
37     // constructor
38     //------------------------------------------------------------
39         /** Constructor */
40         function AbstructLoader(priority:int = 0)
41         {
42             _loader = new URLLoader();
43             _bytesTotal = 0;
44             _bytesLoaded = 0;
45             _isLoadCompleted = false;
46             _childLoaders = [];
47             _eventPriority = priority;
48         }
49        
50        
51        
52        
53     // operation
54     //------------------------------------------------------------
55         /** load */
56         public function load(url:URLRequest) : void
57         {
58             _loader.close();
59             _bytesTotal = 0;
60             _bytesLoaded = 0;
61             _isLoadCompleted = false;
62             _addAllListeners();
63             _loader.load(url);
64         }
65        
66        
67         /** add child loader */
68         public function addChild(child:AbstructLoader) : void
69         {
70             _childLoaders.push(child);
71             child.addEventListener(Event.COMPLETE, _onChildComplete);
72         }
73        
74        
75        
76        
77     // virtual function
78     //------------------------------------------------------------
79         /** overriding function when completes loading */
80         protected function onComplete() : void { }
81        
82        
83        
84        
85     // default handler
86     //------------------------------------------------------------
87         private function _onProgress(e:ProgressEvent) : void
88         {
89             _bytesTotal  = e.bytesTotal;
90             _bytesLoaded = e.bytesLoaded;
91             _isLoadCompleted = false;
92             dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _bytesLoaded, _bytesTotal));
93         }
94        
95        
96         private function _onComplete(e:Event) : void
97         {
98             _removeAllListeners();
99             _bytesLoaded = _bytesTotal;
100             _isLoadCompleted = true;
101             onComplete();
102             if (_childLoaders.length == 0) {
103                 dispatchEvent(new Event(Event.COMPLETE));
104             }
105         }
106
107    
108         private function _onError(e:ErrorEvent) : void
109         {
110             _removeAllListeners();
111             dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, e.toString()));
112         }
113        
114        
115         private function _onChildComplete(e:Event) : void
116         {
117             var index:int = _childLoaders.indexOf(e.target);
118             if (index == -1) throw new Error("AbstructLoader; unkown error, children mismatched.");
119             _childLoaders.splice(index, 1);
120             if (_childLoaders.length == 0 && _isLoadCompleted) {
121                 dispatchEvent(new Event(Event.COMPLETE));
122             }
123         }
124        
125        
126         private function _addAllListeners() : void
127         {
128             _loader.addEventListener(Event.COMPLETE, _onComplete, false, _eventPriority);
129             _loader.addEventListener(ProgressEvent.PROGRESS, _onProgress, false, _eventPriority);
130             _loader.addEventListener(IOErrorEvent.IO_ERROR, _onError, false, _eventPriority);
131             _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _onError, false, _eventPriority);
132         }
133        
134        
135         private function _removeAllListeners() : void
136         {
137             _loader.removeEventListener(Event.COMPLETE, _onComplete);
138             _loader.removeEventListener(ProgressEvent.PROGRESS, _onProgress);
139             _loader.removeEventListener(IOErrorEvent.IO_ERROR, _onError);
140             _loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, _onError);
141         }
142     }
143 }
144
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。