1 package com.ca.directory.jxplorer.tree; 2 3 import javax.swing.*; 4 import javax.swing.tree.DefaultTreeCellRenderer ; 5 import java.awt.*; 6 7 21 22 public class SmartTreeCellRenderer extends DefaultTreeCellRenderer 23 { 24 boolean useIcons = true; 25 26 public JLabel displayLabel = new JLabel("label"); 27 28 private static final Color BLUE = new Color(0x000077); 29 private static final Color WHITE = Color.white; 30 private static final Color BLACK = Color.black; 31 32 private Color currentBackground = null; 33 private Color currentForeground = null; 34 private ImageIcon currentIcon = null; 35 36 40 public SmartTreeCellRenderer() 41 { 42 super(); 43 } 44 45 52 53 public SmartTreeCellRenderer(boolean usingIcons) 54 { 55 this(); 56 useIcons = usingIcons; 57 } 58 59 79 80 public Component getTreeCellRendererComponent(JTree tree, Object value, 81 boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) 82 { 83 Object icon = null; 84 85 if (value instanceof SmartNode) 86 { 87 SmartNode node = (SmartNode) value; 88 89 if (selected) 90 93 { 94 currentForeground = WHITE; 95 currentBackground = BLUE; 96 displayLabel.setForeground(currentForeground); 97 displayLabel.setBackground(currentBackground); 98 displayLabel.setOpaque(true); 99 } 100 else 101 104 { 105 if (currentBackground != WHITE) 106 110 { 111 currentForeground = BLACK; 112 currentBackground = WHITE; 113 displayLabel.setForeground(currentForeground); 114 displayLabel.setBackground(currentBackground); 115 displayLabel.setOpaque(false); 116 } 117 } 118 119 120 if (node.isMultiValued() == true) { 123 StringBuffer buffy = new StringBuffer (); 124 buffy.append(node.rdn.getRawVal(0)); 125 int size = node.rdn.size(); 126 for (int i=1; i<size; i++) 127 { 128 buffy.append(" + "); buffy.append(node.rdn.getRawVal(i)); 130 } 131 132 displayLabel.setText(buffy.toString()); 133 } 134 else if (node.isDummy()) 135 { 136 displayLabel.setText(node.getDummyMessage()); 137 } 138 else if (node.isBlankRoot()) 139 { 140 displayLabel.setText(node.getBlankRootName()); 141 } 142 else { 144 displayLabel.setText(node.rdn.getRawVal(0)); 145 } 146 147 148 try 150 { 151 154 155 if (node.getIcon() != currentIcon) 156 { 157 currentIcon = node.getIcon(); 158 displayLabel.setIcon(currentIcon); 159 } 160 } 161 catch (Exception e) { 163 } 165 166 167 return displayLabel; 168 } 169 else 170 { 171 displayLabel.setText(value.toString()); return displayLabel; 173 } 174 } 175 } | Popular Tags |