Example usage for javax.swing JTree getUI

List of usage examples for javax.swing JTree getUI

Introduction

In this page you can find the example usage for javax.swing JTree getUI.

Prototype

public TreeUI getUI() 

Source Link

Document

Returns the L&F object that renders this component.

Usage

From source file:de.codesourcery.eve.skills.ui.renderer.SkillTreeRenderer.java

@Override
public Component getTreeCellRendererComponent(final JTree tree, Object n, boolean sel, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {

    final ITreeNode node = (ITreeNode) n;
    final Object value = node.getValue();

    if (value == null) {

        super.getTreeCellRendererComponent(tree, n, sel, expanded, leaf, row, hasFocus);

        setText(null);//from  ww w  .  j ava  2  s. co m
        return this;
    }

    final ICharacter character = charProvider.getSelectedItem();

    final String label;
    if (value instanceof Prerequisite) {

        super.getTreeCellRendererComponent(tree, n, sel, expanded, leaf, row, hasFocus);

        final Prerequisite r = (Prerequisite) value;

        String sCurrentLvl = "";
        if (character.hasSkill(r.getSkill())) {
            final int currentLvl = character.getSkillLevel(r.getSkill()).getLevel();
            if (currentLvl < r.getRequiredLevel()) {
                sCurrentLvl = " ( current: " + currentLvl + " )";
            }
        }
        label = "req: " + r.getSkill().getName() + " lvl " + r.getRequiredLevel() + sCurrentLvl;

        setForeground(getColorForSkill(r, character));
        setText(label);

        setToolTipText(toToolTip(character, r.getSkill()));

    } else if (value instanceof SkillGroup) {

        super.getTreeCellRendererComponent(tree, n, sel, expanded, leaf, row, hasFocus);

        final SkillGroup cat = (SkillGroup) value;

        if (character != null) {

            final long current = cat.getSkillpoints(character);

            final long maximumSkillpoints = cat.getMaximumSkillpoints();

            final String percent = skillPointDeltaToString(current, maximumSkillpoints);

            label = cat.getName() + "          " + StringUtils.leftPad(percent, 30 - cat.getName().length());
        } else {
            label = cat.getName();
        }
    } else if (value instanceof Skill) {

        final Skill s = (Skill) value;

        final int skillLevel;
        final String skillPoints;
        TrainedSkill trained = null;
        if (character != null) {

            trained = character.getSkillLevel(s);

            final long current = trained.getSkillpoints();
            final long max = s.getMaximumSkillpoints();

            skillLevel = trained.getLevel();
            if (renderCurrentSkillPoints) {
                skillPoints = "        " + skillPointDeltaToString(current, max);
            } else {
                skillPoints = "";
            }
        } else {
            skillLevel = 0;
            skillPoints = "";
        }

        String partiallyTrained = "";
        if (trained != null && skillLevel != Skill.MAX_LEVEL && trained.isPartiallyTrained()) {
            final int nextLevel = skillLevel + 1;
            int percent = Math.round(trained.getFractionOfLevelTrained(nextLevel));
            if (percent == 100) {
                percent = 99;
            }
            partiallyTrained = " [ " + percent + "% of lvl " + nextLevel + " trained ]";
        }

        label = s.getName() + " (Rank " + s.getRank() + ")" + skillPoints + partiallyTrained;

        IRenderCallback callback = new IRenderCallback() {

            @Override
            public Icon getClosedIcon() {
                return (Icon) DefaultLookup.get(tree, tree.getUI(), "Tree.closedIcon");
                //               return DefaultLookup.getIcon(tree, tree.getUI(), "Tree.closedIcon");
            }

            @Override
            public Icon getLeafIcon() {
                return (Icon) DefaultLookup.get(tree, tree.getUI(), "Tree.leafIcon");
                //               return DefaultLookup.getIcon(tree, tree.getUI() , "Tree.leafIcon" );
            }

            @Override
            public Icon getOpenIcon() {
                return (Icon) DefaultLookup.get(tree, tree.getUI(), "Tree.openIcon");
                //               return DefaultLookup.getIcon(tree, tree.getUI() , "Tree.openIcon" );               
            }

            @Override
            public Color getTextNonSelectionColor() {
                return (Color) DefaultLookup.get(tree, tree.getUI(), "Tree.textForeground");
                //               return DefaultLookup.getColor(tree, tree.getUI() , "Tree.textForeground");
            }

            @Override
            public Color getTextSelectionColor() {
                return (Color) DefaultLookup.get(tree, tree.getUI(), "Tree.selectionForeground");
                //               return DefaultLookup.getColor(tree, tree.getUI() , "Tree.selectionForeground");
            }

            @Override
            public Color getBackgroundSelectionColor() {
                return (Color) DefaultLookup.get(tree, tree.getUI(), "Tree.selectionBackground");
                //               return DefaultLookup.getColor(tree, tree.getUI() , "Tree.selectionBackground");
            }

            @Override
            public Color getBackgroundNonSelectionColor() {
                return (Color) DefaultLookup.get(tree, tree.getUI(), "Tree.textBackground");
                //               return DefaultLookup.getColor(tree, tree.getUI() , "Tree.textBackground");
            }
        };

        skillLabel.setText(label);
        skillLabel.setupTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus, callback);
        if (trained != null) {
            skillLabel.setTrainedSkill(trained);
        } else {
            skillLabel.setTrainedSkill(new TrainedSkill(s, 0));
        }
        skillLabel.setForeground(getColorForSkill(s, character));

        skillLabel.setToolTipText(toToolTip(character, s));

        return skillLabel;
    } else {
        label = null;
    }

    if (DEBUG && value != null) {
        final String[] className = value.getClass().getName().split("\\.");
        final String name = "[" + className[className.length - 1] + "]";
        if (label != null) {
            setText(name + " " + label);
        } else {
            setText(name);
        }
    } else {
        setText(label);
    }

    return this;
}