All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dua3.utility.fx.controls.SimpleInputControl Maven / Gradle / Ivy

There is a newer version: 15.0.2
Show newest version
package com.dua3.utility.fx.controls;

import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.scene.control.Control;
import org.jspecify.annotations.Nullable;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * SimpleInputControl is a generic class designed to manage an input control element and its state.
 * It provides functionalities to reset the control's value, validate input, and obtain error messages.
 *
 * @param  the type of the control, which must extend from Control
 * @param  the type of the value held by the input control
 */
public class SimpleInputControl implements InputControl {

    private final C control;
    private final State state;
    private final Supplier dflt;

    protected SimpleInputControl(C control, Property value, Supplier dflt, Function> validate) {
        this.control = control;
        this.state = new State<>(value, dflt, validate);
        this.dflt = dflt;

        reset();
    }

    @Override
    public C node() {
        return control;
    }

    @Override
    public Property valueProperty() {
        return state.valueProperty();
    }

    @Override
    public void reset() {
        state.valueProperty().setValue(dflt.get());
    }

    @Override
    public ReadOnlyBooleanProperty validProperty() {
        return state.validProperty();
    }

    @Override
    public ReadOnlyStringProperty errorProperty() {
        return state.errorProperty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy