/
PromptingTextInput.as
264 lines (226 loc) · 7.96 KB
/
PromptingTextInput.as
1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright (c) 2007 FlexLib Contributors. See:
http://code.google.com/p/flexlib/wiki/ProjectContributors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package org.flexunit.flexui.controls
{
import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextField;
import mx.controls.TextInput;
import mx.events.FlexEvent;
/**
* The <code>PromptingTextInput</code> component is a small enhancement to
* standard <code>TextInput</code>. It adds the ability to specify a prompt
* value that displays when the text is empty, similar to how the prompt
* property of the <code>ComboBox</code> behaves when there is no selected value.
*/
public class PromptingTextInput extends TextInput
{
/** Flag to indicate if the text is empty or not */
private var _textEmpty:Boolean;
/**
* Flag to prevent us from re-inserting the prompt if the text is cleared
* while the component still has focus.
*/
private var _currentlyFocused:Boolean = false;
/**
* Constructor
*/
public function PromptingTextInput()
{
_textEmpty = true;
addEventListener( Event.CHANGE, handleChange );
addEventListener( FocusEvent.FOCUS_IN, handleFocusIn );
addEventListener( FocusEvent.FOCUS_OUT, handleFocusOut );
}
// ==============================================================
// prompt
// ==============================================================
/** Storage for the prompt property */
private var _prompt:String = "";
/**
* The string to use as the prompt value
*/
public function get prompt():String
{
return _prompt;
}
[Bindable]
public function set prompt( value:String ):void
{
_prompt = value;
invalidateProperties();
}
// ==============================================================
// promptFormat
// ==============================================================
/** Storage for the promptFormat property */
private var _promptFormat:String = '<font color="#999999"><i>[prompt]</i></font>';
/**
* A format string to specify how the prompt is displayed. This is typically
* an HTML string that can set the font color and style. Use <code>[prompt]</code>
* within the string as a replacement token that will be replaced with the actual
* prompt text.
*
* The default value is "<font color="#999999"><i>[prompt]</i></font>"
*/
public function get promptFormat():String
{
return _promptFormat;
}
public function set promptFormat( value:String ):void
{
_promptFormat = value;
// Check to see if the replacement code is found in the new format string
if ( _promptFormat.indexOf( "[prompt]" ) < 0 )
{
// TODO: Log error with the logging framework, or just use trace?
//trace( "PromptingTextInput warning: prompt format does not contain [prompt] replacement code." );
}
invalidateDisplayList();
}
// ==============================================================
// text
// ==============================================================
/**
* Override the behavior of text so that it doesn't take into account
* the prompt. If the prompt is displaying, the text is just an empty
* string.
*/
[Bindable("textChanged")]
[CollapseWhiteSpace]
[NonCommittingChangeEvent("change")]
override public function set text( value:String ):void
{
// changed the test to also test for null values, not just 0 length
// if we were passed undefined or null then the zero length test would
// still return false. - Doug McCune
_textEmpty = (!value) || value.length == 0;
super.text = value;
invalidateDisplayList();
}
override public function get text():String
{
// If the text has changed
if ( _textEmpty )
{
// Skip the prompt text value
return "";
}
else
{
return super.text;
}
}
/**
* We store a local copy of displayAsPassword. We need to keep this so that we can
* change it to false if we're showing the prompt. Then we change it back (if it was
* set to true) once we're no longer showing the prompt.
*/
private var _displayAsPassword:Boolean = false;
override public function set displayAsPassword(value:Boolean):void {
_displayAsPassword = value;
super.displayAsPassword = value;
}
override public function get displayAsPassword():Boolean {
return _displayAsPassword;
}
// ==============================================================
// overriden methods
// ==============================================================
/**
* @private
*
* Determines if the prompt needs to be displayed.
*/
override protected function updateDisplayList( unscaledWidth:Number, unscaledHeight:Number ):void
{
// If the text is empty and a prompt value is set and the
// component does not currently have focus, then the component
// needs to display the prompt
if ( _textEmpty && _prompt != "" && !_currentlyFocused )
{
if ( _promptFormat == "" )
{
super.text = _prompt;
}
else
{
super.htmlText = _promptFormat.replace( /\[prompt\]/g, _prompt );
}
if ( super.displayAsPassword )
{
//If we're showing the prompt and we have displayAsPassword set then
//we need to set it to false while the prompt is showing.
var oldVal:Boolean = _displayAsPassword;
super.displayAsPassword = false;
_displayAsPassword = oldVal;
}
}
else
{
if ( super.displayAsPassword != _displayAsPassword )
{
super.displayAsPassword = _displayAsPassword;
}
}
super.updateDisplayList( unscaledWidth, unscaledHeight );
}
// ==============================================================
// event handlers
// ==============================================================
/**
* @private
*/
protected function handleChange( event:Event ):void
{
_textEmpty = super.text.length == 0;
}
/**
* @private
*
* When the component recevies focus, check to see if the prompt
* needs to be cleared or not.
*/
protected function handleFocusIn( event:FocusEvent ):void
{
_currentlyFocused = true;
// If the text is empty, clear the prompt
if ( _textEmpty )
{
super.htmlText = "";
// KLUDGE: Have to validate now to avoid a bug where the format
// gets "stuck" even though the text gets cleared.
validateNow();
}
}
/**
* @private
*
* When the component loses focus, check to see if the prompt needs
* to be displayed or not.
*/
protected function handleFocusOut( event:FocusEvent ):void
{
_currentlyFocused = false;
// If the text is empty, put the prompt back
invalidateDisplayList();
}
} // end class
} // en package