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

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

The newest version!
package com.dua3.utility.fx.controls;

import com.dua3.utility.options.ChoiceOption;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;

import java.util.function.Supplier;

/**
 * Interface for an input field.
 *
 * @param  the input result type
 */
public class ChoiceInputControl implements InputControl {

    private final ComboBox> control;
    private final ChoiceOption option;
    private final Supplier dfltValue;
    private final Property valueProperty;

    public ChoiceInputControl(ChoiceOption option, Supplier dfltValue) {
        this.option = option;
        this.dfltValue = dfltValue;
        this.control = new ComboBox<>();
        this.valueProperty = new SimpleObjectProperty<>();

        control.valueProperty().addListener((v, o, n) -> valueProperty.setValue(n == null ? null : n.value()));
        valueProperty.addListener((v, o, n) -> control.getSelectionModel().select(n == null ? null : option.choice(n)));

        control.getItems().setAll(option.choices());
        control.getSelectionModel().select(option.choice(dfltValue.get()));
    }

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

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

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

    @Override
    public ReadOnlyBooleanProperty validProperty() {
        return new SimpleBooleanProperty(true);
    }

    @Override
    public ReadOnlyStringProperty errorProperty() {
        return new SimpleStringProperty("");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy