Source code: dexter/swingExtensions/sortedTable/StateToComponentCellRenderer.java
1 /*
2 * StateToComponentCellRenderer.java
3 *
4 * Created on October 27, 2002, 9:30 PM
5 */
6
7 package dexter.swingExtensions.sortedTable;
8
9
10 import javax.swing.table.*;
11 import java.awt.*;
12 import javax.swing.*;
13 import java.util.*;
14
15 /**
16 * <p>Title: </p>
17 * <p>Description: </p>
18 * <p>Copyright: Copyright (c) 2001</p>
19 * <p>Company: </p>
20 * @author unascribed
21 * @version 1.0
22 */
23
24 public class StateToComponentCellRenderer implements TableCellRenderer {
25
26 private Map componentMap;
27
28 public StateToComponentCellRenderer() {
29 componentMap = new HashMap();
30 }
31
32 public void addMapping(Object key, Component comp)
33 {
34 componentMap.put(key, comp);
35 }
36
37 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
38
39 Component comp;
40
41 if (componentMap.containsKey(value))
42 {
43 comp = (Component)(componentMap.get(value));
44 }
45 else
46 {
47 if (value!=null) comp = new JLabel(value.toString());
48 else comp = new JLabel("");
49 }
50
51 return refineComponent(comp, table, isSelected, hasFocus);
52 }
53
54 protected Component refineComponent(Component comp, JTable table, boolean isSelected, boolean hasFocus)
55 {
56 if (isSelected) {
57 comp.setForeground(table.getSelectionForeground());
58 comp.setBackground(table.getSelectionBackground());
59 }
60 else {
61 comp.setForeground(table.getForeground());
62 comp.setBackground(table.getBackground());
63 }
64
65 comp.setFont(table.getFont());
66
67 // if (hasFocus) {
68 // comp.setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
69 // if (table.isCellEditable(row, column)) {
70 // comp.setForeground( UIManager.getColor("Table.focusCellForeground") );
71 // comp.setBackground( UIManager.getColor("Table.focusCellBackground") );
72 // }
73 // } else {
74 // setBorder(noFocusBorder);
75 // }
76
77 // ---- begin optimization to avoid painting background ----
78 // Color back = comp.getBackground();
79 // boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
80 // comp.setOpaque(!colorMatch);
81 // ---- end optimization to aviod painting background ----
82
83 return (Component)comp;
84 }
85 }