package com.imyeyu.fx.ui.components;

import com.imyeyu.java.ref.Ref;
import javafx.geometry.Pos;
import javafx.scene.control.ListCell;

/**
 * 通用枚举列表项，默认反射 name 字段
 *
 * @param <T> 枚举类型，必须含有 name 字段
 *
 * @author 夜雨
 * @since 2023-06-08 17:01
 */
public class EnumListCell<T extends Enum<?>> extends ListCell<T> {

	@Override
	protected void updateItem(T item, boolean empty) {
		super.updateItem(item, empty);
		if (empty || item == null) {
			setText("");
			setGraphic(null);
		} else {
			setAlignment(Pos.CENTER);
			try {
				setText(Ref.getFieldValue(item, "name", String.class));
			} catch (IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
	}
}
