blob: cc68ff2ff161fba7bfcea0900d727806deb085ea (
plain)
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
|
/**
* File containing the RegExTextField class.
*/
package de.fhswf.in.inf.java2.aufgabe03;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
/**
* A class that checks an input against a regex and gives optical feedback.
*
* @author $Author: $
* @version $Revision: $, $Date: $ UTC
*/
public class RegExTextField extends HBox
{
private Label label;
private TextField textField;
private RegExValidator rv;
/**
* Creates an element that checks regexes.
*
* @param regEx
* The regex to check the text field against.
*/
public RegExTextField(String regEx)
{
this(regEx, "regEx");
}
/**
* Creates an element that checks regexes.
*
* @param regEx
* The regex to check the text field against.
* @param labelText
* An optional label that is shown before the text field.
*/
public RegExTextField(String regEx, String labelText)
{
if (regEx == null)
{
throw new IllegalArgumentException("RegEx can't be null.");
}
if (regEx.isEmpty())
{
throw new IllegalArgumentException("RegEx can't be empty.");
}
if (labelText == null)
{
throw new IllegalArgumentException("labelText can't be null.");
}
if (labelText.isEmpty())
{
throw new IllegalArgumentException("labelText can't be empty.");
}
label = new Label(labelText);
textField = new TextField();
label.setContentDisplay(ContentDisplay.RIGHT);
this.getChildren().addAll(label, textField);
rv = new RegExValidator(regEx);
rv.addStatusListener(e -> {
if (!e.isStatus())
{
label.setGraphic(new ImageView(new Image("img/error.png")));
}
else
{
label.setGraphic(null);
}
});
textField.textProperty().addListener(rv);
}
}
|