diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-14 17:00:22 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-14 17:00:22 +0200 |
| commit | 31b7641ec7724f7eb2b5833c71705d176cb1bfdf (patch) | |
| tree | ead1f0d4a872e6053358049caa1901b34b8923ce /src/de/fhswf/in/inf/java2/aufgabe03/RegExTextField.java | |
| parent | dbfb59b0dd320bfd4f284cd6c3896892529e42f7 (diff) | |
| download | Java2-31b7641ec7724f7eb2b5833c71705d176cb1bfdf.tar.gz Java2-31b7641ec7724f7eb2b5833c71705d176cb1bfdf.zip | |
Includes validator framework and completes Task 3
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); + } + +} |
