00001 import gugga.collections.HashTable;
00002 import gugga.common.ICommand;
00003 import gugga.common.UIComponentEx;
00004 import gugga.debug.Assertion;
00005
00009 class gugga.commands.CommandFactory extends UIComponentEx {
00010
00011 private static var mInstance:CommandFactory = undefined;
00012
00013 public static function get Instance():CommandFactory
00014 {
00015 if (mInstance == undefined)
00016 {
00017 mInstance = new CommandFactory();
00018 }
00019 return mInstance;
00020 }
00021 private function CommandFactory() {
00022 super();
00023 mCommandCatalog = new HashTable();
00024 }
00025
00026 private var mCommandCatalog:HashTable;
00027
00028 public function create(aCommandType:String,aParams:HashTable):ICommand
00029 {
00030
00031 if (!mCommandCatalog.containsKey(aCommandType) || mCommandCatalog[aCommandType] == undefined)
00032 {
00033 Assertion.warning("Command " + aCommandType + " is not defined.", this, arguments);
00034
00035 return undefined;
00036 }
00037
00038 var cmd:ICommand = ICommand(mCommandCatalog[aCommandType]).clone();
00039
00040
00041 for(var key:String in aParams)
00042 {
00043 cmd[key] = aParams[key];
00044 }
00045 return cmd;
00046 }
00047
00048
00049
00050 public function createFromArgumnets(aCommandType:String,aArguments:String)
00051 {
00052 var argumentsArray:Array = aArguments.split("&");
00053 var paramsHash:HashTable = new HashTable();
00054
00055 for (var i = 0 ; i < argumentsArray.length ; i++)
00056 {
00057 var argNameValue:Array = argumentsArray[0].split("&",2);;
00058 if (argNameValue.length = 2)
00059 paramsHash[argNameValue[0]] = argNameValue[1];
00060 else
00061 paramsHash[argNameValue[0]] = true;
00062 }
00063
00064 return create(aCommandType,paramsHash);
00065
00066 }
00067
00068 public function add(aCommandType:String,aCommand:ICommand)
00069 {
00070 if (mCommandCatalog.containsKey(aCommandType) && mCommandCatalog[aCommandType] != undefined)
00071 {
00072 Assertion.warning("Command " + aCommandType + " already added. To replace it use replace instead", this, arguments);
00073 return;
00074 }
00075 mCommandCatalog[aCommandType] = aCommand;
00076 }
00077
00078
00079 public function replace(aCommandType:String,aCommandPrototype:ICommand)
00080 {
00081 mCommandCatalog[aCommandType] = aCommandType;
00082 }
00083 public function remove(aCommandType:String)
00084 {
00085 mCommandCatalog[aCommandType] = undefined;
00086 }
00087
00088 }