/** * @class CustomDateChooser * @brief DateChooser * 주말 색 변경, 특정 날짜 색 변경, 한영 표시방법 변경 * @author 장연태 * @version 1.0 */ package AC { import flash.events.Event; import flash.geom.Rectangle; import flash.text.TextLineMetrics; import mx.controls.CalendarLayout; import mx.controls.DateChooser; import mx.core.UITextField; import mx.events.FlexEvent; import mx.styles.CSSStyleDeclaration; import mx.styles.StyleManager; /** * The default value is 0xFF0000. */ [Style(name="sundayTextColor", type="uint", format="Color", inherit="no")] /** * The default value is 0x0000FF. */ [Style(name="saturdayTextColor", type="uint", format="Color", inherit="no")] public class CustomDateChooser extends DateChooser { private static var classConstructed : Boolean = classConstruct(); private static function classConstruct() : Boolean { if ( !StyleManager.getStyleDeclaration( "CustomDateChooser" ) ){ var newStyleDeclaration : CSSStyleDeclaration = new CSSStyleDeclaration(); newStyleDeclaration.setStyle( "sundayTextColor", 0xFF0000 ); newStyleDeclaration.setStyle( "saturdayTextColor", 0x0000FF ); StyleManager.setStyleDeclaration( "CustomDateChooser", newStyleDeclaration, true ); } return true; } public function CustomDateChooser() { super(); this.addEventListener(FlexEvent.UPDATE_COMPLETE, updateHandler); } /** * @public * 한글여부, isKorean set, get 함수 * @param Boolean * @return Boolean * */ [Bindable] private var _isKorean : Boolean = true; public function set isKorean( value : Boolean ) : void { this._isKorean = value; this.setLanguage(); } public function get isKorean() : Boolean { return this._isKorean; } /** * @public * 특정날짜 색 다르게 표현, specialDates set, get 함수 * @param Array * @return Array * */ [Bindable] private var _specialDates : Array; public function set specialDates( value : Array ) : void { this._specialDates = value; setSpecialDates(); } public function get specialDates() : Array { return this._specialDates; } /** * @public * 특정 날짜의 색 지정, specialDatesColor set, get 함수 * @param String * @return String * */ [Bindable] private var _specialDatesColor : String; public function set specialDatesColor( value : String ) : void { if ( this._specialDatesColor != value ) { this._specialDatesColor = value; var newStyleDeclaration1 : CSSStyleDeclaration = new CSSStyleDeclaration(); newStyleDeclaration1.setStyle( "sundayTextColor", getStyle( "sundayTextColor" ) ); newStyleDeclaration1.setStyle( "saturdayTextColor", getStyle( "saturdayTextColor" ) ); newStyleDeclaration1.setStyle( "specialDatesColor", value ); StyleManager.setStyleDeclaration( "CustomDateChooser", newStyleDeclaration1, true ); } } public function get specialDatesColor() : String { return this._specialDatesColor; } /** * @public * 날짜 구분자, specialSplite set, get 함수 * @param String * @return String * */ [Bindable] private var _specialSplite : String = "-"; public function set specialSplite( value : String ) : void { this._specialSplite = value; setSpecialDates(); } public function get specialSplite() : String { return this._specialSplite; } /** * @public * CalendarLayout 가져오기 함수 * @return CalendarLayout * */ public function getCalendarLayout() : CalendarLayout { var calendarLayout : CalendarLayout; var n : int = this.numChildren; for ( var i : int = 0; i < n; i++ ) { if ( this.getChildAt(i) as CalendarLayout ) { calendarLayout = this.getChildAt(i) as CalendarLayout; break; } } return calendarLayout; } private function updateHandler( e : flash.events.Event ) : void { var oldValue : String; var textField : UITextField; var calendarLayout : CalendarLayout = getCalendarLayout(); var n : int = calendarLayout.numChildren; var s : String = ""; for ( var i : int = 0; i < n; i++ ) { textField = ( calendarLayout.getChildAt(i) as UITextField ); if ( textField ) { oldValue = textField.text; if ( textField.enabled ) { if ( Number( oldValue ) ) { if( isSunday( this.displayedYear, this.displayedMonth, Number(oldValue) ) ) { textField.text = ""; textField.textColor = getStyle( "sundayTextColor" ); textField.text = oldValue; } if( isSaturday( this.displayedYear, this.displayedMonth, Number(oldValue) ) ) { textField.text = ""; textField.textColor = getStyle( "saturdayTextColor" ); textField.text = oldValue; } s = this.displayedYear.toString() + this._specialSplite + ZeroAdd( this.displayedMonth + 1 ) + this._specialSplite + ZeroAdd( Number( oldValue ) ); if( isSpecialday( s, this._specialDates ) ) { textField.text = ""; textField.textColor = getStyle( "specialDatesColor" ); textField.text = oldValue; } } } } } } override public function styleChanged( styleProp : String ) : void { super.styleChanged( styleProp ); if ( styleProp == null || styleProp == "sundayTextColor" ) { invalidateDisplayList(); } } /** * @private * 요일 반환 * @param Number, Number, Number * @return Number * */ private function getDay( year : Number, month : Number, date : Number ) : Number { return new Date( year, month, date ).day; } /** * @private * 일요일 인지 확인 * @param Number, Number, Number * @return Boolean * */ private function isSunday( year : Number, month : Number, date : Number ) : Boolean { return getDay( year, month, date ) == 0 ? true : false; } /** * @private * 토요일 인지 확인 * @param Number, Number, Number * @return Boolean * */ private function isSaturday( year : Number, month : Number, date : Number ) : Boolean { return getDay( year, month, date ) == 6 ? true : false; } /** * @private * 특정날짜 인지 확인 * @param Number, Number, Number * @return Boolean * */ private function isSpecialday( d1 : String, d2 : Array ) : Boolean { if( d2 == null ) return false; return ( d2.indexOf( d1 ) == -1 ) ? false : true; } /** * @private * 특정날짜 색 변경 함수 * @return void * */ private function setSpecialDates() : void { var oldValue : String; var textField : UITextField; var calendarLayout : CalendarLayout = getCalendarLayout(); if ( calendarLayout == null ) return; var n : int = calendarLayout.numChildren; var s : String; for ( var i : int = 0; i < n; i++ ) { textField = ( calendarLayout.getChildAt(i) as UITextField ); if ( textField ) { oldValue = textField.text; if ( textField.enabled ) { if ( Number( oldValue ) ) { s = this.displayedYear.toString() + this._specialSplite + ZeroAdd( this.displayedMonth + 1 ) + this._specialSplite + ZeroAdd( Number( oldValue ) ); if( isSpecialday( s, this._specialDates ) ) { textField.text = ""; textField.textColor = getStyle( "specialDatesColor" ); textField.text = oldValue; } } } } } } /** * @private * 숫자가 10보다 작을때 에 0추가 함수 * @param Number * @return String * */ private function ZeroAdd( value : Number ) : String { if ( isNaN( value ) == true ) return ""; if ( value > 9 ) return value.toString(); else if ( value == 0 ) return "00"; else return "0" + value.toString(); } /** * @private * 한글이면 한글 문자열로 보여줌 * @return void * */ private function setLanguage() : void { if( _isKorean ) { this.yearSymbol = "년"; this.monthSymbol = "월"; this.dayNames = new Array( "일", "월", "화", "수", "목", "금", "토" ); this.monthNames = new Array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ); } else { this.yearSymbol = ""; this.monthSymbol = ""; this.dayNames = new Array( "S", "M", "T", "W", "T", "F", "S" ); this.monthNames = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ); } } override protected function createChildren():void { super.createChildren(); this.setLanguage(); } override protected function updateDisplayList( unscaledWidth : Number, unscaledHeight : Number ) : void { super.updateDisplayList( unscaledWidth, unscaledHeight ); if ( isKorean ) { var txiYear : UITextField = UITextField( this.getChildAt(5) ); //년 var txiMonth : UITextField = UITextField( this.getChildAt(4) ); //월 var xYear : Number = txiYear.x; var xMonth : Number = txiMonth.x; var widthYear : Number = txiYear.width; var widthMonth : Number = txiMonth.width; var borderThickness : Number = getStyle( "borderThickness" ); var t : String = txiMonth.text; t = getMinimumText( t ); var textFieldBounds : Rectangle = measureTextFieldBounds(t); widthMonth = textFieldBounds.width; txiMonth.setActualSize( textFieldBounds.width, txiMonth.height ); txiYear.x = ( this.width / 2 ) - ( ( (widthYear + widthMonth) / 2 ) ) ; txiMonth.x = ( txiYear.x - 5 ) + txiYear.width; } } /** * @private * 최소 문자열 리턴 * @param String * @return String * */ private function getMinimumText( t : String ) : String { if ( !t || t.length < 2 ) t = "Wj"; return t; } /** * @private * 문자열의 사각형 영역 리턴 * @param String * @return Rectangle * */ private function measureTextFieldBounds( s : String ) : Rectangle { var lineMetrics : TextLineMetrics = measureText( s ); return new Rectangle( 0, 0, lineMetrics.width + 10, lineMetrics.height ); } } } // ActionScript file