001: /*
002: * @(#)DialogPageTreeCellRenderer.java
003: *
004: * Copyright 2002 - 2003 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.dialog;
007:
008: import com.jidesoft.plaf.UIDefaultsLookup;
009:
010: import javax.swing.*;
011: import javax.swing.tree.DefaultMutableTreeNode;
012: import javax.swing.tree.TreeCellRenderer;
013: import java.awt.*;
014:
015: /**
016: * A tree cell renderer for AbstractDialogPage.
017: */
018: class DialogPageTreeCellRenderer extends JLabel implements
019: TreeCellRenderer {
020: protected Color _textSelectionColor;
021: protected Color _textNonSelectionColor;
022: protected Color _bkSelectionColor;
023: protected Color _bkNonSelectionColor;
024: protected Color _borderSelectionColor;
025:
026: protected boolean m_selected;
027:
028: private static final Icon SELECTED = TreeIconsFactory
029: .getImageIcon(TreeIconsFactory.CellRenderer.SELECTED_B16);
030: private static final Icon BLANK = TreeIconsFactory
031: .getImageIcon(TreeIconsFactory.CellRenderer.BLANK_16);
032:
033: public DialogPageTreeCellRenderer() {
034: super ();
035: _textSelectionColor = UIDefaultsLookup
036: .getColor("Tree.selectionForeground");
037: _textNonSelectionColor = UIDefaultsLookup
038: .getColor("Tree.textForeground");
039: _bkSelectionColor = UIDefaultsLookup
040: .getColor("Tree.selectionBackground");
041: _bkNonSelectionColor = UIDefaultsLookup
042: .getColor("Tree.textBackground");
043: _borderSelectionColor = UIDefaultsLookup
044: .getColor("Tree.selectionBorderColor");
045: setOpaque(false);
046: }
047:
048: public Component getTreeCellRendererComponent(JTree tree,
049: Object value, boolean sel, boolean expanded, boolean leaf,
050: int row, boolean hasFocus) {
051: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
052: Object obj = node.getUserObject();
053:
054: if (obj instanceof Boolean)
055: setText("Retrieving data...");
056:
057: if (obj instanceof AbstractDialogPage) {
058: AbstractDialogPage idata = (AbstractDialogPage) obj;
059: setText(idata.getTitle());
060: } else {
061: setText(obj.toString());
062: setIcon(null);
063: }
064:
065: setFont(tree.getFont());
066: setForeground(sel ? _textSelectionColor
067: : _textNonSelectionColor);
068: setBackground(sel ? _bkSelectionColor : _bkNonSelectionColor);
069:
070: if (leaf) {
071: if (sel) {
072: setIcon(SELECTED);
073: } else {
074: setIcon(BLANK);
075: }
076: } else {
077: if (expanded) {
078: setIcon(UIDefaultsLookup.getIcon("Tree.openIcon"));
079: } else {
080: setIcon(UIDefaultsLookup.getIcon("Tree.closedIcon"));
081: }
082: }
083: m_selected = sel;
084: setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
085: return this ;
086: }
087:
088: @Override
089: public void paintComponent(Graphics g) {
090: Color bColor = getBackground();
091: Icon icon = getIcon();
092:
093: g.setColor(bColor);
094: int offset = 0;
095: if (icon != null && getText() != null)
096: offset = (icon.getIconWidth() + getIconTextGap()) - 1;
097: g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
098:
099: if (m_selected) {
100: g.setColor(_borderSelectionColor);
101: g.drawRect(offset, 0, getWidth() - offset - 1,
102: getHeight() - 1);
103: }
104: super.paintComponent(g);
105: }
106: }
|