1 package net.logAnalyzer.gui.messages.cellrenderers;
2
3 import java.awt.Component;
4 import java.awt.Font;
5
6 import javax.swing.Icon;
7 import javax.swing.JLabel;
8 import javax.swing.JTable;
9 import javax.swing.UIManager;
10 import javax.swing.border.Border;
11 import javax.swing.border.EmptyBorder;
12 import javax.swing.table.TableCellRenderer;
13
14 /***
15 * This class defines a cell renderer displaying an image linked to the cell
16 * value.
17 *
18 * @author Karim REFEYTON
19 * @version 0.1
20 */
21 public abstract class ImageTextCellRenderer extends JLabel implements
22 TableCellRenderer {
23 private static final long serialVersionUID = 1L;
24
25 /***
26 * Default border.
27 */
28 protected static final Border NOFOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
29
30 /***
31 * Image to display.
32 */
33 private Icon icon;
34
35 /***
36 * Constructs a new renderer displaying an image at the lefet of the value.
37 *
38 * @param icon
39 * image to render.
40 */
41 public ImageTextCellRenderer(Icon icon) {
42 super();
43 this.icon = icon;
44 Font font = getFont();
45 setFont(font.deriveFont(Font.PLAIN));
46 setOpaque(true);
47 }
48
49 /***
50 * Sets text, height, borders and colors of the cell regarding to value,
51 * selection and focus.
52 *
53 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
54 * java.lang.Object, boolean, boolean, int, int)
55 */
56 public Component getTableCellRendererComponent(JTable table, Object value,
57 boolean isSelected, boolean hasFocus, int row, int column) {
58
59 if (value != null) {
60 setText(value.toString());
61 }
62 if (value != null && !value.toString().trim().equals("")) {
63
64 setIcon(icon);
65 } else {
66 setIcon(null);
67 }
68
69
70 if (isSelected) {
71 super.setForeground(table.getSelectionForeground());
72 super.setBackground(table.getSelectionBackground());
73 } else {
74 super.setForeground(table.getForeground());
75 super.setBackground(table.getBackground());
76 }
77 if (hasFocus) {
78 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
79 if (table.isCellEditable(row, column)) {
80 super.setForeground(UIManager
81 .getColor("Table.focusCellForeground"));
82 super.setBackground(UIManager
83 .getColor("Table.focusCellBackground"));
84 }
85 } else {
86 setBorder(NOFOCUS_BORDER);
87 }
88 return this;
89 }
90 }