Hi, all.
I'm extending TextArea to make it show default text if it's empty. It will
be cleared (if needed) on focusIn.
The problem I've faced is that when I set text property in commitProperties
method that text isn't shown in the TextArea itself. :\
Following is my class:
package {
import flash.display.DisplayObject;
import flash.events.FocusEvent;
import mx.controls.TextArea;
public class ExtendedTextArea extends TextArea {
public function get defaultText() : String {
return _defaultText;
}
public function set defaultText(value : String) : void {
_defaultText = value;
_defaultTextChanged = true;
invalidateProperties();
}
public function get defaultTextFunctional() : Boolean {
return _defaultTextFunctional;
}
public function set defaultTextFunctional(value : Boolean) : void {
_defaultTextFunctional = value;
}
override public function get text() : String {
if (_showingDefaultText) {
return "";
} else {
return super.text;
}
}
override public function set text(value : String) : void {
super.text = value;
_textChanged = true;
invalidateProperties();
}
override protected function commitProperties() : void {
super.commitProperties();
if ((_textChanged || _defaultTextChanged) && text == "") {
setText(_defaultText, true);
}
_textChanged = false;
_defaultTextChanged = false;
}
override protected function focusInHandler(event : FocusEvent) :
void {
super.focusInHandler(event);
if (contains(DisplayObject(event.target)) && _showingDefaultText
&& !_defaultTextFunctional) {
setText("");
}
}
override protected function focusOutHandler(event : FocusEvent) :
void {
super.focusOutHandler(event);
if (contains(DisplayObject(event.target)) &&
!_defaultTextFunctional && text == "") {
setText(_defaultText, true);
}
}
private function setText(value : String, isDefaultText : Boolean =
false) : void {
text = value;
_showingDefaultText = isDefaultText;
}
private var _defaultText : String = "";
private var _defaultTextChanged : Boolean;
private var _textChanged : Boolean;
private var _showingDefaultText : Boolean;
private var _defaultTextFunctional : Boolean;
}
}
The class is far from complete.
Here's sample application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<ExtendedTextArea
width="220"
height="80"
defaultText="Album description goes here..."
/>
<mx:Button label="button" />
</mx:Application>
Can anyone explain me this behavior and/or where is my mistake?
Thanks,
R.