package org.hildan.fx.components;

import java.util.function.Predicate;

import javafx.scene.control.cell.TextFieldListCell;
import javafx.util.StringConverter;

import org.hildan.fxlog.themes.Css;

public class ValidatingTextFieldListCell<T> extends TextFieldListCell<T> {

    private final Predicate<String> validator;

    public ValidatingTextFieldListCell(StringConverter<T> converter, Predicate<String> validator) {
        super(converter);
        this.validator = validator;
    }

    public void commitEdit(T item) {
        if (!isEditing()) {
            return;
        }
        boolean itemIsValid = validator.test(getText());
        pseudoClassStateChanged(Css.INVALID, !itemIsValid);
        if (itemIsValid) {
            // only commit if the item is valid, otherwise we stay in edit state
            super.commitEdit(item);
        }
    }
}

@@ -66,16 +66,47 @@
 icate;%0A%0A
+import javafx.scene.control.*;%0A
 import j
@@ -607,24 +607,229 @@
 ;%0A        %7D%0A
+%0A        // the edited text is not in getText() but in the TextField used as Graphic for this cell%0A        TextField textField = (TextField) getGraphic();%0A        String editedText = textField.getText();%0A%0A
         bool
@@ -861,25 +861,26 @@
 or.test(
-get
+edited
 Text
-()
 );%0A     

Fix ValidatedTextFieldListCell to use the actual edited text