1 package com.genimen.djeneric.tools.generator.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 GeneratorTreeCellRenderer 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 DjenericTreeNode) 46 { 47 setIcon(((DjenericTreeNode) (value)).getImageIcon()); 48 } 49 else 50 { 51 setIcon(Specifier.getImageIcon("document.gif")); } 53 54 55 _selected = selected; 56 57 return this; 58 } 59 60 65 public void paint(Graphics g) 66 { 67 Color bColor; 68 Icon currentI = getIcon(); 69 70 if (_selected) 71 { 72 bColor = _selectedBackgroundColor; 73 } 74 else if (getParent() != null) 75 { bColor = getParent().getBackground(); 77 } 78 else 79 { 80 bColor = getBackground(); 81 } 82 g.setColor(bColor); 83 84 if (currentI != null && getText() != null) 85 { 86 int offset = (currentI.getIconWidth() + getIconTextGap()); 87 g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); 88 } 89 else 90 { 91 g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 92 } 93 super.paint(g); 94 } 95 } | Popular Tags |