package v2ch12.treeRender;

import module java.base;
import module java.desktop;

/**
 * This class renders a class name either in plain or italic. Abstract classes are italic.
 */
public class ClassNameTreeCellRenderer extends DefaultTreeCellRenderer {
    private Font plainFont = null;
    private Font italicFont = null;

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
            boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
                hasFocus);
        // get the user object
        var node = (DefaultMutableTreeNode) value;
        Class<?> c = (Class<?>) node.getUserObject();

        // the first time, derive italic font from plain font
        if (plainFont == null) {
            plainFont = getFont();
            // the tree cell renderer is sometimes called with a label that has a null font
            if (plainFont != null) italicFont = plainFont.deriveFont(Font.ITALIC);
        }

        // set font to italic if the class is abstract, plain otherwise
        if (Modifier.isAbstract(c.getModifiers()))
            setFont(italicFont);
        else
            setFont(plainFont);
        return this;
    }
}
