1 package net.logAnalyzer.gui.messages.cellrenderers;
2
3 import java.awt.Component;
4 import java.awt.Font;
5 import java.util.HashMap;
6 import java.util.Iterator;
7
8 import javax.swing.Icon;
9 import javax.swing.JLabel;
10 import javax.swing.JTable;
11 import javax.swing.UIManager;
12 import javax.swing.border.Border;
13 import javax.swing.border.EmptyBorder;
14 import javax.swing.table.TableCellRenderer;
15
16 /***
17 * This class defines a cell renderer displaying an image linked to the cell
18 * value.
19 *
20 * @author Karim REFEYTON
21 * @version 0.1
22 */
23 public abstract class ImageKeyCellRender extends JLabel implements
24 TableCellRenderer {
25 private static final long serialVersionUID = 1L;
26
27 /***
28 * Default border.
29 */
30 public static final Border NOFOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
31
32 /***
33 * Map of images.
34 */
35 private HashMap icons;
36
37 /***
38 * If <tt>true</tt>, the cell value must strictly equals a key to display
39 * an image; otherwise, the start of the cell value is compare to the keys.
40 */
41 private boolean exactMatch;
42
43 /***
44 * <tt>true</tt> if text is rendered.
45 */
46 private boolean displayText;
47
48 /***
49 * Constructs a new renderer. To render a cell, its value is used as the
50 * image key to render.
51 *
52 * @param icons
53 * images to render.
54 * @param keys
55 * images keys.
56 * @param exactMatch
57 * If <tt>true</tt>, the cell value must strictly equals a key
58 * to display an image (fastest method); otherwise, the start of
59 * the cell value is compare to the keys (slowest method).
60 * @param displayText
61 * Renders the value if <tt>true</tt>.
62 */
63 public ImageKeyCellRender(Icon[] icons, String[] keys, boolean exactMatch,
64 boolean displayText) {
65 super();
66 this.icons = new HashMap();
67 for (int i = 0; i < keys.length; i++) {
68 this.icons.put(keys[i], icons[i]);
69 }
70 this.exactMatch = exactMatch;
71 this.displayText = displayText;
72 Font font = getFont();
73 setFont(font.deriveFont(Font.PLAIN));
74 setOpaque(true);
75 }
76
77 /***
78 * Sets text, height, borders and colors of the cell regarding to value,
79 * selection and focus.
80 *
81 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
82 * java.lang.Object, boolean, boolean, int, int)
83 */
84 public Component getTableCellRendererComponent(JTable table, Object value,
85 boolean isSelected, boolean hasFocus, int row, int column) {
86
87 if (displayText && value != null) {
88 setText(value.toString());
89 } else {
90 setText(null);
91 if (value != null) {
92 setToolTipText(value.toString());
93 }
94 }
95
96
97 Icon icon = null;
98 if (value != null) {
99 icon = (Icon) icons.get(value.toString());
100 if (icon == null && !exactMatch) {
101 for (Iterator keys = icons.keySet().iterator(); keys.hasNext();) {
102 String key = (String) keys.next();
103 if (value.toString().startsWith(key)) {
104 icon = (Icon) icons.get(key);
105 }
106 }
107 }
108 }
109 setIcon(icon);
110
111
112 if (isSelected) {
113 super.setForeground(table.getSelectionForeground());
114 super.setBackground(table.getSelectionBackground());
115 } else {
116 super.setForeground(table.getForeground());
117 super.setBackground(table.getBackground());
118 }
119 if (hasFocus) {
120 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
121 if (table.isCellEditable(row, column)) {
122 super.setForeground(UIManager
123 .getColor("Table.focusCellForeground"));
124 super.setBackground(UIManager
125 .getColor("Table.focusCellBackground"));
126 }
127 } else {
128 setBorder(NOFOCUS_BORDER);
129 }
130 return this;
131 }
132 }