diff options
Diffstat (limited to 'src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java')
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java b/src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java new file mode 100644 index 0000000..cc68ff2 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java @@ -0,0 +1,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); + } + +} |
