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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.ecep.contract.MyDateTimeUtils;

/**
 * 本地日期时间单元格
 */
public class LocalDateTimeTableCell<T>
        extends javafx.scene.control.TableCell<T, java.time.LocalDateTime> {

    private final DateTimeFormatter formatter;

    public LocalDateTimeTableCell(DateTimeFormatter formatter) {
        this.formatter = formatter;
    }

    public LocalDateTimeTableCell(String format) {
        this(DateTimeFormatter.ofPattern(format));
    }

    public LocalDateTimeTableCell() {
        this(MyDateTimeUtils.DEFAULT_DATETIME_FORMAT_PATTERN);
    }

    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setText(null);
        } else {
            setText(item.format(formatter));
        }
    }
}
