1 7 8 package org.jdesktop.swing.table; 9 10 import java.awt.Component ; 11 import java.awt.Color ; 12 import java.awt.FontMetrics ; 13 import java.awt.Graphics ; 14 import java.awt.Rectangle ; 15 16 import java.awt.geom.Rectangle2D ; 17 18 import java.text.DateFormat ; 19 import java.text.NumberFormat ; 20 21 import java.util.Date ; 22 import java.util.HashMap ; 23 24 import javax.swing.Icon ; 25 import javax.swing.JCheckBox ; 26 import javax.swing.JLabel ; 27 import javax.swing.JTable ; 28 import javax.swing.SwingConstants ; 29 import javax.swing.table.TableCellRenderer ; 30 import javax.swing.table.DefaultTableCellRenderer ; 31 32 import org.jdesktop.swing.data.Link; 33 34 public class TableCellRenderers { 35 36 private static HashMap typeMap; 37 38 static { 39 typeMap = new HashMap (); 42 typeMap.put(Number .class, 43 "org.jdesktop.swing.table.TableCellRenderers$NumberRenderer"); 44 typeMap.put(Double .class, 45 "org.jdesktop.swing.table.TableCellRenderers$DoubleRenderer"); 46 typeMap.put(Float .class, 47 "org.jdesktop.swing.table.TableCellRenderers$DoubleRenderer"); 48 typeMap.put(Date .class, 49 "org.jdesktop.swing.table.TableCellRenderers$DateRenderer"); 50 typeMap.put(Icon .class, 51 "org.jdesktop.swing.table.TableCellRenderers$IconRenderer"); 52 typeMap.put(Boolean .class, 53 "org.jdesktop.swing.table.TableCellRenderers$BooleanRenderer"); 54 typeMap.put(Link.class, 55 "org.jdesktop.swing.table.TableCellRenderers$LinkRenderer"); 56 57 } 58 59 private static String getRendererClassName(Class columnClass) { 60 String rendererClassName = (String ) typeMap.get(columnClass); 61 return rendererClassName != null ? rendererClassName : 62 "javax.swing.table.DefaultTableCellRenderer"; 63 } 64 65 71 public static void setDefaultRenderer(Class columnClass, String rendererClassName) { 72 typeMap.put(columnClass, rendererClassName); 73 } 74 75 84 public static TableCellRenderer getNewDefaultRenderer(Class columnClass) { 85 TableCellRenderer renderer = null; 86 String rendererClassName = getRendererClassName(columnClass); 87 try { 88 Class rendererClass = Class.forName(rendererClassName); 89 renderer = (TableCellRenderer ) rendererClass.newInstance(); 90 } 91 catch (Exception e) { 92 renderer = new DefaultTableCellRenderer (); 93 } 94 return renderer; 95 } 96 97 TableCellRenderers() { 98 } 99 100 109 static class NumberRenderer extends DefaultTableCellRenderer { 110 public NumberRenderer() { 111 super(); 112 setHorizontalAlignment(JLabel.RIGHT); 113 } 114 } 115 116 static class DoubleRenderer extends NumberRenderer { 117 NumberFormat formatter; 118 public DoubleRenderer() { 119 super(); 120 } 121 122 public void setValue(Object value) { 123 if (formatter == null) { 124 formatter = NumberFormat.getInstance(); 125 } 126 setText( (value == null) ? "" : formatter.format(value)); 127 } 128 } 129 130 static class DateRenderer extends DefaultTableCellRenderer { 131 DateFormat formatter; 132 public DateRenderer() { 133 super(); 134 } 135 136 public void setValue(Object value) { 137 if (formatter == null) { 138 formatter = DateFormat.getDateInstance(); 139 } 140 setText( (value == null) ? "" : formatter.format(value)); 141 } 142 } 143 144 static class IconRenderer extends DefaultTableCellRenderer { 145 public IconRenderer() { 146 super(); 147 setHorizontalAlignment(JLabel.CENTER); 148 } 149 150 public void setValue(Object value) { 151 setIcon( (value instanceof Icon ) ? (Icon ) value : null); 152 } 153 } 154 155 static class BooleanRenderer extends JCheckBox 156 implements TableCellRenderer { 157 public BooleanRenderer() { 158 super(); 159 setHorizontalAlignment(JLabel.CENTER); 160 } 161 162 public Component getTableCellRendererComponent(JTable table, 163 Object value, 164 boolean isSelected, boolean hasFocus, int row, int column) { 165 if (isSelected) { 166 setForeground(table.getSelectionForeground()); 167 super.setBackground(table.getSelectionBackground()); 168 } 169 else { 170 setForeground(table.getForeground()); 171 setBackground(table.getBackground()); 172 } 173 setSelected( (value != null && ( (Boolean ) value).booleanValue())); 174 return this; 175 } 176 } 177 178 181 public static class LinkRenderer extends DefaultTableCellRenderer { 182 183 private static Color colorLive = new Color (0, 0, 238); 185 private static Color colorVisited = new Color (82, 24, 139); 186 187 public void setValue(Object value) { 188 if (value != null && value instanceof Link) { 189 Link link = (Link) value; 190 191 setText(link.getText()); 192 setToolTipText(link.getURL().toString()); 193 194 if (link.getVisited()) { 195 setForeground(colorVisited); 196 } 197 else { 198 setForeground(colorLive); 199 } 200 } 201 else { 202 super.setValue(value != null? value.toString() : ""); 203 } 204 } 205 206 public void paintComponent(Graphics g) { 207 super.paintComponent(g); 208 if (!getText().equals("")) { 209 Rectangle rect = TableCellRenderers.getTextBounds(g, this); 213 214 FontMetrics fm = g.getFontMetrics(); 215 int descent = fm.getDescent(); 216 217 220 g.drawLine(rect.x, (rect.y + rect.height) - descent + 1, 221 rect.x + rect.width, 222 (rect.y + rect.height) - descent + 1); 223 } 224 } 225 } 226 227 228 232 static Rectangle getTextBounds(Graphics g, JLabel label) { 233 234 FontMetrics fm = g.getFontMetrics(); 235 Rectangle2D r2d = fm.getStringBounds(label.getText(), g); 236 Rectangle rect = r2d.getBounds(); 237 238 int xOffset = 0; 239 switch (label.getHorizontalAlignment()) { 240 case SwingConstants.RIGHT: 241 case SwingConstants.TRAILING: 242 xOffset = label.getBounds().width - rect.width; 243 break; 244 245 case SwingConstants.CENTER: 246 xOffset = (label.getBounds().width - rect.width)/2; 247 break; 248 249 default: 250 case SwingConstants.LEFT: 251 case SwingConstants.LEADING: 252 xOffset = 0; 253 break; 254 } 255 256 int yOffset = 0; 257 switch (label.getVerticalAlignment()) { 258 case SwingConstants.TOP: 259 yOffset = 0; 260 break; 261 case SwingConstants.CENTER: 262 yOffset = (label.getBounds().height - rect.height)/2; 263 break; 264 case SwingConstants.BOTTOM: 265 yOffset = label.getBounds().height - rect.height; 266 break; 267 } 268 return new Rectangle (xOffset, yOffset, rect.width, rect.height); 269 } 270 271 272 273 } 274 | Popular Tags |