|
@@ -22,45 +22,30 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.addons.ui_swing.components.validating;
|
24
|
24
|
|
25
|
|
-import com.dmdirc.addons.ui_swing.UIUtilities;
|
|
25
|
+import com.dmdirc.addons.ui_swing.components.JIconTextField;
|
26
|
26
|
import com.dmdirc.ui.IconManager;
|
27
|
27
|
import com.dmdirc.util.validators.ValidationResponse;
|
28
|
28
|
import com.dmdirc.util.validators.Validator;
|
29
|
29
|
|
30
|
|
-import java.awt.Color;
|
31
|
|
-import java.awt.Font;
|
32
|
|
-import java.awt.event.KeyListener;
|
33
|
|
-import java.awt.event.MouseListener;
|
|
30
|
+import java.awt.Image;
|
|
31
|
+import java.awt.Insets;
|
34
|
32
|
|
35
|
|
-import javax.swing.Icon;
|
36
|
|
-import javax.swing.JComponent;
|
37
|
|
-import javax.swing.JLabel;
|
38
|
|
-import javax.swing.JTextField;
|
39
|
|
-import javax.swing.TransferHandler;
|
|
33
|
+import javax.swing.ImageIcon;
|
40
|
34
|
import javax.swing.event.DocumentEvent;
|
41
|
35
|
import javax.swing.event.DocumentListener;
|
42
|
|
-import javax.swing.text.BadLocationException;
|
43
|
|
-import javax.swing.text.Document;
|
44
|
36
|
|
45
|
|
-import net.miginfocom.swing.MigLayout;
|
46
|
37
|
|
47
|
38
|
/**
|
48
|
39
|
* Validating Text field.
|
49
|
40
|
*/
|
50
|
|
-public class ValidatingJTextField extends JComponent implements DocumentListener {
|
|
41
|
+public class ValidatingJTextField extends JIconTextField implements DocumentListener {
|
51
|
42
|
|
52
|
|
- /**
|
53
|
|
- * A version number for this class. It should be changed whenever the class
|
54
|
|
- * structure is changed (or anything else that would prevent serialized
|
55
|
|
- * objects being unserialized with the new class).
|
56
|
|
- */
|
57
|
|
- private static final long serialVersionUID = 1;
|
58
|
|
- /** TextField. */
|
59
|
|
- private final JTextField textField;
|
|
43
|
+ /** A version number for this class. */
|
|
44
|
+ private static final long serialVersionUID = 2;
|
60
|
45
|
/** Validator. */
|
61
|
46
|
private Validator<String> validator;
|
62
|
|
- /** Error icon. */
|
63
|
|
- private final JLabel errorIcon;
|
|
47
|
+ /** Error image. */
|
|
48
|
+ private final ImageIcon errorIcon;
|
64
|
49
|
|
65
|
50
|
/**
|
66
|
51
|
* Instantiates a new Validating text field.
|
|
@@ -70,41 +55,39 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
|
70
|
55
|
*/
|
71
|
56
|
public ValidatingJTextField(final IconManager iconManager,
|
72
|
57
|
final Validator<String> validator) {
|
73
|
|
- this(iconManager, new JTextField(), validator);
|
|
58
|
+ this(iconManager.getImage("input-error"), "", validator);
|
74
|
59
|
}
|
|
60
|
+
|
75
|
61
|
/**
|
76
|
62
|
* Instantiates a new Validating text field.
|
77
|
63
|
*
|
78
|
64
|
* @param iconManager Icon manager
|
79
|
|
- * @param textField JTextField to wrap
|
|
65
|
+ * @param text Text to display in textfield
|
80
|
66
|
* @param validator Validator instance
|
81
|
67
|
*/
|
82
|
|
- public ValidatingJTextField(final IconManager iconManager,
|
83
|
|
- final JTextField textField, final Validator<String> validator) {
|
84
|
|
- this(iconManager.getIcon("input-error"), textField, validator);
|
|
68
|
+ public ValidatingJTextField(final IconManager iconManager, final String text,
|
|
69
|
+ final Validator<String> validator) {
|
|
70
|
+ this(iconManager.getImage("input-error"), text, validator);
|
85
|
71
|
}
|
86
|
72
|
|
87
|
73
|
/**
|
88
|
74
|
* Instantiates a new Validating text field.
|
89
|
75
|
*
|
90
|
76
|
* @param icon Icon to show on error
|
91
|
|
- * @param textField JTextField to wrap
|
|
77
|
+ * @param text Text to display in textfield
|
92
|
78
|
* @param validator Validator instance
|
93
|
79
|
*/
|
94
|
|
- public ValidatingJTextField(final Icon icon,
|
95
|
|
- final JTextField textField, final Validator<String> validator) {
|
|
80
|
+ public ValidatingJTextField(final Image icon, final String text, final Validator<String> validator) {
|
96
|
81
|
super();
|
97
|
|
- this.textField = textField;
|
98
|
82
|
this.validator = validator;
|
99
|
|
- errorIcon = new JLabel(icon);
|
|
83
|
+ errorIcon = new ImageIcon(icon);
|
|
84
|
+ setText(text);
|
100
|
85
|
|
101
|
|
- setLayout(new MigLayout("fill, ins 0, hidemode 3, gap 0"));
|
102
|
|
- add(textField, "grow, pushx");
|
103
|
|
- add(errorIcon);
|
|
86
|
+ setMargin(new Insets(0, 0, 0, errorIcon.getIconWidth()));
|
104
|
87
|
|
105
|
88
|
checkError();
|
106
|
89
|
|
107
|
|
- textField.getDocument().addDocumentListener(this);
|
|
90
|
+ getDocument().addDocumentListener(this);
|
108
|
91
|
}
|
109
|
92
|
|
110
|
93
|
/**
|
|
@@ -121,15 +104,19 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
|
121
|
104
|
* Checks the text for errors and sets the error state accordingly.
|
122
|
105
|
*/
|
123
|
106
|
public void checkError() {
|
124
|
|
- if (textField.isEnabled()) {
|
125
|
|
- final ValidationResponse vr =
|
126
|
|
- validator.validate(textField.getText());
|
127
|
|
- errorIcon.setToolTipText(vr.getFailureReason());
|
128
|
|
- firePropertyChange("validationResult", !errorIcon.isVisible(), !vr.isFailure());
|
129
|
|
- errorIcon.setVisible(vr.isFailure());
|
|
107
|
+ if (isEnabled()) {
|
|
108
|
+ final ValidationResponse vr = validator.validate(getText());
|
|
109
|
+ setMessage(vr.getFailureReason());
|
|
110
|
+ firePropertyChange("validationResult", getMessage() != null, !vr.isFailure());
|
|
111
|
+ if (vr.isFailure()) {
|
|
112
|
+ setIcon(errorIcon);
|
|
113
|
+ } else {
|
|
114
|
+ setIcon(null);
|
|
115
|
+ }
|
130
|
116
|
} else {
|
131
|
|
- firePropertyChange("validationResult", !errorIcon.isVisible(), true);
|
132
|
|
- errorIcon.setVisible(false);
|
|
117
|
+ setIcon(null);
|
|
118
|
+ setMessage(null);
|
|
119
|
+ firePropertyChange("validationResult", !getMessage().isEmpty(), true);
|
133
|
120
|
}
|
134
|
121
|
}
|
135
|
122
|
|
|
@@ -141,7 +128,7 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
|
141
|
128
|
* @return true iif the text validates
|
142
|
129
|
*/
|
143
|
130
|
public boolean validateText() {
|
144
|
|
- if (textField.isEnabled()) {
|
|
131
|
+ if (isEnabled()) {
|
145
|
132
|
return !validator.validate(getText()).isFailure();
|
146
|
133
|
} else {
|
147
|
134
|
return true;
|
|
@@ -165,351 +152,4 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
|
165
|
152
|
public void removeUpdate(final DocumentEvent e) {
|
166
|
153
|
checkError();
|
167
|
154
|
}
|
168
|
|
-
|
169
|
|
- /** {@inheritDoc} */
|
170
|
|
- @Override
|
171
|
|
- public void setToolTipText(final String text) {
|
172
|
|
- textField.setToolTipText(text);
|
173
|
|
- }
|
174
|
|
-
|
175
|
|
- /** {@inheritDoc} */
|
176
|
|
- @Override
|
177
|
|
- public void setEnabled(final boolean enabled) {
|
178
|
|
- textField.setEnabled(enabled);
|
179
|
|
- checkError();
|
180
|
|
- }
|
181
|
|
-
|
182
|
|
- /** {@inheritDoc} */
|
183
|
|
- @Override
|
184
|
|
- public void requestFocus() {
|
185
|
|
- textField.requestFocus();
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- /** {@inheritDoc} */
|
189
|
|
- @Override
|
190
|
|
- public boolean requestFocusInWindow() {
|
191
|
|
- return textField.requestFocusInWindow();
|
192
|
|
- }
|
193
|
|
-
|
194
|
|
- /**
|
195
|
|
- * Sets the text in the textfield.
|
196
|
|
- *
|
197
|
|
- * @see javax.swing.JTextField#setText(String)
|
198
|
|
- *
|
199
|
|
- * @param t Text to set
|
200
|
|
- */
|
201
|
|
- public void setText(final String t) {
|
202
|
|
- textField.setText(t);
|
203
|
|
- }
|
204
|
|
-
|
205
|
|
- /**
|
206
|
|
- * Sets the selection start.
|
207
|
|
- *
|
208
|
|
- * @see javax.swing.JTextField#setSelectionStart(int)
|
209
|
|
- *
|
210
|
|
- * @param selectionStart Start of the selection
|
211
|
|
- */
|
212
|
|
- public void setSelectionStart(final int selectionStart) {
|
213
|
|
- textField.setSelectionStart(selectionStart);
|
214
|
|
- }
|
215
|
|
-
|
216
|
|
- /**
|
217
|
|
- * Sets the selection end.
|
218
|
|
- *
|
219
|
|
- * @see javax.swing.JTextField#setSelectionEnd(int)
|
220
|
|
- *
|
221
|
|
- * @param selectionEnd End of the selection
|
222
|
|
- */
|
223
|
|
- public void setSelectionEnd(final int selectionEnd) {
|
224
|
|
- textField.setSelectionEnd(selectionEnd);
|
225
|
|
- }
|
226
|
|
-
|
227
|
|
- /**
|
228
|
|
- * Sets whether the component is editable.
|
229
|
|
- *
|
230
|
|
- * @see javax.swing.JTextField#setEditable(boolean)
|
231
|
|
- *
|
232
|
|
- * @param b editable state for the component
|
233
|
|
- */
|
234
|
|
- public void setEditable(final boolean b) {
|
235
|
|
- textField.setEditable(b);
|
236
|
|
- }
|
237
|
|
-
|
238
|
|
- /**
|
239
|
|
- * Selects all text in the textfield.
|
240
|
|
- *
|
241
|
|
- * @see javax.swing.JTextField#selectAll()
|
242
|
|
- */
|
243
|
|
- public void selectAll() {
|
244
|
|
- textField.selectAll();
|
245
|
|
- }
|
246
|
|
-
|
247
|
|
- /**
|
248
|
|
- * Selects the specified text in the textfield.
|
249
|
|
- *
|
250
|
|
- * @see javax.swing.JTextField#select(int, int)
|
251
|
|
- *
|
252
|
|
- * @param selectionStart Selection start
|
253
|
|
- * @param selectionEnd Selection end
|
254
|
|
- */
|
255
|
|
- public void select(final int selectionStart, final int selectionEnd) {
|
256
|
|
- textField.select(selectionStart, selectionEnd);
|
257
|
|
- }
|
258
|
|
-
|
259
|
|
- /**
|
260
|
|
- * Replaces the textfields selection with the specified content.
|
261
|
|
- *
|
262
|
|
- * @see javax.swing.JTextField#replaceSelection(String)
|
263
|
|
- *
|
264
|
|
- * @param content Text to replace selection with
|
265
|
|
- */
|
266
|
|
- public void replaceSelection(final String content) {
|
267
|
|
- textField.replaceSelection(content);
|
268
|
|
- }
|
269
|
|
-
|
270
|
|
- /**
|
271
|
|
- * Paste's the system clipboard into the textfield.
|
272
|
|
- *
|
273
|
|
- * @see javax.swing.JTextField#paste()
|
274
|
|
- */
|
275
|
|
- public void paste() {
|
276
|
|
- textField.paste();
|
277
|
|
- }
|
278
|
|
-
|
279
|
|
- /**
|
280
|
|
- * Checks if the textfield is editable.
|
281
|
|
- *
|
282
|
|
- * @see javax.swing.JTextField#isEditable()
|
283
|
|
- *
|
284
|
|
- * @return true iif the textfield is editable
|
285
|
|
- */
|
286
|
|
- public boolean isEditable() {
|
287
|
|
- return textField.isEditable();
|
288
|
|
- }
|
289
|
|
-
|
290
|
|
- /**
|
291
|
|
- * Returns the text in the textfield.
|
292
|
|
- *
|
293
|
|
- * @see javax.swing.JTextField#getText()
|
294
|
|
- *
|
295
|
|
- * @return Textfield content
|
296
|
|
- */
|
297
|
|
- public String getText() {
|
298
|
|
- return textField.getText();
|
299
|
|
- }
|
300
|
|
-
|
301
|
|
- /**
|
302
|
|
- * Returns the specified section of text in the textfield.
|
303
|
|
- *
|
304
|
|
- * @see javax.swing.JTextField#getText(int, int)
|
305
|
|
- *
|
306
|
|
- * @param offs Start offset
|
307
|
|
- * @param len section length
|
308
|
|
- *
|
309
|
|
- * @return Specified textfield content
|
310
|
|
- *
|
311
|
|
- * @throws javax.swing.text.BadLocationException if the bounds are wrong
|
312
|
|
- */
|
313
|
|
- public String getText(final int offs, final int len) throws BadLocationException {
|
314
|
|
- return textField.getText(offs, len);
|
315
|
|
- }
|
316
|
|
-
|
317
|
|
- /**
|
318
|
|
- * Returns the start of the selection in the textfield.
|
319
|
|
- *
|
320
|
|
- * @see javax.swing.JTextField#getSelectionStart()
|
321
|
|
- *
|
322
|
|
- * @return Selection start
|
323
|
|
- */
|
324
|
|
- public int getSelectionStart() {
|
325
|
|
- return textField.getSelectionStart();
|
326
|
|
- }
|
327
|
|
-
|
328
|
|
- /**
|
329
|
|
- * Returns the end of the textfield selection.
|
330
|
|
- *
|
331
|
|
- * @see javax.swing.JTextField#getSelectionEnd()
|
332
|
|
- *
|
333
|
|
- * @return Selection end
|
334
|
|
- */
|
335
|
|
- public int getSelectionEnd() {
|
336
|
|
- return textField.getSelectionEnd();
|
337
|
|
- }
|
338
|
|
-
|
339
|
|
- /**
|
340
|
|
- * Returns the selected text in the textfield.
|
341
|
|
- *
|
342
|
|
- * @see javax.swing.JTextField#getSelectedText()
|
343
|
|
- *
|
344
|
|
- * @return Selected text
|
345
|
|
- */
|
346
|
|
- public String getSelectedText() {
|
347
|
|
- return textField.getSelectedText();
|
348
|
|
- }
|
349
|
|
-
|
350
|
|
- /**
|
351
|
|
- * Returns the textfield's document.
|
352
|
|
- *
|
353
|
|
- * @see javax.swing.JTextField#getDocument()
|
354
|
|
- *
|
355
|
|
- * @return Textfield's document
|
356
|
|
- */
|
357
|
|
- public Document getDocument() {
|
358
|
|
- return textField.getDocument();
|
359
|
|
- }
|
360
|
|
-
|
361
|
|
- /**
|
362
|
|
- * Cuts the selected text from the textfield into the clipboard.
|
363
|
|
- *
|
364
|
|
- * @see javax.swing.JTextField#cut()
|
365
|
|
- */
|
366
|
|
- public void cut() {
|
367
|
|
- textField.cut();
|
368
|
|
- }
|
369
|
|
-
|
370
|
|
- /**
|
371
|
|
- * Copies the selected text from the textfield into the clipboard.
|
372
|
|
- *
|
373
|
|
- * @see javax.swing.JTextField#copy()
|
374
|
|
- */
|
375
|
|
- public void copy() {
|
376
|
|
- textField.copy();
|
377
|
|
- }
|
378
|
|
-
|
379
|
|
- /**
|
380
|
|
- * Returns the font for the textfield.
|
381
|
|
- *
|
382
|
|
- * @see javax.swing.JTextField#copy()
|
383
|
|
- */
|
384
|
|
- @Override
|
385
|
|
- public Font getFont() {
|
386
|
|
- return textField.getFont();
|
387
|
|
- }
|
388
|
|
-
|
389
|
|
- /** {@inheritDoc} */
|
390
|
|
- @Override
|
391
|
|
- public synchronized void addKeyListener(final KeyListener l) {
|
392
|
|
- textField.addKeyListener(l);
|
393
|
|
- }
|
394
|
|
-
|
395
|
|
- /** {@inheritDoc} */
|
396
|
|
- @Override
|
397
|
|
- public synchronized void removeKeyListener(final KeyListener l) {
|
398
|
|
- textField.removeKeyListener(l);
|
399
|
|
- }
|
400
|
|
-
|
401
|
|
- /** {@inheritDoc} */
|
402
|
|
- @Override
|
403
|
|
- public synchronized void addMouseListener(final MouseListener l) {
|
404
|
|
- textField.addMouseListener(l);
|
405
|
|
- }
|
406
|
|
-
|
407
|
|
- /** {@inheritDoc} */
|
408
|
|
- @Override
|
409
|
|
- public synchronized void removeMouseListener(final MouseListener l) {
|
410
|
|
- textField.removeMouseListener(l);
|
411
|
|
- }
|
412
|
|
-
|
413
|
|
- /**
|
414
|
|
- * Sets the drag enabled property on the textfield.
|
415
|
|
- *
|
416
|
|
- * @param enabled Enabled?
|
417
|
|
- */
|
418
|
|
- public void setDragEnabled(final boolean enabled) {
|
419
|
|
- textField.setDragEnabled(enabled);
|
420
|
|
- }
|
421
|
|
-
|
422
|
|
- /** {@inheritDoc} */
|
423
|
|
- @Override
|
424
|
|
- public void setTransferHandler(final TransferHandler newHandler) {
|
425
|
|
- textField.setTransferHandler(newHandler);
|
426
|
|
- }
|
427
|
|
-
|
428
|
|
- /**
|
429
|
|
- * Returns the validator used by this text field.
|
430
|
|
- *
|
431
|
|
- * @since 0.6.3m1
|
432
|
|
- *
|
433
|
|
- * @return This field's validator
|
434
|
|
- */
|
435
|
|
- public Validator<String> getValidator() {
|
436
|
|
- return validator;
|
437
|
|
- }
|
438
|
|
-
|
439
|
|
- /**
|
440
|
|
- * Returns the text field used by this validating text field.
|
441
|
|
- *
|
442
|
|
- * @since 0.6.3m1
|
443
|
|
- *
|
444
|
|
- * @return This field's text field
|
445
|
|
- */
|
446
|
|
- public JTextField getTextField() {
|
447
|
|
- return textField;
|
448
|
|
- }
|
449
|
|
-
|
450
|
|
- /**
|
451
|
|
- * Sets the caret colour to the specified coloour.
|
452
|
|
- *
|
453
|
|
- * @param optionColour Colour for the caret
|
454
|
|
- */
|
455
|
|
- public void setCaretColor(final Color optionColour) {
|
456
|
|
- UIUtilities.invokeLater(new Runnable() {
|
457
|
|
-
|
458
|
|
- /** {@inheritDoc} */
|
459
|
|
- @Override
|
460
|
|
- public void run() {
|
461
|
|
- textField.setCaretColor(optionColour);
|
462
|
|
- }
|
463
|
|
- });
|
464
|
|
- }
|
465
|
|
-
|
466
|
|
- /**
|
467
|
|
- * Sets the foreground colour to the specified coloour.
|
468
|
|
- *
|
469
|
|
- * @param optionColour Colour for the foreground
|
470
|
|
- */
|
471
|
|
- @Override
|
472
|
|
- public void setForeground(final Color optionColour) {
|
473
|
|
- UIUtilities.invokeLater(new Runnable() {
|
474
|
|
-
|
475
|
|
- /** {@inheritDoc} */
|
476
|
|
- @Override
|
477
|
|
- public void run() {
|
478
|
|
- textField.setForeground(optionColour);
|
479
|
|
- }
|
480
|
|
- });
|
481
|
|
- }
|
482
|
|
-
|
483
|
|
- /**
|
484
|
|
- * Sets the disabled text colour to the specified coloour.
|
485
|
|
- *
|
486
|
|
- * @param optionColour Colour for the disabled text
|
487
|
|
- */
|
488
|
|
- public void setDisabledTextColour(final Color optionColour) {
|
489
|
|
- UIUtilities.invokeLater(new Runnable() {
|
490
|
|
-
|
491
|
|
- /** {@inheritDoc} */
|
492
|
|
- @Override
|
493
|
|
- public void run() {
|
494
|
|
- textField.setDisabledTextColor(optionColour);
|
495
|
|
- }
|
496
|
|
- });
|
497
|
|
- }
|
498
|
|
-
|
499
|
|
- /**
|
500
|
|
- * Sets the background colour to the specified coloour.
|
501
|
|
- *
|
502
|
|
- * @param optionColour Colour for the caret
|
503
|
|
- */
|
504
|
|
- @Override
|
505
|
|
- public void setBackground(final Color optionColour) {
|
506
|
|
- UIUtilities.invokeLater(new Runnable() {
|
507
|
|
-
|
508
|
|
- /** {@inheritDoc} */
|
509
|
|
- @Override
|
510
|
|
- public void run() {
|
511
|
|
- textField.setBackground(optionColour);
|
512
|
|
- }
|
513
|
|
- });
|
514
|
|
- }
|
515
|
155
|
}
|