1 package com.genimen.djeneric.tools.templateeditor.tree; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.awt.Font ; 6 import java.awt.Graphics ; 7 8 import javax.swing.Icon ; 9 import javax.swing.JLabel ; 10 import javax.swing.JTree ; 11 import javax.swing.tree.TreeCellRenderer ; 12 13 import com.genimen.djeneric.tools.specifier.Specifier; 14 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode; 15 16 public class TemplateTreeCellRenderer extends JLabel implements TreeCellRenderer 17 { 18 private static final long serialVersionUID = 1L; 19 static protected Font _defaultFont; 20 static protected final Color _selectedBackgroundColor = Color.yellow; 22 static 23 { 24 _defaultFont = new Font ("SansSerif", 0, 12); 25 } 26 27 28 protected boolean _selected; 29 30 36 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 37 boolean leaf, int row, boolean hasFocus) 38 { 39 if (value == null) return this; 40 String stringValue = value.toString(); 41 setText(stringValue); 42 setToolTipText(stringValue); 43 44 45 if (value instanceof DefaultTemplateTreeNode) 46 { 47 setIcon(((DefaultTemplateTreeNode) (value)).getImageIcon()); 48 } 49 else if (value instanceof DjenericTreeNode) 50 { 51 setIcon(((DjenericTreeNode) (value)).getImageIcon()); 52 } 53 else 54 { 55 setIcon(Specifier.getImageIcon("document.gif")); } 57 58 59 _selected = selected; 60 61 return this; 62 } 63 64 69 public void paint(Graphics g) 70 { 71 Color bColor; 72 Icon currentI = getIcon(); 73 74 if (_selected) 75 { 76 bColor = _selectedBackgroundColor; 77 } 78 else if (getParent() != null) 79 { bColor = getParent().getBackground(); 81 } 82 else 83 { 84 bColor = getBackground(); 85 } 86 g.setColor(bColor); 87 88 if (currentI != null && getText() != null) 89 { 90 int offset = (currentI.getIconWidth() + getIconTextGap()); 91 g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); 92 } 93 else 94 { 95 g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 96 } 97 super.paint(g); 98 } 99 } | Popular Tags |