チェンジセット 537

差分発生行の前後
無視リスト:
コミット日時:
2008/05/28 15:29:08 (6 ヶ月前)
コミッタ:
yossy
ログメッセージ:

Thread(soumen): LoaderThread? を新仕様にアップデート

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/Thread/branches/soumen/src/org/libspark/thread/threads/display/LoaderThread.as

    r177 r537  
    1 package org.libspark.display 
     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 */ 
     28package org.libspark.thread.threads.display 
    229{ 
    330        import flash.display.Loader; 
     
    1138 
    1239        /** 
    13          * Loaderを用いてファイルを読み込むためのスレッドです 
     40         * Loader を用いてファイルを読み込むためのスレッドです 
    1441         * </p> 
    15          * このスレッドをbeginすると、与えられたURLRequestとLoaderContextを用いてロード処理を開始し、 
    16          * ロードが完了(Event.COMPLETE)するとスレッドが終了します。 
     42         * このスレッドを start すると、与えられた URLRequest と LoaderContext を用いてロード処理を開始し、 
     43         * ロードが完了 (Event.COMPLETE) するとスレッドが終了します。 
    1744         * </p> 
    18          * joinメソッドを用いると、簡単にロード待ちをすることが出来ます。 
     45         * join メソッドを用いると、簡単にロード待ちをすることが出来ます。 
    1946         * </p> 
    2047         * ロード中にエラーが発生した場合は、以下の例外がスローされます。 
    21          * これからの例外は、このスレッドをbeginしたスレッド(親スレッド)内の 
    22          * catchErrorハンドラで捕捉することができます。 
     48         * これからの例外は、このスレッドを start したスレッド (親スレッド) で捕捉することができます。 
    2349         * </p> 
    2450         * flash.events.IOErrorEvent.IO_ERROR: flash.errors.IOError 
     51         *  
     52         * @author      yossy:beinteractive 
    2553         */ 
    2654        public class LoaderThread extends Thread 
    2755        { 
    2856                /** 
    29                  * 新しいLoaderThreadクラスのインスタンスを生成します 
     57                 * 新しい LoaderThread クラスのインスタンスを生成します 
    3058                 *  
    31                  * @param request ロード対象となるURLRequest 
    32                  * @param context ロードに用いるLoaderContext 
    33                  * @param loader ロードに使用するLoader。省略もしくはnullの場合、新たに生成したLoaderを使用します 
     59                 * @param request ロード対象となる URLRequest 
     60                 * @param context ロードに用いる LoaderContext 
     61                 * @param loader ロードに使用する Loader 。省略もしくは null の場合、新たに生成した Loader を使用します 
    3462                 */ 
    3563                public function LoaderThread(request:URLRequest, context:LoaderContext = null, loader:Loader = null) 
     
    4371                private var _context:LoaderContext; 
    4472                private var _loader:Loader; 
    45                 private var _isLoading:Boolean; 
    46                 private var _error:Error; 
    4773                 
    4874                /** 
    49                  * ロード対象となるURLRequestを返します 
     75                 * ロード対象となる URLRequest を返します 
    5076                 *  
    51                  * @return ロード対象となるURLRequest 
     77                 * @return ロード対象となる URLRequest 
    5278                 */ 
    5379                public function get request():URLRequest 
     
    5783                 
    5884                /** 
    59                  * ロードに用いるLoaderContextを返します 
     85                 * ロードに用いる LoaderContext を返します 
    6086                 * 
    61                  * @return ロードに用いるLoaderContext 
     87                 * @return ロードに用いる LoaderContext 
    6288                 */ 
    6389                public function get context():LoaderContext 
     
    6793                 
    6894                /** 
    69                  * ロードに使用するLoaderを返します 
     95                 * ロードに使用する Loader を返します 
    7096                 * </p> 
    71                  * ロード完了(スレッド終了)後に、ロードしたファイル(Loader.content)を取得したい場合などに 
     97                 * ロード完了 (スレッド終了) 後に、ロードしたファイル (Loader.content) を取得したい場合などに 
    7298                 * このプロパティを使用します。 
    7399                 * 
    74                  * @return ロードに使用するLoader 
     100                 * @return ロードに使用する Loader 
    75101                 */ 
    76102                public function get loader():Loader 
     
    80106                 
    81107                /** 
    82                  * 初期化 
     108                 * 実行 
    83109                 *  
    84110                 * @throws SecurityError 
    85111                 */ 
    86                 protected override function initialize():void 
     112                override protected function run():void  
    87113                { 
    88                         _error = null; 
     114                        // イベントハンドラを設定 
     115                        // Note: イベントハンドラを設定した場合、自動的に wait がかかる 
     116                        event(_loader.contentLoaderInfo, Event.COMPLETE, completeHandler); 
     117                        event(_loader.contentLoaderInfo, IOErrorEvent.IO_ERROR, ioErrorHandler); 
    89118                         
    90                         _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
    91                         _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
    92                          
    93                         // ロードした瞬間にエラーイベントが発生してnotifyAllされるかもしれないので 
    94                         // 先にwaitしておく(notifyAllするまでexecuteは呼び出されなくなる) 
    95                         wait(); 
     119                        // 割り込みハンドラを設定 
     120                        interrupted(interruptedHandler); 
    96121                         
    97122                        // ロード開始 
    98123                        _loader.load(_request, _context); 
    99                          
    100                         _isLoading = true; 
    101                 } 
    102                  
    103                 /** 
    104                  * 実行 
    105                  */ 
    106                 protected override function execute():void 
    107                 { 
    108                         _isLoading = false; 
    109                          
    110                         // 例外はexecuteメソッド(もしくはswitchExecuteMethodで指定したメソッド)内で 
    111                         // スローしないと、catchErrorなどで捕捉できないので注意 
    112                         if (_error != null) { 
    113                                 throw _error; 
    114                         } 
    115                          
    116                         // おわる 
    117                         terminate(); 
    118                 } 
    119                  
    120                 /** 
    121                  * 終了処理 
    122                  */ 
    123                 protected override function finalize():void 
    124                 { 
    125                         // ちなみに、例外によって終了した場合もfinalizeは呼ばれる 
    126                         if (_isLoading) { 
    127                                 _loader.close(); 
    128                                 _isLoading = false; 
    129                         } 
    130                         _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); 
    131                         _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
    132124                } 
    133125                 
     
    137129                private function completeHandler(e:Event):void 
    138130                { 
    139                         // 何事も無く無事終了したのでスレッドを起こす→executeが呼ばれてterminateする 
    140                         notifyAll(); 
     131                        // 何もしない → スレッドが終了する 
    141132                } 
    142133                 
     
    146137                private function ioErrorHandler(e:IOErrorEvent):void 
    147138                { 
    148                         // 例外をセットしてスレッドを起こす→executeが呼ばれて例外が投げられる 
    149                         _error = new IOError(e.text); 
    150                         notifyAll(); 
     139                        // IOError をスロー 
     140                        throw new IOError(e.text); 
    151141                } 
    152142                 
    153                 public override function toString():String 
     143                /** 
     144                 * 割り込みハンドラ 
     145                 */ 
     146                private function interruptedHandler():void 
    154147                { 
    155                         return '[Loader]'; 
     148                        // ロードをキャンセル 
     149                        _loader.close(); 
    156150                } 
    157151        }