package com.ecep.contract.converter;

import com.ecep.contract.service.ProjectTypeService;
import com.ecep.contract.vo.ProjectTypeVo;
import javafx.util.StringConverter;

/**
 * ProjectTypeVo的StringConverter实现，用于JavaFX控件中的显示和转换
 */
public class ProjectTypeStringConverter extends StringConverter<ProjectTypeVo> {
    private final ProjectTypeService service;

    /**
     * 构造函数
     * 
     * @param service ProjectTypeService实例
     */
    public ProjectTypeStringConverter(ProjectTypeService service) {
        this.service = service;
    }

    /**
     * 将ProjectTypeVo对象转换为字符串
     * 
     * @param object ProjectTypeVo对象
     * @return 转换后的字符串
     */
    @Override
    public String toString(ProjectTypeVo object) {
        return object == null ? "" : object.getName();
    }

    /**
     * 将字符串转换为ProjectTypeVo对象
     * 
     * @param string 字符串
     * @return 转换后的ProjectTypeVo对象
     */
    @Override
    public ProjectTypeVo fromString(String string) {
        if (string == null || string.isEmpty()) {
            return null;
        }
        return service.findByName(string);
    }
}