Uma dica, aqui nós formatamos o texto digitado, ao entrar no DateField,
retiramos as barras(/) e deixamos só os números, e ao sair, formatamos ele
com o DateFormatter e validamos com o DateValidator, setando o maxChars do
_textInput do DateField para 8 quando entra e 10 ao sair. Isso economiza o
trabalho do usuário digitar as barras. Mas que eu dei uma olhada rápida no
código, parece ser muito funcional.

Obrigado,
Felipe Bianconi

2009/9/2 thiagoalgo <[email protected]>

>
> Pessoal,
>
> Primeiramente desculpem a pretenção no título do email, mas incluí a
> palavra enviando pois se não a maioria das pessoas iria achar que
> estou solicitando um componente de máscara.
>
> Fiz um componente de data que está funcionando muito bem, porém está
> bem "engessado" aceitando apenas o formato dd/mm/yyyy. Por isso estou
> disponibilizando para uso e se alguma alma caridosa se inspirar e
> quiser melhorá-lo e talvez até dinamizar o formato da máscara o código
> está aí.
>
> Uma coisa que acho que ficou nesse componente é que você pode
> trabalhar diretamente com dados do tipo Date e não com a propriedade
> text do componente.
>
> Segue abaixo o código, se alguém quiser o arquivo posso enviar também.
> Lembro que foi feito a "marretada" então tem coisas que com certeza
> podem ser melhoradas.
>
> package {
>        import flash.events.Event;
>        import flash.events.KeyboardEvent;
>        import flash.ui.Keyboard;
>
>        import mx.controls.TextInput;
>        import mx.formatters.DateFormatter;
>
>        public class ERKDateInput extends TextInput {
>
>                private var _text:String = "";
>                private var _textFormated:String = "";
>
>                [Inspectable(defaultValue="false", enumeration="true,false",
> category="ERK")]
>                private var _keepInitial:Boolean = false; // Verifica se o
> valor
> inicial deve ser setado quando chamado o metodo reset
>
>                private var _initialValue:Date = null; // Armazena o
> primeiro valor
> setado no componente para setar novamente quando o metodo reset for
> invocado e quando _keepInitial for verdadeiro
>
>                [Inspectable(defaultValue="false", enumeration="true,false",
> category="ERK")]
>                private var _controlavel:Boolean = true;
>
>                public function ERKDateInput() {
>                        super();
>                }
>
>                override protected function childrenCreated():void {
>                        super.childrenCreated();
>                        this.width = 80;
>                        this.addEventListener(KeyboardEvent.KEY_DOWN,
> _onKeyDown);
>                        this.formatar();
>                }
>
>                private function _onKeyDown(event:KeyboardEvent):void {
>                        var ini:int = selectionBeginIndex; // inicio da
> selecao
>                        var fim:int = selectionEndIndex; // fim da selecao
>
>                        if (event.keyCode == Keyboard.BACKSPACE) {
>                                var limiteAuxIni:int = 0;
>                                var inicioAuxFim:int = 0;
>
>                                if (fim - ini > 0) {
>                                        if (ini >= 6) {
>                                                limiteAuxIni = ini - 2;
>                                        } else if (ini >= 3) {
>                                                limiteAuxIni = ini - 1;
>                                        } else {
>                                                limiteAuxIni = ini - 0;
>                                        }
>
>                                        if (fim >= 6) {
>                                                inicioAuxFim = fim - 2;
>                                        } else if (fim >= 3) {
>                                                inicioAuxFim = fim - 1;
>                                        } else {
>                                                inicioAuxFim = fim - 0;
>                                        }
>
>                                } else {
>                                        if (ini >= 6) {
>                                                limiteAuxIni = ini - 1 - 2;
>                                        } else if (ini >= 3) {
>                                                limiteAuxIni = ini - 1 - 1;
>                                        } else {
>                                                limiteAuxIni = ini - 1 - 0;
>                                        }
>
>                                        if (fim >= 6) {
>                                                inicioAuxFim = fim - 2;
>                                        } else if (fim >= 3) {
>                                                inicioAuxFim = fim - 1;
>                                        } else {
>                                                inicioAuxFim = fim - 0;
>                                        }
>                                }
>
>
>                                var auxIni:String = this._text.substring(0,
> limiteAuxIni);
>                                var auxFim:String =
> this._text.substring(inicioAuxFim,
> this._text.length);
>
>                                this._text = auxIni.concat(auxFim);
>
>                                // Definindo a posicao do cursor
>                                if (ini == 6) {
>                                        this.selectionBeginIndex = 4;
>                                        this.selectionEndIndex = 4;
>
>                                } else if (ini == 3) {
>                                        this.selectionBeginIndex = 1;
>                                        this.selectionEndIndex = 1;
>
>                                }
>
>                        } else if (event.keyCode == Keyboard.DELETE) {
>                                var limiteAuxIni:int = 0;
>                                var inicioAuxFim:int = 0;
>
>                                if (fim - ini > 0) {
>                                        if (ini >= 6) {
>                                                limiteAuxIni = ini - 2;
>                                        } else if (ini >= 3) {
>                                                limiteAuxIni = ini - 1;
>                                        } else {
>                                                limiteAuxIni = ini - 0;
>                                        }
>
>                                        if (fim >= 6) {
>                                                inicioAuxFim = fim - 2;
>                                        } else if (fim >= 3) {
>                                                inicioAuxFim = fim - 1;
>                                        } else {
>                                                inicioAuxFim = fim - 0;
>                                        }
>
>                                } else {
>                                        if (ini >= 6) {
>                                                limiteAuxIni = ini - 2;
>                                        } else if (ini >= 3) {
>                                                limiteAuxIni = ini - 1;
>                                        } else {
>                                                limiteAuxIni = ini - 0;
>                                        }
>
>                                        if (fim >= 6) {
>                                                inicioAuxFim = fim + 1 - 2;
>                                        } else if (fim >= 3) {
>                                                inicioAuxFim = fim + 1 - 1;
>                                        } else {
>                                                inicioAuxFim = fim + 1 - 0;
>                                        }
>                                }
>
>                                var auxIni:String = this._text.substring(0,
> limiteAuxIni);
>                                var auxFim:String =
> this._text.substring(inicioAuxFim,
> this._text.length);
>                                this._text = auxIni.concat(auxFim);
>
>                        } else if (event.keyCode >= Keyboard.NUMPAD_0 &&
> event.keyCode <=
> Keyboard.NUMPAD_9) {
>                                var digitado:String = null;
>                                if (ini >= 6) {
>                                        ini -= 2;
>                                } else if (ini >= 3) {
>                                        ini--;
>                                }
>
>                                if (fim >= 6) {
>                                        fim -= 2;
>                                } else if (fim >= 3) {
>                                        fim--;
>                                }
>
>                                if (event.keyCode == Keyboard.NUMPAD_0) {
> digitado = "0"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_1)
> { digitado = "1"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_2)
> { digitado = "2"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_3)
> { digitado = "3"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_4)
> { digitado = "4"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_5)
> { digitado = "5"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_6)
> { digitado = "6"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_7)
> { digitado = "7"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_8)
> { digitado = "8"; }
>                                else if (event.keyCode == Keyboard.NUMPAD_9)
> { digitado = "9"; }
>
>                                // Verificando se tem selecao e alterando o
> valor selecionado ou
>                                // se nao ha selecao e eh apenas para
> adicionar o valor digitado.
>                                if (fim - ini > 0) {
>                                        if (digitado != null) {
>                                                var auxIni:String =
> this._text.substring(0, ini);
>                                                var auxFim:String =
> this._text.substring(fim,
> this._text.length);
>                                                auxIni =
> auxIni.concat(digitado);
>                                                this._text =
> auxIni.concat(auxFim);
>                                        } else if (event.keyCode ==
> Keyboard.BACKSPACE || event.keyCode
> == Keyboard.DELETE) {
>                                                var auxIni:String =
> this._text.substring(0, ini);
>                                                var auxFim:String =
> this._text.substring(fim,
> this._text.length);
>                                                this._text =
> auxIni.concat(auxFim);
>                                        }
>                                } else {
>                                        // So sera adicionado o caracter
> digitado se nao houver valor
> apos o cursor
>                                        // ou se o texto apos ainda poder
> ser deloscado
>                                        if (this._text.charAt(ini) == null
> || this._text.charAt(ini) ==
> "" || this._text.length + 1 <= 8) {
>                                                if (digitado != null) {
>                                                        var auxIni:String =
> this._text.substring(0, ini);
>                                                        var auxFim:String =
> this._text.substring(fim,
> this._text.length);
>                                                        auxIni =
> auxIni.concat(digitado);
>                                                        this._text =
> auxIni.concat(auxFim);
>                                                }
>                                        }
>                                }
>
>                                // Definindo a posicao do cursor
>                                if (ini == 3) {
>                                        this.selectionBeginIndex = 6;
>                                        this.selectionEndIndex = 6;
>
>                                } else if (ini == 1) {
>                                        this.selectionBeginIndex = 3;
>                                        this.selectionEndIndex = 3;
>
>                                }
>
>                        }
>
>                        this._text = this._text.substr(0, 8);
>                        this.formatar();
>
>                }
>
>                private function formatar():void {
>                        var aux:String = "";
>                        var posInText:int = 0;
>                        for (var i:int = 0; i < 10; i++) {
>                                if (i == 2 || i == 5) {
>                                        aux = aux.concat("/");
>                                        posInText--;
>                                } else {
>                                        if (this._text.length >= (posInText
> + 1)) {
>                                                aux =
> aux.concat(this._text.charAt(posInText));
>                                        } else {
>                                                aux = aux.concat("_");
>                                        }
>                                }
>                                posInText++;
>                        }
>
>                        this._textFormated = aux;
>                        this.invalidateDisplayList();
>                }
>
>                override protected function
> updateDisplayList(unscaledWidth:Number,
> unscaledHeight:Number):void {
>                        super.updateDisplayList(unscaledWidth,
> unscaledHeight);
>                        this.text = this._textFormated;
>                        this.dispatchEvent(new Event("dateChange"));
>                }
>
>                public function set date(value:Date):void {
>                        if (this._keepInitial && this._initialValue == null)
> {
>                                this._initialValue = value;
>                        }
>
>                        var dateFormatter:DateFormatter = new
> DateFormatter();
>                        dateFormatter.formatString = "DDMMYYYY";
>
>                        this._text = dateFormatter.format(value);
>                        this.formatar();
>
>                }
>
>                [Bindable("dateChange")]
>                public function get date():Date {
>                        trace(">>>>>>> DateChange");
>                        trace(this._text);
>                        if (this._text.length == 8) {
>                                var dia:int = int(this._text.substr(0, 2));
>                                var mes:int = int(this._text.substr(2, 2)) -
> 1; // Mes inicia em 0
> (zero)
>                                var ano:int = int(this._text.substr(4, 4));
>                                var date:Date = new Date(ano, mes, dia);
>                                trace("Retornou: " + date);
>                                return date;
>                        } else {
>                                trace("Retornou: " + null);
>                                return null;
>                        }
>                }
>
>                public function reset():void {
>                        if (this._keepInitial) {
>                                this.date = this._initialValue;
>                        } else {
>                                this.date = null;
>                        }
>                }
>
>                public function set controlavel(value:Boolean):void {
>                        this._controlavel = value;
>                }
>
>                public function get controlavel():Boolean {
>                        return this._controlavel;
>                }
>
>                public function set keepInitial(value:Boolean):void {
>                        this._keepInitial = value;
>                }
>
>                public function get keepInitial():Boolean {
>                        return this._keepInitial;
>                }
>        }
> }
> >
>

--~--~---------~--~----~------------~-------~--~----~
Você recebeu esta mensagem porque está inscrito na lista "flexdev"
Para enviar uma mensagem, envie um e-mail para [email protected]
Para sair da lista, envie um email em branco para 
[email protected]
Mais opções estão disponíveis em http://groups.google.com/group/flexdev
-~----------~----~----~----~------~----~------~--~---

Responder a