13
13
*/
14
14
package org .apache .pivot .tests ;
15
15
16
+ // import java.text.DecimalFormat;
17
+ import java .math .BigDecimal ;
18
+ import java .text .NumberFormat ;
19
+ import java .util .Locale ;
20
+
16
21
import org .apache .pivot .beans .BXMLSerializer ;
17
22
import org .apache .pivot .collections .Map ;
18
23
import org .apache .pivot .wtk .Application ;
23
28
import org .apache .pivot .wtk .TextInput ;
24
29
import org .apache .pivot .wtk .TextInputListener ;
25
30
import org .apache .pivot .wtk .Window ;
31
+ import org .apache .pivot .wtk .validation .BigDecimalValidator ;
32
+ import org .apache .pivot .wtk .validation .ComparableRangeValidator ;
26
33
import org .apache .pivot .wtk .validation .DoubleValidator ;
27
- import org .apache .pivot .wtk .validation .FloatValidator ;
34
+ import org .apache .pivot .wtk .validation .EmptyTextValidator ;
28
35
import org .apache .pivot .wtk .validation .FloatRangeValidator ;
36
+ import org .apache .pivot .wtk .validation .FloatValidator ;
29
37
import org .apache .pivot .wtk .validation .IntRangeValidator ;
30
38
import org .apache .pivot .wtk .validation .NotEmptyTextValidator ;
31
39
import org .apache .pivot .wtk .validation .RegexTextValidator ;
32
40
import org .apache .pivot .wtk .validation .Validator ;
41
+ // import java.util.Formatter;
33
42
34
43
/**
35
44
* Text input validator test.
36
45
*/
37
46
public class TextInputValidatorTest extends Application .Adapter {
47
+ private Locale locale = Locale .getDefault (); // the default locale
48
+
38
49
private Window window = null ;
50
+ private TextInput textinputLocale = null ;
51
+ private TextInput textinputComparableBigDecimal = null ;
52
+ private TextInput textinputComparableRange = null ;
53
+ private Label invalidComparableRangeLabel = null ;
39
54
private TextInput textinputDouble = null ;
40
55
private TextInput textinputFloat = null ;
41
56
private TextInput textinputFloatRange = null ;
@@ -44,30 +59,89 @@ public class TextInputValidatorTest extends Application.Adapter {
44
59
private TextInput textinputDateRegex = null ;
45
60
private TextInput textinputCustomBoolean = null ;
46
61
private TextInput textinputNotEmptyText = null ;
62
+ private TextInput textinputEmptyText = null ;
47
63
48
64
@ Override
49
65
public void startup (Display display , Map <String , String > properties ) throws Exception {
66
+ System .out .println ("Starting TextInputValidatorTest ..." );
67
+ System .out .println ("current Locale is " + locale );
68
+
69
+ // sample different ways to format numbers in i18n compatible way
70
+ NumberFormat nf = NumberFormat .getInstance ();
71
+ //
72
+ // String customDecimalPattern = ""###,###.###"";
73
+ // DecimalFormat df = new DecimalFormat(customDecimalPattern);
74
+ //
75
+ // StringBuffer sb = new StringBuffer();
76
+ // Formatter formatter = new Formatter(sb, locale);
77
+ // String customDecimalFormat = "%,.3f";
78
+ //
79
+
50
80
BXMLSerializer bxmlSerializer = new BXMLSerializer ();
51
81
window = new Window ((Component )bxmlSerializer .readObject (
52
82
getClass ().getResource ("text_input_validator_test.bxml" )));
53
83
84
+ textinputLocale = (TextInput )bxmlSerializer .getNamespace ().get ("textinputLocale" );
85
+
86
+ textinputComparableBigDecimal = (TextInput )bxmlSerializer .getNamespace ().get ("textinputComparableBigDecimal" );
87
+ textinputComparableRange = (TextInput )bxmlSerializer .getNamespace ().get ("textinputComparableRange" );
54
88
textinputDouble = (TextInput )bxmlSerializer .getNamespace ().get ("textinputDouble" );
55
89
textinputFloat = (TextInput )bxmlSerializer .getNamespace ().get ("textinputFloat" );
56
90
textinputFloatRange = (TextInput )bxmlSerializer .getNamespace ().get ("textinputFloatRange" );
57
91
textinputIntRange = (TextInput )bxmlSerializer .getNamespace ().get ("textinputIntRange" );
58
92
textinputDateRegex = (TextInput )bxmlSerializer .getNamespace ().get ("textinputDateRegex" );
59
93
textinputCustomBoolean = (TextInput )bxmlSerializer .getNamespace ().get ("textinputCustomBoolean" );
60
94
textinputNotEmptyText = (TextInput )bxmlSerializer .getNamespace ().get ("textinputNotEmptyText" );
95
+ textinputEmptyText = (TextInput )bxmlSerializer .getNamespace ().get ("textinputEmptyText" );
96
+
97
+ textinputLocale .setText (locale .toString ());
98
+
99
+ String testValue = "123456789.0" ;
100
+ // new, validate a value but using BigDecimalValidator (subclass of ComparableValidator)
101
+ textinputComparableBigDecimal .setText ("1e300" ); // huge value, and outside double range ...
102
+ BigDecimalValidator bdComp = new BigDecimalValidator ();
103
+ System .out .println ("BigDecimalValidator: created instance with value: " + bdComp );
104
+ bdComp .setAutoTrim (true ); // enable auto-trim of input string, before validating
105
+ System .out .println ("BigDecimalValidator: enable auto-trim of input string, before validating" );
106
+ textinputComparableBigDecimal .setValidator (bdComp );
107
+
108
+ // new, validate in a range but using ComparableRangeValidator
109
+ textinputComparableRange .setText (nf .format (new BigDecimal (testValue )));
110
+ ComparableRangeValidator <BigDecimal > bdCompRange = new ComparableRangeValidator <BigDecimal >(
111
+ new BigDecimal ("2.0" ), new BigDecimal ("123456789" )
112
+ );
113
+ System .out .println ("ComparableRangeValidator: created instance with value: " + bdCompRange );
114
+ bdCompRange .setAutoTrim (true ); // enable auto-trim of input string, before validating
115
+ System .out .println ("ComparableRangeValidator: enable auto-trim of input string, before validating" );
116
+ textinputComparableRange .setValidator (bdCompRange );
117
+ textinputComparableRange .getTextInputListeners ().add (new TextInputListener .Adapter () {
118
+ @ Override
119
+ public void textValidChanged (TextInput textInput ) {
120
+ invalidComparableRangeLabel .setText (textInput .isTextValid () ? "valid" : "invalid" );
121
+ }
122
+ });
123
+ invalidComparableRangeLabel = (Label )bxmlSerializer .getNamespace ().get ("invalidComparableRangeLabel" );
61
124
62
- textinputDouble .setText ("\u221E " );
125
+ textinputDouble .setText ("\u221E " ); // infinite symbol
63
126
textinputDouble .setValidator (new DoubleValidator ());
64
127
65
- textinputFloat .setText ("1.5" );
128
+ // textinputFloat.setText("123456.789");
129
+ // new, show different ways to format decimal values in i18n format
130
+ Double value = new Double (testValue );
131
+ // textinputFloat.setText(value.toString());
132
+ // textinputFloat.setText(String.format(customDecimalFormat, value)); // sample using String.format
133
+ // formatter.format(customDecimalFormat, value); // sample using Formatter
134
+ // textinputFloat.setText(sb.toString()); // sample using Formatter
135
+ // textinputFloat.setText(nf.format(value)); // sample using NumberFormat
136
+ // textinputFloat.setText(df.format(value)); // sample using DecimalFormat
137
+ textinputFloat .setText (nf .format (value )); // using this as a sample
66
138
textinputFloat .setValidator (new FloatValidator ());
67
139
68
140
// standard float range model
69
- textinputFloatRange .setText ("0.5" );
70
- textinputFloatRange .setValidator (new FloatRangeValidator (0.3f , 2000f ));
141
+ // note that float approximations could give errors,
142
+ // try to increment/decrement the initial value near a range end, to see problems ...
143
+ textinputFloatRange .setText (nf .format (new Float (testValue )));
144
+ textinputFloatRange .setValidator (new FloatRangeValidator (2.0f , 123456789f ));
71
145
72
146
// test the listener by updating a label
73
147
textinputFloatRange .getTextInputListeners ().add (new TextInputListener .Adapter () {
@@ -101,6 +175,10 @@ public boolean isValid(String s) {
101
175
textinputNotEmptyText .setText (" Not Empty, and with spaces " );
102
176
textinputNotEmptyText .setValidator (new NotEmptyTextValidator ());
103
177
178
+ // validate any empty text, edge case
179
+ textinputEmptyText .setText (" " );
180
+ textinputEmptyText .setValidator (new EmptyTextValidator ());
181
+
104
182
105
183
window .setTitle ("Text Input Validator Test" );
106
184
window .setMaximized (true );
0 commit comments