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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.ui;
import java.text.Format;
import java.util.Map;
import com.vaadin.data.Property;
import com.vaadin.event.FieldEvents;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.gwt.client.ui.VTextField;
import com.vaadin.ui.ClientWidget.LoadStyle;
/**
* <p>
* A text editor component that can be bound to any bindable Property. The text
* editor supports both multiline and single line modes, default is one-line
* mode.
* </p>
*
* <p>
* Since <code>TextField</code> extends <code>AbstractField</code> it implements
* the {@link com.vaadin.data.Buffered} interface. A <code>TextField</code> is
* in write-through mode by default, so
* {@link com.vaadin.ui.AbstractField#setWriteThrough(boolean)} must be called
* to enable buffering.
* </p>
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
@SuppressWarnings("serial")
@ClientWidget(value = VTextField.class, loadStyle = LoadStyle.EAGER)
public class TextField extends AbstractField implements
FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
/* Private members */
/**
* Value formatter used to format the string contents.
*/
private Format format;
/**
* Number of visible columns in the TextField.
*/
private int columns = 0;
/**
* Number of visible rows in a multiline TextField. Value 0 implies a
* single-line text-editor.
*/
private int rows = 0;
/**
* Tells if word-wrapping should be used in multiline mode.
*/
private boolean wordwrap = true;
/**
* Tells if input is used to enter sensitive information that is not echoed
* to display. Typically passwords.
*/
private boolean secret = false;
/**
* Null representation.
*/
private String nullRepresentation = "null";
/**
* Is setting to null from non-null value allowed by setting with null
* representation .
*/
private boolean nullSettingAllowed = false;
private String inputPrompt = null;
/**
* Maximum character count in text field.
*/
private int maxLength = -1;
private int selectionPosition = -1;
private int selectionLength;
/* Constructors */
/**
* Constructs an empty <code>TextField</code> with no caption.
*/
public TextField() {
setValue("");
}
/**
* Constructs an empty <code>TextField</code> with given caption.
*
* @param caption
* the caption <code>String</code> for the editor.
*/
public TextField(String caption) {
setValue("");
setCaption(caption);
}
/**
* Constructs a new <code>TextField</code> that's bound to the specified
* <code>Property</code> and has no caption.
*
* @param dataSource
* the Property to be edited with this editor.
*/
public TextField(Property dataSource) {
setPropertyDataSource(dataSource);
}
/**
* Constructs a new <code>TextField</code> that's bound to the specified
* <code>Property</code> and has the given caption <code>String</code>.
*
* @param caption
* the caption <code>String</code> for the editor.
* @param dataSource
* the Property to be edited with this editor.
*/
public TextField(String caption, Property dataSource) {
this(dataSource);
setCaption(caption);
}
/**
* Constructs a new <code>TextField</code> with the given caption and
* initial text contents. The editor constructed this way will not be bound
* to a Property unless
* {@link com.vaadin.data.Property.Viewer#setPropertyDataSource(Property)}
* is called to bind it.
*
* @param caption
* the caption <code>String</code> for the editor.
* @param text
* the initial text content of the editor.
*/
public TextField(String caption, String value) {
setValue(value);
setCaption(caption);
}
/* Component basic features */
/*
* Paints this component. Don't add a JavaDoc comment here, we use the
* default documentation from implemented interface.
*/
@Override
public void paintContent(PaintTarget target) throws PaintException {
super.paintContent(target);
// Sets the secret attribute
if (isSecret()) {
target.addAttribute("secret", true);
}
if (getMaxLength() >= 0) {
target.addAttribute("maxLength", getMaxLength());
}
if (inputPrompt != null) {
target.addAttribute("prompt", inputPrompt);
}
if (selectionPosition != -1) {
target.addAttribute("selpos", selectionPosition);
target.addAttribute("sellen", selectionLength);
selectionPosition = -1;
}
// Adds the number of column and rows
final int columns = getColumns();
final int rows = getRows();
if (columns != 0) {
target.addAttribute("cols", String.valueOf(columns));
}
if (rows != 0) {
target.addAttribute("rows", String.valueOf(rows));
target.addAttribute("multiline", true);
if (!wordwrap) {
target.addAttribute("wordwrap", false);
}
}
// Adds the content as variable
String value = getFormattedValue();
if (value == null) {
value = getNullRepresentation();
}
if (value == null) {
throw new IllegalStateException(
"Null values are not allowed if the null-representation is null");
}
target.addVariable(this, "text", value);
}
/**
* Gets the formatted string value. Sets the field value by using the
* assigned Format.
*
* @return the Formatted value.
* @see #setFormat(Format)
* @see Format
* @deprecated
*/
@Deprecated
protected String getFormattedValue() {
Object v = getValue();
if (v == null) {
return null;
}
return v.toString();
}
/*
* Gets the value of the field, but uses formatter is given. Don't add a
* JavaDoc comment here, we use the default documentation from implemented
* interface.
*/
@Override
public Object getValue() {
Object v = super.getValue();
if (format == null || v == null) {
return v;
}
try {
return format.format(v);
} catch (final IllegalArgumentException e) {
return v;
}
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#changeVariables(java.lang.Object,
* java.util.Map)
*/
@Override
public void changeVariables(Object source, Map<String, Object> variables) {
super.changeVariables(source, variables);
// Sets the text
if (variables.containsKey("text") && !isReadOnly()) {
// Only do the setting if the string representation of the value
// has been updated
String newValue = (String) variables.get("text");
// server side check for max length
if (getMaxLength() != -1 && newValue.length() > getMaxLength()) {
newValue = newValue.substring(0, getMaxLength());
}
final String oldValue = getFormattedValue();
if (newValue != null
&& (oldValue == null || isNullSettingAllowed())
&& newValue.equals(getNullRepresentation())) {
newValue = null;
}
if (newValue != oldValue
&& (newValue == null || !newValue.equals(oldValue))) {
boolean wasModified = isModified();
setValue(newValue, true);
// If the modified status changes, or if we have a formatter,
// repaint is needed after all.
if (format != null || wasModified != isModified()) {
requestRepaint();
}
}
}
if (variables.containsKey(FocusEvent.EVENT_ID)) {
fireEvent(new FocusEvent(this));
}
if (variables.containsKey(BlurEvent.EVENT_ID)) {
fireEvent(new BlurEvent(this));
}
}
/* Text field configuration */
/**
* Gets the number of columns in the editor. If the number of columns is set
* 0, the actual number of displayed columns is determined implicitly by the
* adapter.
*
* @return the number of columns in the editor.
*/
public int getColumns() {
return columns;
}
/**
* Sets the number of columns in the editor. If the number of columns is set
* 0, the actual number of displayed columns is determined implicitly by the
* adapter.
*
* @param columns
* the number of columns to set.
*/
public void setColumns(int columns) {
if (columns < 0) {
columns = 0;
}
this.columns = columns;
requestRepaint();
}
/**
* Gets the number of rows in the editor. If the number of rows is set to 0,
* the actual number of displayed rows is determined implicitly by the
* adapter.
*
* @return number of explicitly set rows.
*/
public int getRows() {
return rows;
}
/**
* Sets the number of rows in the editor.
*
* @param rows
* the number of rows for this editor.
*/
public void setRows(int rows) {
if (rows < 0) {
rows = 0;
}
if (this.rows != rows) {
this.rows = rows;
requestRepaint();
}
}
/**
* Sets the height of the {@link TextField} instance.
*
* <p>
* Setting height for {@link TextField} also has a side-effect that puts
* {@link TextField} into multiline mode (aka "textarea"). Multiline mode
* can also be achieved by calling {@link #setRows(int)}. The height value
* overrides the number of rows set by {@link #setRows(int)}.
* <p>
* If you want to set height of single line {@link TextField}, call
* {@link #setRows(int)} with value 0 after setting the height. Setting rows
* to 0 resets the side-effect.
*
* @see com.vaadin.ui.AbstractComponent#setHeight(float, int)
*/
@Override
public void setHeight(float height, int unit) {
super.setHeight(height, unit);
if (height > 1) {
/*
* In html based terminals we most commonly want to make component
* to be textarea if height is defined. Setting row field above 0
* will render component as textarea.
*/
rows = 2;
}
}
/**
* Sets the height of the {@link TextField} instance.
*
* <p>
* Setting height for {@link TextField} also has a side-effect that puts
* {@link TextField} into multiline mode (aka "textarea"). Multiline mode
* can also be achieved by calling {@link #setRows(int)}. The height value
* overrides the number of rows set by {@link #setRows(int)}.
* <p>
* If you want to set height of single line {@link TextField}, call
* {@link #setRows(int)} with value 0 after setting the height. Setting rows
* to 0 resets the side-effect.
*
* @see com.vaadin.ui.AbstractComponent#setHeight(java.lang.String)
*/
@Override
public void setHeight(String height) {
// will call setHeight(float, int) the actually does the magic. Method
// is overridden just to document side-effects.
super.setHeight(height);
}
/**
* Tests if the editor is in word-wrap mode.
*
* @return <code>true</code> if the component is in the word-wrap mode,
* <code>false</code> if not.
*/
public boolean isWordwrap() {
return wordwrap;
}
/**
* Sets the editor's word-wrap mode on or off.
*
* @param wordwrap
* the boolean value specifying if the editor should be in
* word-wrap mode after the call or not.
*/
public void setWordwrap(boolean wordwrap) {
if (this.wordwrap != wordwrap) {
this.wordwrap = wordwrap;
requestRepaint();
}
}
/* Property features */
/*
* Gets the edited property's type. Don't add a JavaDoc comment here, we use
* the default documentation from implemented interface.
*/
@Override
public Class getType() {
return String.class;
}
/**
* Gets the secret property on and off. If a field is used to enter
* secretinformation the information is not echoed to display.
*
* @return <code>true</code> if the field is used to enter secret
* information, <code>false</code> otherwise.
*/
public boolean isSecret() {
return secret;
}
/**
* Sets the secret property on and off. If a field is used to enter
* secretinformation the information is not echoed to display.
*
* @param secret
* the value specifying if the field is used to enter secret
* information.
*/
public void setSecret(boolean secret) {
if (this.secret != secret) {
this.secret = secret;
requestRepaint();
}
}
/**
* Gets the null-string representation.
*
* <p>
* The null-valued strings are represented on the user interface by
* replacing the null value with this string. If the null representation is
* set null (not 'null' string), painting null value throws exception.
* </p>
*
* <p>
* The default value is string 'null'.
* </p>
*
* @return the String Textual representation for null strings.
* @see TextField#isNullSettingAllowed()
*/
public String getNullRepresentation() {
return nullRepresentation;
}
/**
* Is setting nulls with null-string representation allowed.
*
* <p>
* If this property is true, writing null-representation string to text
* field always sets the field value to real null. If this property is
* false, null setting is not made, but the null values are maintained.
* Maintenance of null-values is made by only converting the textfield
* contents to real null, if the text field matches the null-string
* representation and the current value of the field is null.
* </p>
*
* <p>
* By default this setting is false
* </p>
*
* @return boolean Should the null-string represenation be always converted
* to null-values.
* @see TextField#getNullRepresentation()
*/
public boolean isNullSettingAllowed() {
return nullSettingAllowed;
}
/**
* Sets the null-string representation.
*
* <p>
* The null-valued strings are represented on the user interface by
* replacing the null value with this string. If the null representation is
* set null (not 'null' string), painting null value throws exception.
* </p>
*
* <p>
* The default value is string 'null'
* </p>
*
* @param nullRepresentation
* Textual representation for null strings.
* @see TextField#setNullSettingAllowed(boolean)
*/
public void setNullRepresentation(String nullRepresentation) {
this.nullRepresentation = nullRepresentation;
}
/**
* Sets the null conversion mode.
*
* <p>
* If this property is true, writing null-representation string to text
* field always sets the field value to real null. If this property is
* false, null setting is not made, but the null values are maintained.
* Maintenance of null-values is made by only converting the textfield
* contents to real null, if the text field matches the null-string
* representation and the current value of the field is null.
* </p>
*
* <p>
* By default this setting is false.
* </p>
*
* @param nullSettingAllowed
* Should the null-string represenation be always converted to
* null-values.
* @see TextField#getNullRepresentation()
*/
public void setNullSettingAllowed(boolean nullSettingAllowed) {
this.nullSettingAllowed = nullSettingAllowed;
}
/**
* Gets the current input prompt.
*
* @see #setInputPrompt(String)
* @return the current input prompt, or null if not enabled
*/
public String getInputPrompt() {
return inputPrompt;
}
/**
* Sets the input prompt - a textual prompt that is displayed when the field
* would otherwise be empty, to prompt the user for input.
*
* @param inputPrompt
*/
public void setInputPrompt(String inputPrompt) {
this.inputPrompt = inputPrompt;
requestRepaint();
}
/**
* Gets the value formatter of TextField.
*
* @return the Format used to format the value.
* @deprecated replaced by {@link com.vaadin.data.util.PropertyFormatter}
*/
@Deprecated
public Format getFormat() {
return format;
}
/**
* Gets the value formatter of TextField.
*
* @param format
* the Format used to format the value. Null disables the
* formatting.
* @deprecated replaced by {@link com.vaadin.data.util.PropertyFormatter}
*/
@Deprecated
public void setFormat(Format format) {
this.format = format;
requestRepaint();
}
@Override
protected boolean isEmpty() {
return super.isEmpty() || toString().length() == 0;
}
/**
* Returns the maximum number of characters in the field. Value -1 is
* considered unlimited. Terminal may however have some technical limits.
*
* @return the maxLength
*/
public int getMaxLength() {
return maxLength;
}
/**
* Sets the maximum number of characters in the field. Value -1 is
* considered unlimited. Terminal may however have some technical limits.
*
* @param maxLength
* the maxLength to set
*/
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
requestRepaint();
}
public void addListener(FocusListener listener) {
addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
FocusListener.focusMethod);
}
public void removeListener(FocusListener listener) {
removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
}
public void addListener(BlurListener listener) {
addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
BlurListener.blurMethod);
}
public void removeListener(BlurListener listener) {
removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
}
/**
* Selects all text in the field.
*
* @since 6.4
*/
public void selectAll() {
String text = getValue() == null ? "" : getValue().toString();
setSelectionRange(0, text.length());
}
/**
* Sets the range of text to be selected.
*
* As a side effect the field will become focused.
*
* @since 6.4
*
* @param pos
* the position of the first character to be selected
* @param length
* the number of characters to be selected
*/
public void setSelectionRange(int pos, int length) {
selectionPosition = pos;
selectionLength = length;
focus();
requestRepaint();
}
/**
* Sets the cursor position in the field. As a side effect the field will
* become focused.
*
* @since 6.4
*
* @param pos
* the position for the cursor
* */
public void setCursorPosition(int pos) {
setSelectionRange(pos, 0);
}
}
|