参考我前面的文章http://blog.csdn.net/rubyzhudragon/archive/2009/06/10/4257836.aspx
因为项目开始需求还不清楚,代码几乎没有遵循OO原则,今天有空重写了一些代码,
这样的话无需太多的if-else了。
public class XComponent{
public function reset():void{}
public function getVal():String{
return "";
}
public function getVo():IdNameVo{
return null;
}
}
public class XTextInput extends XComponent
{
public var idname:String;
public var t:TextInput;
public var validator:Validator;
public function XTextInput(idname:String,len:int,val:String)
{
t=new IextInput();
this.idname=idname;
init(idname,len,val);
}
public function XTextInput(idname:String,len:int)
{
super(idname,len);
}
private function init(len:int,val:String):void{
t.id=this.idname;
t.visible=true;
t.height=20;
if(len!=0){
t.width=len*12;
}
t.text=val;
}
override public function reset():void{
t.text="";
}
override public function getVal():String{
return t.text;
}
override public function getVo():IdNameVo{
if(t.text!=""){
vo=new IdNameVo();
vo.name=idname;
vo.desc=t.text;
return vo;
}
}
public function resetVal(val:String):void{
t.text=val;
}
public setValidator(obj:Object){
this.validator=obj["validator"];
this.validator.source=t;
this.validator.property="text";
this.validator.required =obj["required"];
this.validator.requiredFieldError=obj["msg"];
}
}
public class XCombo extends XComponent
{
public var c:ComboBox;
public var idname:String;
public var index:int;
public var ac:ArrayCollection;
public var validator:Validator;
public function XCombo(c:ComboBox,idname:String,index:int,ac:ArrayCollection)
{
this.c=new ComboBox();
this.idname= idname;
this.index=index;
this.ac=ac;
init();
}
private function init():void{
c.id=this.idname;
c.visible=true;
c.height=20;
c.labelField="name";
c.dataProvider=this.ac;
if(index!=-1) c.selectedIndex=index;
else c.selectedIndex=-1;
}
override public function reset():void{
c.selectedIndex=-1;
this.index=-1;
}
override public function getVo():IdNameVo{
if(this.index!=-1){
vo=new IdNameVo();
vo.name=idname;
vo.desc=c.selectedItem['name'];
return vo;
}
return null;
}
override public function getVal():String{
return c.selectedItem['name'];
}
public function resetVal(index:int):void{
c.selectedIndex=index;
this.index=index;
}
public setValidator(obj:Object){
this.validator=obj["validator"];
this.validator.source=t;
this.validator.property="text";
validator.required =obj["required"];
validator.requiredFieldError=obj["msg"];
}
}
public class XCombine extends XComponent
{
public var h:HBox;
public var policy:String="";
public var validators:Array;
private var symbol:XTextInput;
private var number:XTextInput
private var mod:XTextInput;
public function XCombine(policy:String)
{
h=new HBox();
this.policy=policy;
init();
}
private function init():void{
symbol=new XTextInput("symbol",3);
number=new XTextInput("number",7);
mod=new XTextInput("mod",2);
resetVal(this.policy);
}
override public function resetVal(policy:String):void{
if(policy!=""){
symbol.resetVal(policy.substr(0,3));
number.resetVal(policy.substr(3,7));
number.resetVal(policy.substr(10,2));
}
}
override public function reset():void{
symbol.text="";
number.text="";
mod.text="";
}
override public function getVo():IdNameVo{
var val:String==symbol.text?"%":symbol.text
+number.text?"%":number.text+mod.text?"%":mod.text;
if(t.text!="%%%"){
vo=new IdNameVo();
vo.name="INDEX2";
vo.desc=t.text+symbol.text+mod.text;
return vo;
}
}
public function getVal():String{
return symbol.text+number.text+mod.text;
}
public function setValidator:void(obj:Object){
symbol.setValidator(obj['symbol']);
number.setValidator(obj['number']);
mod.setValidator(obj['mod']);
validators.push(symbol.validator);
validators.push(number.validator);
validators.push(mod.validator);
}
}