1 | |
package us.daveread.basicquery.gui; |
2 | |
|
3 | |
import java.awt.Component; |
4 | |
import java.awt.Color; |
5 | |
import java.util.Map; |
6 | |
import java.util.HashMap; |
7 | |
import java.util.List; |
8 | |
import java.util.ArrayList; |
9 | |
import javax.swing.JLabel; |
10 | |
import javax.swing.BorderFactory; |
11 | |
import javax.swing.JTable; |
12 | |
import javax.swing.table.TableCellRenderer; |
13 | |
|
14 | |
import org.apache.log4j.Logger; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class ColoredCellRenderer extends JLabel implements TableCellRenderer { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
private static final long serialVersionUID = -2456625376274110406L; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
@SuppressWarnings("unused") |
37 | 1 | private static final Logger LOGGER = Logger |
38 | |
.getLogger(ColoredCellRenderer.class); |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
private Map<String, Color> mapcFGPatterns; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private Map<String, Color> mapcBGPatterns; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private List<Color> objcAlternatingBGColors; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private List<Color> objcAlternatingFGColors; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private JTable objcTable; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public ColoredCellRenderer() { |
71 | 6 | this(JLabel.LEFT, JLabel.CENTER); |
72 | 6 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public ColoredCellRenderer(int iaHorizontalAlignment) { |
82 | 1 | this(iaHorizontalAlignment, JLabel.CENTER); |
83 | 1 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 8 | public ColoredCellRenderer(int iaHorizAlignment, int iaVerticalAlignment) { |
94 | 8 | setOpaque(true); |
95 | 8 | setHorizontalAlignment(iaHorizAlignment); |
96 | 8 | setVerticalAlignment(iaVerticalAlignment); |
97 | |
|
98 | 8 | mapcFGPatterns = new HashMap<String, Color>(); |
99 | 8 | mapcBGPatterns = new HashMap<String, Color>(); |
100 | 8 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public void addAlternatingRowColor(Color objaTextColor, Color objaBGColor) { |
114 | 3 | if (objcAlternatingBGColors == null) { |
115 | 2 | objcAlternatingBGColors = new ArrayList<Color>(); |
116 | |
} |
117 | |
|
118 | 3 | if (objcAlternatingFGColors == null) { |
119 | 2 | objcAlternatingFGColors = new ArrayList<Color>(); |
120 | |
} |
121 | |
|
122 | 3 | objcAlternatingBGColors.add(objaBGColor); |
123 | 3 | objcAlternatingFGColors.add(objaTextColor); |
124 | 3 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public void setDefaultColors(Color objaTextColor, Color objaBackgroundColor) { |
135 | 1 | objcAlternatingBGColors = new ArrayList<Color>(); |
136 | 1 | objcAlternatingBGColors.add(objaBackgroundColor); |
137 | |
|
138 | 1 | objcAlternatingFGColors = new ArrayList<Color>(); |
139 | 1 | objcAlternatingFGColors.add(objaTextColor); |
140 | 1 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public void clearColors() { |
146 | 2 | objcAlternatingBGColors = null; |
147 | 2 | objcAlternatingFGColors = null; |
148 | 2 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public void addPattern(String saPattern, Color objaTextColor, |
164 | |
Color objaBackgroundColor) { |
165 | 1 | mapcFGPatterns.put(saPattern, objaTextColor); |
166 | 1 | mapcBGPatterns.put(saPattern, objaBackgroundColor); |
167 | 1 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
private Color findBGColor(String saText, int row) { |
182 | |
Color objlMatchedColor; |
183 | |
|
184 | 13 | objlMatchedColor = (Color) mapcBGPatterns.get(saText); |
185 | |
|
186 | 13 | if (objlMatchedColor == null && objcAlternatingBGColors != null) { |
187 | 10 | objlMatchedColor = (Color) objcAlternatingBGColors.get(row |
188 | |
% objcAlternatingBGColors.size()); |
189 | |
} |
190 | |
|
191 | 13 | return objlMatchedColor; |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private Color findFGColor(String saText, int row) { |
207 | |
Color objlMatchedColor; |
208 | |
|
209 | 13 | objlMatchedColor = (Color) mapcFGPatterns.get(saText); |
210 | |
|
211 | 13 | if (objlMatchedColor == null && objcAlternatingFGColors != null) { |
212 | 10 | objlMatchedColor = (Color) objcAlternatingFGColors.get(row |
213 | |
% objcAlternatingFGColors.size()); |
214 | |
} |
215 | |
|
216 | 13 | return objlMatchedColor; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public Component getTableCellRendererComponent(JTable table, Object value, |
240 | |
boolean isSelected, |
241 | |
boolean hasFocus, int row, |
242 | |
int column) { |
243 | |
|
244 | 13 | if (objcTable == null) { |
245 | 3 | objcTable = table; |
246 | |
} |
247 | |
|
248 | 13 | if (value != null) { |
249 | 11 | setText(value.toString()); |
250 | |
} else { |
251 | 2 | setText(""); |
252 | |
} |
253 | |
|
254 | 13 | setColor(table, isSelected, hasFocus, row, column); |
255 | |
|
256 | 13 | return this; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private void setColor(JTable table, boolean isSelected, boolean hasFocus, |
274 | |
int row, int col) { |
275 | |
|
276 | |
Color objlBackgroundColor; |
277 | |
Color objlForegroundColor; |
278 | |
|
279 | 13 | objlBackgroundColor = findBGColor(getText(), row); |
280 | 13 | objlForegroundColor = findFGColor(getText(), row); |
281 | |
|
282 | 13 | if (objlForegroundColor != null) { |
283 | 10 | setForeground(objlForegroundColor); |
284 | |
} else { |
285 | 3 | setForeground(table.getForeground()); |
286 | |
} |
287 | |
|
288 | 13 | if (objlBackgroundColor != null) { |
289 | 10 | setBorder(BorderFactory.createLineBorder(objlBackgroundColor, 2)); |
290 | 10 | setBackground(objlBackgroundColor); |
291 | |
} else { |
292 | 3 | setBorder(BorderFactory.createLineBorder(table.getBackground(), 2)); |
293 | 3 | setBackground(table.getBackground()); |
294 | |
} |
295 | |
|
296 | 13 | if (isSelected && !hasFocus) { |
297 | 2 | setBorder(BorderFactory.createLineBorder(table.getSelectionBackground(), |
298 | |
2)); |
299 | 2 | setBackground(table.getSelectionBackground()); |
300 | 2 | setForeground(table.getSelectionForeground()); |
301 | 11 | } else if (isSelected) { |
302 | 3 | setBorder(BorderFactory.createLineBorder(table.getSelectionBackground(), |
303 | |
2)); |
304 | 3 | setBackground(table.getBackground()); |
305 | 3 | setForeground(table.getForeground()); |
306 | |
} |
307 | 13 | } |
308 | |
} |