flex 日期组件重写, 添加清空等

package hxht.comps.datefiled
{
    import flash.events.Event;
    import flash.events.FocusEvent;
    import flash.events.MouseEvent;
   
    import mx.controls.DateField;
    import mx.core.UIComponent;
    import mx.core.mx_internal;
    import mx.events.CalendarLayoutChangeEvent;
    import mx.events.DropdownEvent;
   
    import spark.components.Button;

    use namespace mx_internal;

    [Style(name="borderColor", type="uint", format="Color")]
    [Style(name="backGroundColor", type="uint", format="Color")]
    [Style(name="borderAlpha", type="Number")]
    [Style(name="borderWeight", type="Number")]
    [Style(name="cornerRadius", type="Number")]
    [Style(name="iconPaddingRight", type="Number")]

    /**
     *
     * @author Administrator
     */
    public class DateFiledLocal extends DateField
    {
        public function DateFiledLocal()
        {
            super();
   dayNames=['日','一','二','三','四','五','六'];
   monthNames=['一','二','三','四','五','六','七','八','九','十','十一','十二'];
   formatString="YYYY-MM-DD";
        }

        /**
         *  默认遮罩是否存在
         */
        public var mouseMaskEnabel:Boolean = true;

        //back
        protected var _backBorder:UIComponent;

        //border
        protected var _borderUI:UIComponent;

        //mask
        protected var _mouseMask:UIComponent;
  
  protected var _topMask:UIComponent ;
  
  /**
   * 是否显示删除按钮
   */  
  public var showDelete:Boolean = false ;
  
  /**
   * 删除按钮间距
   */  
  public var deletePadding:Number = 3 ;
  
  /**
   * 是否显示toolpit
   */  
  public var showtip:Boolean = true ;
  
  /**
   * 临时删除按钮 后 统一成一遍的编辑输入 和删除 需要修改 textinput默认输入行为 不然会有问题
   */  
  public var temp:Button = null ;

        override protected function createChildren():void
        {
            _backBorder = new UIComponent();
            _mouseMask = new UIComponent();
            _borderUI = new UIComponent();
            _mouseMask.alpha = 1;
            this.addChildAt(_backBorder, 0);
            this.addChildAt(_borderUI, 1);
            this.addChildAt(_mouseMask, 2);
            super.createChildren();
            this.addEventListener(MouseEvent.ROLL_OVER, sparkskin1_mouseOverHandler);
            this.addEventListener(MouseEvent.ROLL_OUT, sparkskin1_mouseOutHandler);
            this.addEventListener(DropdownEvent.OPEN, openHandler);
            this.addEventListener(DropdownEvent.CLOSE, closeHandler);
   
   if(showDelete)
   {
    temp = new Button() ;
    temp.buttonMode = true ;
    temp.width = 17;
    temp.height = 17 ;
    temp.addEventListener(MouseEvent.CLICK,deleteHandler) ;
    temp.toolTip = "清空" ;
    temp.styleName = "deleteForm_sys" ;
    this.addChild(temp) ;
   }
   
   this.textInput.setStyle("borderStyle", "none");
   this.textInput.setStyle("borderAlpha", 0);
   this.textInput.setStyle("contentBackgroundAlpha", 0);
   if (textInput)
   {
    textInput.addEventListener(FocusEvent.FOCUS_OUT, focuseOutHandler);
    textInput.addEventListener(FocusEvent.FOCUS_IN, focuseInHandler);
    textInput.addEventListener(Event.CHANGE,textChangeHandler ) ;
   }
        }
  
  /**
   * 
   * @param e
   *
   */  
  protected function deleteHandler( e:Event ):void
  {
   if(selectedDate)
     selectedDate = null ;
   
   this.dispatchEvent(new CalendarLayoutChangeEvent(CalendarLayoutChangeEvent.CHANGE,false,false,null)) ;
   
   e.stopPropagation() ;
   e.preventDefault();
  }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            this.downArrowButton.buttonMode = true;
            this.downArrowButton.useHandCursor = true;
            this.downArrowButton.mouseChildren = false;
         
   var _w:Number = unscaledWidth ;
   var _h:Number = unscaledHeight ;
   if(showDelete)
   {
    _w -= (temp.width +deletePadding);
    temp.x = _w + deletePadding;
    temp.y = (_h-temp.height)/2 ;
   }
   
   
            var _bordercolor:Object = this.getStyle("borderColor");
            var _borderweight:Object = this.getStyle("borderWeight");
            var _borderAlpha:Number = this.getStyle("borderAlpha");
            var _bgColor:Object = this.getStyle("backGroundColor");
            var _bgAlpha:Object = this.getStyle("backGroundAlpha");
            var _iconRight:Number = this.getStyle("iconPaddingRight");
            var _focusC:Object = this.getStyle("focusColor");
            var _focusA:uint = this.getStyle("errorColor");
   var _cor:Number = this.getStyle("cornerRadius") ;
            if (_bordercolor == null)
                _bordercolor = 0x1c75c3;
            if (_borderweight == null)
                _borderweight = 1;
            if (isNaN(_borderAlpha))
                _borderAlpha = 1;
            if (_bgColor == null)
                _bgColor = 0xffffff;
            if (_bgAlpha == null)
                _bgAlpha = 1;
            if (isNaN(_iconRight))
                _iconRight = 1;
            if (_focusC == null)
                _focusC = 0xeaf2fb;
   if(isNaN(_cor))
    _cor = 0 ;

            with (_backBorder.graphics)
            {
                clear();
                beginFill(_focusC, _bgAlpha);
                drawRoundRect(0, 0,_w, _h , 0);
                endFill();
            }

            with (_borderUI.graphics)
            {
                clear();
                lineStyle(_borderweight, _bordercolor, _borderAlpha);
                drawRoundRect(0, 0,_w, _h , 0);
                endFill();
            }

            /*mask*/
            mouseMaskEnabel = (_focusA == 0xff1000 ? false : true);
            _mouseMask.alpha = 1;
            _mouseMask.x = Number(_borderweight);
            _mouseMask.y = Number(_borderweight);
            _mouseMask.width = (_w - Number(_borderweight));
            _mouseMask.height = (_h - Number(_borderweight));
            with (_mouseMask.graphics)
            {
                clear();
                beginFill(_bgColor, 1);
                drawRoundRect(0, 0, _mouseMask.width, _mouseMask.height,0 );
                endFill();
            }

            super.updateDisplayList(unscaledWidth, unscaledHeight);
            downArrowButton.x = (_w - downArrowButton.width - _iconRight);
            downArrowButton.y = (_h - downArrowButton.height) / 2;
   
   if (textInput && showDelete)
    textInput.width =  downArrowButton.x  ;
   if(showtip)
    this.toolTip = textInput.text ;
            sparkskin1_mouseOutHandler();
        }


        //-----------------------------------------------------style---------------------------------------------------
        /**
         * focus in flag
         */
        private var _textIn:Boolean = false;

        /**
         * mouse in this bound
         */
        private var _mouseIn:Boolean = false;

        /**
         * is openning state
         */
        private var _openFlag:Boolean = false;

        /**
         * focuse in handler
         * @param e
         *
         */
        protected function focuseInHandler(e:Event):void
        {
            _textIn = true;
            sparkskin1_mouseOutHandler();
        }

        /**
         * focuse out handler
         * @param e
         *
         */
        protected function focuseOutHandler(e:Event):void
        {
            _textIn = false;
            sparkskin1_mouseOutHandler();
        }

        /**
         * mouse over handler hidden mask
         * @param event
         *
         */
        protected function sparkskin1_mouseOverHandler(event:MouseEvent):void
        {
            mouseIn = true;
        }

        /**
         * mouse out handler show mask
         * @param event
         *
         */
        protected function sparkskin1_mouseOutHandler(event:MouseEvent = null):void
        {
            if (!editable)
                _textIn = false;
            if ((mouseX < 0 || mouseY > this.height || mouseX > this.width || mouseY < 0) && !_textIn && !_openFlag)
                mouseIn = false;
            else
                mouseIn = true;
        }

        /**
         * set mouse in
         * @return
         *
         */
        public function get mouseIn():Boolean
        {
            return _mouseIn;
        }

        /**
         * set mouse out
         * @param value
         *
         */
        public function set mouseIn(value:Boolean):void
        {
            if (this.enabled)
            {
                _mouseIn = value;
                if (_mouseMask && mouseMaskEnabel)
                {
                    _mouseMask.visible = value;
                    _mouseMask.includeInLayout = value;
                }
            }
        }

        /**
         * when closed reset view if uneditabled
         * @param e
         *
         */
        protected function closeHandler(e:Event):void
        {
            _openFlag = false;
            if (!editable)
                sparkskin1_mouseOutHandler();
        }

        /**
         *
         * @param e
         *
         */
        private function openHandler(e:Event):void
        {
            _openFlag = true;
        }
  
  private function textChangeHandler( e:Event ):void
  {
   
  }

    }
}

你可能感兴趣的:(flex代码片段,日期组件的重写)