package com.ecep.contract.controller.table.cell;

import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import javafx.util.converter.NumberStringConverter;

public class NumberTableCell<S> extends TableCell<S, Number> {
    private final NumberStringConverter numberStringConverter;

    public NumberTableCell(NumberStringConverter numberStringConverter) {
        this.numberStringConverter = numberStringConverter;
    }

    @Override
    protected void updateItem(Number item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setText(null);
        } else {
            setText(numberStringConverter.toString(item));
        }
    }

    public static <S> Callback<TableColumn<S, Number>, TableCell<S, Number>> forTableColumn(NumberStringConverter numberStringConverter) {
        return param -> new NumberTableCell<>(numberStringConverter);
    }   
}
