自定義DateField

MyDateChooser.as

package hhmmss
{
 import mx.controls.DateChooser; 
 
 //重寫DateChooser,添加了時分秒的選擇  
 public class MyDateChooser extends DateChooser 
 { 
  public var times:MyTimesChoose; 
  
  public function MyDateChooser() 
  { 

   super(); 
   
   this.width=195; 
   this.height=195; 
   this.setStyle("fontSize", "12"); 
   times = new MyTimesChoose(); 
   times.x=0; 
   times.y=this.height; 
  } 
  
  protected override function createChildren():void 
  { 
   super.createChildren(); 
   addChild(times); 
  } 
 }
}


MyDateField.as

package hhmmss

{
 import flash.events.Event;
 import flash.events.KeyboardEvent;
 import flash.events.MouseEvent;
 import flash.ui.Keyboard;
 
 import mx.controls.DateField;
 import mx.core.ClassFactory;
 import mx.events.CalendarLayoutChangeEvent;
 import mx.events.FlexEvent;
 import mx.formatters.DateFormatter;
 
 //重寫DateField,設置默認中文顯示,添加了時分秒的選擇  
 public class MyDateField extends DateField 
 { 
  
  public var hours:int;
  public var minutes:int;
  public var seconds:int;
  
  public function MyDateField() 
  { 
   super(); 
   this.formatString="YYYY-MM-DD"; 
   this.dropdownFactory=new ClassFactory(MyDateChooser); 
   this.labelFunction=formatDate;
   this.editable=false; 
   this.addEventListener(MouseEvent.DOUBLE_CLICK,closeDateField);
  } 
  
  public override function initialize():void{
   bindTimes();
   super.initialize();
  }
  
  private function bindTimes():void{
   if(this.text){
    var bindDate:Date = stringToDate(this.text);
    hours = bindDate.hours;
    minutes = bindDate.minutes;
    seconds = bindDate.seconds;
   }
  }
  
  private function initHMS():void{
   bindTimes();
   if(this.hours+this.minutes+this.seconds>0){
    var tempNmsDateChooser:MyDateChooser=this.dropdown as MyDateChooser;
    if(tempNmsDateChooser || tempNmsDateChooser.times.nmsHour){
     this.open();
    }
    tempNmsDateChooser.times.nmsHour.selectedIndex = this.hours;
    tempNmsDateChooser.times.nmsMinute.selectedIndex = this.minutes;
    tempNmsDateChooser.times.nmsSecond.selectedIndex = this.seconds;
    if(this.selectedDate){
     this.selectedDate.hours = this.hours;
     this.selectedDate.minutes = this.minutes;
     this.selectedDate.seconds = this.seconds;
     this.text = dateToString(this.selectedDate, "YYYY-MM-DD JJ:NN:SS");
     //選中當前選擇日期
     var selectedDate:Date = new Date(this.selectedDate.fullYear, this.selectedDate.month, this.selectedDate.date);
     this.selectedDate = selectedDate;
    }
   }
  }
  
  protected override function downArrowButton_buttonDownHandler(
   event:FlexEvent):void{
   super.downArrowButton_buttonDownHandler(event);
   initHMS();
  }
  
  private function formatDate(currentDate:Date):String 
  { 
   var tempNmsDateChooser:MyDateChooser=this.dropdown as MyDateChooser;
   if(tempNmsDateChooser && tempNmsDateChooser.times.nmsHour){
    
    currentDate.hours=(Number)(tempNmsDateChooser.times.nmsHour.selectedItem); 
    currentDate.minutes=(Number)(tempNmsDateChooser.times.nmsMinute.selectedItem); 
    currentDate.seconds=(Number)(tempNmsDateChooser.times.nmsSecond.selectedItem); 
   }
   return dateToString(currentDate, "YYYY-MM-DD JJ:NN:SS");
  } 
  
  public function closeDateField():void{
   //此處處理在時分秒下拉框出現時,關閉控件下拉框不會收回去的bug
   var tempNmsDateChooser:MyDateChooser=this.dropdown as MyDateChooser;
   tempNmsDateChooser.times.nmsHour.close(); 
   tempNmsDateChooser.times.nmsMinute.close(); 
   tempNmsDateChooser.times.nmsSecond.close(); 
   this.close();
  }
  
  
  public function stringToDate(dateString:String):Date{
   var reg:RegExp = null;
   var array:Array = null;
   var year:int;
   var month:int;
   var day:int;
   var hour:int;
   var minute:int;
   var second:int;
   
   reg=/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
   array = dateString.match(reg);
   if(array!=null){
    year = int(array[1]);
    month = int(array[2])-1;
    day = int(array[3]);
    hour = int(array[4]);
    minute = int(array[5]);
    second = int(array[6]);
    return new Date(year,month,day,hour, minute, second);
   }
   
   reg=/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
   array = dateString.match(reg);
   if(array!=null){
    year = int(array[1]);
    month = int(array[2])-1;
    day = int(array[3]);
    hour = int(array[4]);
    minute = int(array[5]);
    second = int(array[6]);
    return new Date(year,month,day,hour, minute, second);
   }
   
   return null;
  }
  
  
  public function dateToString(date:Date, formatString:String = "YYYYMMDDJJNNSS"):String{
   var tempNmsDateChooser:MyDateChooser=this.dropdown as MyDateChooser;
   if(tempNmsDateChooser && tempNmsDateChooser.times.nmsHour){
    
    date.hours=(Number)(tempNmsDateChooser.times.nmsHour.selectedItem); 
    date.minutes=(Number)(tempNmsDateChooser.times.nmsMinute.selectedItem); 
    date.seconds=(Number)(tempNmsDateChooser.times.nmsSecond.selectedItem); 
   }
   var format:DateFormatter = new DateFormatter();
   format.formatString = formatString;
   return format.format(date);
  }
 }
}


