root/as3/Thread/branches/soumen/src/org/libspark/thread/threads/display/LoaderThread.as

リビジョン 637, 6.4 kB (コミッタ: yossy, コミット時期: 4 年 前)

Thread(soumen): どきゅどきゅどっきゅん☆ASDoc 完遂

Line 
1 /*
2  * ActionScript Thread Library
3  *
4  * Licensed under the MIT License
5  *
6  * Copyright (c) 2008 BeInteractive! (www.be-interactive.org) and
7  *                    Spark project  (www.libspark.org)
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  *
27  */
28 package org.libspark.thread.threads.display
29 {
30         import flash.display.Loader;
31         import flash.events.Event;
32         import flash.events.IOErrorEvent;
33         import flash.events.ProgressEvent;
34         import flash.net.URLRequest;
35         import flash.system.LoaderContext;
36         import flash.errors.IOError;
37         import org.libspark.thread.utils.IProgress;
38         import org.libspark.thread.utils.IProgressNotifier;
39         import org.libspark.thread.utils.Progress;
40        
41         import org.libspark.thread.Thread;
42
43         /**
44          * Loader を用いてファイルを読み込むためのスレッドです.
45          *
46          * <p>このスレッドを start すると、与えられた URLRequest と LoaderContext を用いてロード処理を開始し、
47          * ロードが完了 (Event.COMPLETE) するとスレッドが終了します。</p>
48          *
49          * <p>join メソッドを用いると、簡単にロード待ちをすることが出来ます。</p>
50          *
51          * <p>ロード中にエラーが発生した場合は、以下の例外がスローされます。
52          * これからの例外は、このスレッドを start したスレッド (親スレッド) で捕捉することができます。</p>
53          *
54          * <ul>
55          * <li>flash.events.IOErrorEvent.IO_ERROR: flash.errors.IOError</li>
56          * </ul>
57          *
58          * @author      yossy:beinteractive
59          */
60         public class LoaderThread extends Thread implements IProgressNotifier
61         {
62                 /**
63                  * 新しい LoaderThread クラスのインスタンスを生成します.
64                  *
65                  * @param request ロード対象となる URLRequest
66                  * @param context ロードに用いる LoaderContext
67                  * @param loader ロードに使用する Loader 。省略もしくは null の場合、新たに生成した Loader を使用します
68                  */
69                 public function LoaderThread(request:URLRequest, context:LoaderContext = null, loader:Loader = null)
70                 {
71                         _request = request;
72                         _context = context;
73                         _loader = loader != null ? loader : new Loader();
74                         _progress = new Progress();
75                 }
76                
77                 private var _request:URLRequest;
78                 private var _context:LoaderContext;
79                 private var _loader:Loader;
80                 private var _progress:Progress;
81                
82                 /**
83                  * ロード対象となる URLRequest を返します.
84                  */
85                 public function get request():URLRequest
86                 {
87                         return _request;
88                 }
89                
90                 /**
91                  * ロードに用いる LoaderContext を返します.
92                  */
93                 public function get context():LoaderContext
94                 {
95                         return _context;
96                 }
97                
98                 /**
99                  * ロードに使用する Loader を返します.
100                  *
101                  * <p>ロード完了 (スレッド終了) 後に、ロードしたファイル (Loader.content) を取得したい場合などに
102                  * このプロパティを使用します。</p>
103                  */
104                 public function get loader():Loader
105                 {
106                         return _loader;
107                 }
108                
109                 /**
110                  * @inheritDoc
111                  */
112                 public function get progress():IProgress
113                 {
114                         return _progress;
115                 }
116                
117                 /**
118                  * @throws SecurityError
119                  * @private
120                  */
121                 override protected function run():void
122                 {
123                         // イベントハンドラを設定
124                         // Note: イベントハンドラを設定した場合、自動的に wait がかかる
125                         events();
126                        
127                         // 割り込みハンドラを設定
128                         interrupted(interruptedHandler);
129                        
130                         // ロード開始
131                         _loader.load(_request, _context);
132                 }
133                
134                 /**
135                  * イベントハンドラの登録
136                  *
137                  * @private
138                  */
139                 private function events():void
140                 {
141                         event(_loader.contentLoaderInfo, Event.COMPLETE, completeHandler);
142                         event(_loader.contentLoaderInfo, ProgressEvent.PROGRESS, progressHandler);
143                         event(_loader.contentLoaderInfo, IOErrorEvent.IO_ERROR, ioErrorHandler);
144                 }
145                
146                 /**
147                  * まだ開始を通知していなければ通知する
148                  *
149                  * @private
150                  */
151                 private function notifyStartIfNeeded(total:Number):void
152                 {
153                         if (!_progress.isStarted) {
154                                 _progress.start(total);
155                         }
156                 }
157                
158                 /**
159                  * ProgressEvent.PROGRESS ハンドラ
160                  *
161                  * @private
162                  */
163                 private function progressHandler(e:ProgressEvent):void
164                 {
165                         // 必要であれば開始を通知
166                         notifyStartIfNeeded(e.bytesTotal);
167                        
168                         // 進捗を通知
169                         _progress.progress(e.bytesLoaded);
170                        
171                         // 割り込みハンドラを設定
172                         interrupted(interruptedHandler);
173                        
174                         // 再びイベント待ち
175                         events();
176                 }
177                
178                 /**
179                  * Event.COMPLETE ハンドラ
180                  *
181                  * @private
182                  */
183                 private function completeHandler(e:Event):void
184                 {
185                         // 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される)
186                         notifyStartIfNeeded(0);
187                        
188                         // 完了を通知
189                         _progress.complete();
190                        
191                         // ここでスレッド終了
192                 }
193                
194                 /**
195                  * IOErrorEvent.IO_ERROR ハンドラ
196                  *
197                  * @private
198                  */
199                 private function ioErrorHandler(e:IOErrorEvent):void
200                 {
201                         // 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される)
202                         notifyStartIfNeeded(0);
203                        
204                         // 失敗を通知
205                         _progress.fail();
206                        
207                         // IOError をスロー
208                         throw new IOError(e.text);
209                 }
210                
211                 /**
212                  * 割り込みハンドラ
213                  *
214                  * @private
215                  */
216                 private function interruptedHandler():void
217                 {
218                         // 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される)
219                         notifyStartIfNeeded(0);
220                        
221                         // ロードをキャンセル
222                         _loader.close();
223                        
224                         // キャンセルを通知
225                         _progress.cancel();
226                 }
227         }
228 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。