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

import javafx.beans.property.StringProperty;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import org.springframework.util.StringUtils;

public class FilePathTableCell<V> extends TableCell<V, String> {

    private StringProperty path;

    /**
     * 创建一个用于表格列的单元格工厂
     */
    public static <T> Callback<TableColumn<T, String>, TableCell<T, String>> forTableColumn(
            StringProperty parentPath) {
        return param -> new FilePathTableCell<>(parentPath);
    }

    public FilePathTableCell(StringProperty parentPath) {
        this.path = parentPath;
    }

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || !StringUtils.hasText(item)) {
            setText("");
            return;
        }

        String path = this.path.get();
        if (StringUtils.hasText(path)) {
            if (item.startsWith(path)) {
                item = "~" + item.substring(path.length());
            }
        }
        setText(item);
    }
}