MyTimesChoose.as

package hhmmss

{
 import flash.events.Event;
 import flash.events.MouseEvent;
 
 import mx.containers.HBox;
 import mx.containers.Panel;
 import mx.controls.Button;
 import mx.controls.ComboBox;
 import mx.controls.Label;
 import mx.formatters.DateFormatter;
 
 //重寫Panel,實現時分秒的選擇  
 
 public class MyTimesChoose extends Panel 
 {
  public var timesBox:HBox; 
  
  public var labMinute:Label; 
  public var labSecond:Label; 
  
  //時分秒下拉框  
  public var nmsHour:ComboBox; 
  public var nmsMinute:ComboBox; 
  public var nmsSecond:ComboBox;
  
  //獲取當前時間
  public var now:Button;
  
  private var parseDate:Date; 
  
  //小時  
  private var dataSourceHour:Array=["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]; 
  
  //分鐘和秒  
  private var dataSourceMinuteSecond:Array=["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"]; 
  
  //時分秒下拉框的填充色  
  private var arryBackgroundColor:Array=["white", "white", "white", "white"]; 
  
  public function MyTimesChoose() 
  { 
   super(); 
   this.width=195; 
   this.height=23; 
   
   this.setStyle("headerHeight", 0); 
   this.setStyle("borderStyle", "solid"); 
   this.setStyle("borderThicknessLeft", 1); 
   this.setStyle("borderThicknessRight", 1); 
   this.setStyle("cornerRadius", 0); 
   
   timesBox=new HBox();
   timesBox.setStyle("horizontalGap", "0"); 
   timesBox.setStyle("verticalGap", "0");
   timesBox.setStyle("verticalAlign", "middle");
   timesBox.setStyle("backgroundColor", "white"); 
   timesBox.setStyle("paddingLeft", "2");
   timesBox.setStyle("paddingBottom", "2"); 
   timesBox.setStyle("borderStyle", "none"); 
  } 
  
  protected override function createChildren():void 
  { 
   super.createChildren(); 
   
   if (!nmsHour) 
   { 
    nmsHour=new ComboBox(); 
    nmsHour.width=45; 
    nmsHour.height=18; 
    nmsHour.dataProvider=dataSourceHour; 
    nmsHour.setStyle("cornerRadius", "0"); 
    nmsHour.setStyle("fontSize", "10"); 
    nmsHour.setStyle("fillColors", arryBackgroundColor); 
    nmsHour.addEventListener("change", updateValue); 
   } 
   
   if (!labMinute) 
   { 
    labMinute=new Label(); 
    labMinute.width=6;
    labMinute.text=":"; 
   } 
   
   if (!nmsMinute) 
   { 
    nmsMinute=new ComboBox(); 
    nmsMinute.width=45; 
    nmsMinute.height=18; 
    nmsMinute.dataProvider=dataSourceMinuteSecond; 
    nmsMinute.setStyle("fontSize", "10"); 
    nmsMinute.setStyle("cornerRadius", "0"); 
    nmsMinute.setStyle("fillColors", arryBackgroundColor); 
    nmsMinute.addEventListener("change", updateValue); 
   } 
   
   if (!labSecond) 
   { 
    labSecond=new Label(); 
    labSecond.width=6; 
    labSecond.text=":"; 
   } 
   
   if (!nmsSecond) 
   { 
    nmsSecond=new ComboBox(); 
    nmsSecond.width=45; 
    nmsSecond.height=18; 
    nmsSecond.setStyle("fontSize", "10"); 
    nmsSecond.setStyle("cornerRadius", "0"); 
    nmsSecond.setStyle("fillColors", arryBackgroundColor); 
    nmsSecond.dataProvider=dataSourceMinuteSecond; 
    nmsSecond.addEventListener("change", updateValue); 
   } 
   
   if(!now){
    now = new Button();
    now.label = "now";
    now.width = 42;
    now.height = 18;
    now.addEventListener(MouseEvent.CLICK,setNowDate);
   }
   
   timesBox.addChild(nmsHour); 
   timesBox.addChild(labMinute); 
   timesBox.addChild(nmsMinute); 
   timesBox.addChild(labSecond); 
   timesBox.addChild(nmsSecond);
   timesBox.addChild(now); 
   
   this.addChild(timesBox); 
  } 
  
  //當下拉時分秒下拉框的值改變的時候,動態修改日期控制的textinput的顯示值  
  private function updateValue(event:Event):void 
  { 
   if (this.parent is MyDateChooser) 
   { 
    var dateChooser:MyDateChooser=this.parent as MyDateChooser; 
    //若沒有選擇日期則默認爲當天  
    if (dateChooser.selectedDate == null) 
    { 
     parseDate=new Date(); 
    } 
    else 
    { 
     parseDate=dateChooser.selectedDate; 
    } 
    
    if (dateChooser.owner is MyDateField) 
    { 
     var dateField:MyDateField=dateChooser.owner as MyDateField; 
     dateField.labelFunction=formatDateTemp; 
    } 
   } 
  } 
  
  //日期顯示格式  
  private function formatDateTemp(date:Date):String 
  { 
   if (date == null) 
   { 
    date=new Date(); 
   } 
   
   date.hours=(Number)(nmsHour.selectedItem); 
   date.minutes=(Number)(nmsMinute.selectedItem); 
   date.seconds=(Number)(nmsSecond.selectedItem); 
   
   var df:DateFormatter=new DateFormatter(); 
   df.formatString="YYYY-MM-DD JJ:NN:SS"; 
   
   var times:String=df.format(date); 
   
   return times; 
  }
  
  private function setNowDate(event:Event):void{
   var dateChooser:MyDateChooser=this.parent as MyDateChooser; 
   var dateField:MyDateField=dateChooser.owner as MyDateField;
   var dateNow:Date = new Date();
   dateField.selectedDate = dateNow;
   dateField.selectedDate.hours = dateNow.hours;
   dateField.selectedDate.minutes = dateNow.minutes;
   dateField.selectedDate.seconds = dateNow.seconds;
   
   nmsHour.selectedIndex = dateNow.hours;
   nmsMinute.selectedIndex = dateNow.minutes;
   nmsSecond.selectedIndex = dateNow.seconds;
   dateField.text = dateField.dateToString(dateField.selectedDate, "YYYY-MM-DD JJ:NN:SS");
   dateField.closeDateField();
  }
 }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章