From 31b7641ec7724f7eb2b5833c71705d176cb1bfdf Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 14 Apr 2015 17:00:22 +0200 Subject: Includes validator framework and completes Task 3 --- .../in/inf/java2/aufgabe03/RegExTextField.java | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java (limited to 'src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java') 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); + } + +} -- cgit v1.2.3-70-g09d2