package _VisualDVM.ProjectData.Files.UI;
import Common.MainModule_;
import Common.Visual.Fonts.VisualiserFonts;
import Common.Visual.Trees.StyledTreeCellRenderer;
import _VisualDVM.Global;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.FileState;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.io.File;
import java.util.Map;
//https://kodejava.org/how-do-i-create-jtree-with-different-icons-for-each-node/
//https://ru.coredump.biz/questions/14968005/dynamically-change-icon-of-specific-nodes-in-jtree
public class FilesTreeCellRenderer extends StyledTreeCellRenderer {
    public java.awt.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);
        Object o = ((DefaultMutableTreeNode) value).getUserObject();
        if (o instanceof File) {
            //это папка.
            File dir = (File) o;
            setIcon(new ImageIcon(getClass().getResource("/icons/Folder.png")));
            setText(dir.getName());
            setFont(MainModule_.instance.getUI().getTheme().Fonts.get(VisualiserFonts.TreePlain));
        } else if (o instanceof DBProjectFile) {
            DBProjectFile file = (DBProjectFile) o;
            if (Global.files_multiselection) {
                setIcon(file.GetSelectionIcon());
            } else {
                setIcon(file.GetIcon());
            }
            setText(file.file.getName());
            if (file.IsMain()) setFont(MainModule_.instance.getUI().getTheme().Fonts.get(VisualiserFonts.TreeBold));
            else setFont(MainModule_.instance.getUI().getTheme().Fonts.get(VisualiserFonts.TreePlain));
            if (file.state.equals(FileState.Excluded)) {
                Map attributes = getFont().getAttributes();
                attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                setFont(new Font(attributes));
            }
        }
        setForeground(tree.getForeground());
        return this;
    }
}
