/* * Copyright(c) 2008 the Spark project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package{ import flash.events.ErrorEvent; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.utils.Dictionary; import jp.cohesion.common.Gadget; import jp.cohesion.common.module.BaseModule; import jp.cohesion.common.IKernel; import jp.cohesion.common.module.events.ServerEvent; import jp.cohesion.common.module.MovieLoaderGadget; /* == [Project Cohesion] =================================================== */ /** * システムコマンド包括クラス * * @author $Author$ * @revision $Rev$ * @date $Date$ */ public class SystemCommand extends BaseModule { // コンストラクタ /////////////////////////////////////////////////////////////////// // Constructors // ///////////////////////////////////////////////////////////////////////////////////// /** * デフォルトの設定を用いてオブジェクトを構築するコンストラクタ */ public function SystemCommand(){ super(); } private var movies:Dictionary = new Dictionary(); public static const EVENT_PREFIX:String = "chn:system:"; // インスタンスメソッド ///////////////////////////////////////////////////////////// // Instance Methods // ///////////////////////////////////////////////////////////////////////////////////// /** * * @param kernel */ public override function initialize(kernel:IKernel):void { super.initialize(kernel); kernel.addEventListener(EVENT_PREFIX + "LoadMovie", onLoadMovie); } /** * * @param args * @return */ public function loadMovie(args:Array):Object { if (args === null || args.length < 1) { return null; } var movie:String = args[0]; var pos:String = null; if (args.length > 1) { pos = args[1]; } startLoadingMovie(movie, pos); return null; } /** * * @param args * @return */ public function removeMovie(args:Array):Object { if (args != null && args.length > 0) { var name:String = args[0]; if (name == null) { return null; } var prefix:String = name.substr(0, MovieLoaderGadget.GADGETNAME_PREFIX.length); if (prefix != MovieLoaderGadget.GADGETNAME_PREFIX) { name = MovieLoaderGadget.GADGETNAME_PREFIX + name; } removeMovieGadget(name); } return null; } // イベントハンドラ ///////////////////////////////////////////////////////////////// // Event Handler // ///////////////////////////////////////////////////////////////////////////////////// /** * * @param evt */ public function onLoadMovie(evt:ServerEvent):void { if (evt.data.movie != undefined) { startLoadingMovie(evt.data.movie, evt.data.movie.@position); } } // 内部メソッド ///////////////////////////////////////////////////////////////////// // Private Methods // ///////////////////////////////////////////////////////////////////////////////////// /** * * @param movie * @param position */ public function startLoadingMovie(movie:String, position:String = null):void { var gadget:MovieLoaderGadget = new MovieLoaderGadget(SYSTEM_COMMAND_MODULE, kernel); gadget.url = movie; if (position) { gadget.position = position; } removeMovieGadget(gadget.gadgetName); gadget.addEventListener(Event.COMPLETE, loadComplete); gadget.addEventListener(IOErrorEvent.IO_ERROR, loadFailure); gadget.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadFailure); gadget.load(); } /** * * @param evt */ private function loadFailure(evt:ErrorEvent):void { var gadget:MovieLoaderGadget = evt.target as MovieLoaderGadget; this.kernel.showTemporaryMessage("ムービー\"" + gadget.url + "\"の読み込みに失敗しました。"); } /** * * @param evt */ private function loadComplete(evt:Event):void { var gadget:MovieLoaderGadget = evt.target as MovieLoaderGadget; gadget.removeEventListener(Event.COMPLETE, loadComplete); this.movies[gadget.gadgetName] = gadget; } /** * * @param url */ public function removeMovieByURL(url:String):void { removeMovieGadget(MovieLoaderGadget.GADGETNAME_PREFIX + url); } /** * * @param name */ public function removeMovieGadget(name:String):void { if (name == null) { return; } if (movies[name] == undefined) { return; } var mv:Gadget = movies[name]; try { if (mv.parent != null) { kernel.removeGadget(mv); } }catch (err:Error) { trace(err); } delete movies[name]; } } }