1 package com.explosion.utilities.preferences.editandrender.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Color;
24 import java.awt.Component;
25
26 import javax.swing.BorderFactory;
27 import javax.swing.JLabel;
28 import javax.swing.JTable;
29 import javax.swing.border.Border;
30 import javax.swing.table.TableCellRenderer;
31
32 import com.explosion.utilities.preferences.Preference;
33
34 public class ColorPreferenceRenderer extends JLabel implements TableCellRenderer
35 {
36
37 Border unselectedBorder = null;
38
39 Border selectedBorder = null;
40
41 boolean isBordered = true;
42
43 public ColorPreferenceRenderer(boolean isBordered)
44 {
45 super();
46 this.isBordered = isBordered;
47 setOpaque(true);
48 }
49
50 public Component getTableCellRendererComponent(JTable table, Object prefrence, boolean isSelected, boolean hasFocus, int row, int column)
51 {
52
53 Preference pref = (Preference) prefrence;
54 Object renderableValue = pref.getValue();
55 if (renderableValue == null && pref.getDefaultValue() != null) renderableValue = pref.getDefaultValue();
56
57 if (renderableValue == null)
58 setBackground(Color.white);
59 else
60 setBackground((Color) renderableValue);
61
62 if (isBordered)
63 {
64 if (isSelected)
65 {
66 if (selectedBorder == null)
67 {
68 selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getSelectionBackground());
69 }
70 setBorder(selectedBorder);
71 } else
72 {
73 if (unselectedBorder == null)
74 {
75 unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getBackground());
76 }
77 setBorder(unselectedBorder);
78 }
79 }
80 return this;
81 }
82 }