1 30 package com.genimen.djeneric.tools.console.nodes; 31 32 import java.awt.Color ; 33 import java.awt.Component ; 34 import java.awt.Font ; 35 import java.awt.Graphics ; 36 37 import javax.swing.Icon ; 38 import javax.swing.ImageIcon ; 39 import javax.swing.JLabel ; 40 import javax.swing.JTree ; 41 import javax.swing.tree.DefaultMutableTreeNode ; 42 import javax.swing.tree.TreeCellRenderer ; 43 44 import com.genimen.djeneric.language.Messages; 45 import com.genimen.djeneric.util.DjLogger; 46 47 public class CustomTreeCellRenderer extends JLabel implements TreeCellRenderer 48 { 49 private static final long serialVersionUID = 1L; 50 protected static Font defaultFont; 51 protected static ImageIcon collapsedIcon; 52 protected static ImageIcon expandedIcon; 53 protected static ImageIcon emptyFolder; 54 protected final static Color SelectedBackgroundColor = Color.yellow; 55 57 static 58 { 59 try 60 { 61 defaultFont = new Font ("SansSerif", 0, 12); 62 } 63 catch (Exception e) 64 { 65 DjLogger.log(e); 66 } 67 try 68 { 69 collapsedIcon = CustomTreeNode.getImageIcon("images/folder.gif"); 70 expandedIcon = CustomTreeNode.getImageIcon("images/document.gif"); 71 emptyFolder = CustomTreeNode.getImageIcon("images/emptyfolder.gif"); 72 } 73 catch (Exception e) 74 { 75 DjLogger.log(e); 76 System.out.println(Messages.getString("CustomTreeCellRenderer.CouldNotLoad", e.getMessage())); 77 } 78 } 79 80 83 protected boolean selected; 84 85 99 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 100 boolean leaf, int row, boolean hasFocus) 101 { 102 if (value == null) return (this); 103 String stringValue = value.toString(); 104 setText(stringValue); 105 setToolTipText(stringValue); 106 107 108 if (value instanceof CustomTreeNode) 109 { 110 setIcon(((CustomTreeNode) (value)).getImageIcon()); 111 } 112 else if (value instanceof DefaultMutableTreeNode ) 113 { 114 setIcon(CustomTreeNode.getImageIcon("images/schema.gif")); 116 } 117 else 118 { 119 setIcon(CustomTreeNode.getImageIcon("images/document.gif")); 120 } 122 123 124 this.selected = selected; 125 126 return (this); 127 } 128 129 136 public void paint(Graphics g) 137 { 138 Color bColor; 139 Icon currentI = getIcon(); 140 141 if (selected) 142 { 143 bColor = SelectedBackgroundColor; 144 } 145 else if (getParent() != null) 146 { 147 149 bColor = getParent().getBackground(); 150 } 151 else 152 { 153 bColor = getBackground(); 154 } 155 g.setColor(bColor); 156 157 if (currentI != null && getText() != null) 158 { 159 int offset = (currentI.getIconWidth() + getIconTextGap()); 160 g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); 161 } 162 else 163 { 164 g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 165 } 166 super.paint(g); 167 } 168 } | Popular Tags |