TypeTextField.java

package ui.common;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.TextField;

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class TypeTextField extends TextField {

    private static final String NUMBERS = "[\\-0-9]";
    private static final String FLOATS = "[\\-0-9.,]";
    private static final String LOCAL_DATE = "[\\-0-9]";

    private final StringProperty type = new SimpleStringProperty();

    public String getType() {
        return type.get();
    }

    public void setType(String type) {
        this.type.set(type);
        setPattern(type);
    }

    private String pattern;

    private void setPattern(String type) {
        switch (type) {
            case "Long":
            case "Integer":
            case "Short":
                pattern = NUMBERS;
                break;
            case "Float":
                pattern = FLOATS;
                break;
            case "String":
                pattern = null;
                break;
            case "LocalDate":
                pattern = LOCAL_DATE;
                break;
            default:
                throw new RuntimeException("Unsupported data type: " + type);
        }
    }

    @Override
    public void replaceText(int start, int end, String text) {
        if (pattern == null || text.isEmpty() || text.matches(pattern)) {
            super.replaceText(start, end, text);
        }
    }

    @Override
    public void replaceSelection(String text) {
        if (pattern == null || text.isEmpty() || text.matches(pattern)) {
            super.replaceSelection(text);
        }
    }

    public Object getValue() {
        if (type.get().equals("Long")) {
            try {
                return Long.parseLong(getText());
            } catch (NumberFormatException e) {
                return 0L;
            }
        } else if (type.get().equals("Integer")) {
            try {
                return Integer.parseInt(getText());
            } catch (NumberFormatException e) {
                return 0;
            }
        } else if (type.get().equals("LocalDate")) {
            try {
                return LocalDate.parse(getText(), DateTimeFormatter.ISO_DATE);
            } catch (Exception e) {
                try {
                    return YearMonth.parse(getText());
                } catch (Exception e2) {
                    try {
                        return Year.parse(getText());
                    } catch (Exception e3) {
                        return null;
                    }
                }
            }
        } else if (type.get().equals("Short")) {
            try {
                return Short.parseShort(getText());
            } catch (NumberFormatException e) {
                return (short) 0;
            }
        } else if (type.get().equals("Float")) {
            try {
                return Float.parseFloat(getText());
            } catch (NumberFormatException e) {
                return 0.f;
            }
        }
        return getText();
    }

    public void setValue(Object value) {
        setText(String.valueOf(value));
    }
}