1 /***
2 * ValueIndexedTableColorModel.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing.table.colormodel;
27
28 import java.awt.Color;
29 import java.util.EventListener;
30
31 import javax.swing.event.EventListenerList;
32 import javax.swing.event.TableModelEvent;
33 import javax.swing.event.TableModelListener;
34 import javax.swing.table.TableModel;
35
36 import org.jcreme.swing.table.TableColorModel;
37 import org.jcreme.swing.table.TableColorModelEvent;
38 import org.jcreme.swing.table.TableColorModelListener;
39
40 /***
41 * The purpose of this class is to have a specific background color for the
42 * lines that have a given reference column above a given Threshold value. (the
43 * value of a specified column is at a specified value).
44 *
45 * @author $Author: dbregeon $
46 * @version $Revision: 1.3 $
47 */
48 public abstract class AbstractValueIndexedTableColorModel implements
49 TableColorModel {
50 /***
51 * The underlying TableModel from which we get the values to compare to the
52 * threshold.
53 */
54 private transient TableModel tableModel = null;
55
56 /***
57 * This listener listens to changes in the reference column so that a change
58 * of the line color occurs.
59 */
60 private TableModelListener underlyingModelListener = null;
61
62 /***
63 * The default background color.
64 */
65 private Color backgroundColor = null;
66
67 /***
68 * The default foreground color.
69 */
70 private Color foregroundColor = null;
71
72 /***
73 * The background color to be used when the row is selected and threshold is
74 * exceeded.
75 */
76 private Color thresholdColor = null;
77
78 /***
79 * The background color to be used when the row is selected and the
80 * threshold is not exceeded.
81 */
82 private Color selectedBackgroundColor = null;
83
84 /***
85 * The foreground color to be used when the row is selected and the
86 * threshold is not exceeded.
87 */
88 private Color selectedForegroundColor = null;
89
90 /***
91 * The background color to be used when the threshold is exceeded for the
92 * row .
93 */
94 private Color selectedThresholdColor = null;
95
96 /***
97 * The reference column is the column whose value is monitored to determine
98 * whether the threshold is exceeded.
99 */
100 private int referenceColumn = 0;
101
102 /***
103 * The list of listeners registered for the TableColorModelChanged events.
104 */
105 private final EventListenerList listenerList = new EventListenerList();
106
107 /***
108 * Creates new ValueIndexedTableColorModel
109 */
110 public AbstractValueIndexedTableColorModel() {
111 this.underlyingModelListener = new TableModelListener() {
112 public void tableChanged(TableModelEvent evt) {
113 if ((evt.getType() == TableModelEvent.UPDATE)
114 && ((evt.getColumn() == getReferenceColumn()) || (evt
115 .getColumn() == TableModelEvent.ALL_COLUMNS))) {
116 for (int line = evt.getFirstRow(); line <= evt.getLastRow(); line++) {
117 if (testThreshold(getTableModel().getValueAt(line,
118 getReferenceColumn()))) {
119 fireTableColorModelChanged(new TableColorModelEvent(
120 AbstractValueIndexedTableColorModel.this,
121 line));
122 }
123 }
124 }
125 }
126 };
127 }
128
129 protected abstract boolean testThreshold(Object value);
130
131 protected int getReferenceColumn() {
132 return this.referenceColumn;
133 }
134
135 public TableModel getTableModel() {
136 return this.tableModel;
137 }
138
139 /***
140 * Sets the reference column whose value is monitored to determine the rows'
141 * states.
142 *
143 * @param column
144 * The index of the column to be used as a reference.
145 */
146 public void setReferenceColumn(int column) {
147 if (this.referenceColumn != column) {
148 this.referenceColumn = column;
149 fireTableColorModelChanged(new TableColorModelEvent(this));
150 }
151 }
152
153 /***
154 * Gets the current background color that the cell described by the params
155 * should have.
156 *
157 * @param row
158 * The row index of the cell.
159 * @param column
160 * The column index of the cell.
161 * @param selected
162 * Boolean telling whether the cell is selected.
163 * @param renderer
164 * the renderer that renders the cell.
165 * @return the current background color that the cell described by the
166 * params should have.
167 */
168 public Color getBackgroundColor(int row, int column, boolean selected,
169 java.awt.Component renderer) {
170 Color result = (selected ? this.selectedBackgroundColor
171 : this.backgroundColor);
172 if (testThreshold(this.tableModel.getValueAt(row,
173 this.referenceColumn))) {
174 result = (selected ? this.selectedThresholdColor
175 : this.thresholdColor);
176 }
177 return result;
178 }
179
180 /***
181 * Gets the current foreground color that the cell described by the params
182 * should have.
183 *
184 * @param row
185 * The row index of the cell.
186 * @param column
187 * The column index of the cell.
188 * @param selected
189 * Boolean telling whether the cell is selected.
190 * @param renderer
191 * the renderer that renders the cell.
192 * @return the current foreground color that the cell described by the
193 * params should have.
194 */
195 public Color getForegroundColor(int row, int column, boolean selected,
196 java.awt.Component renderer) {
197 Color result = this.foregroundColor;
198 if (selected) {
199 result = this.selectedForegroundColor;
200 }
201 return result;
202 }
203
204 /***
205 * Registers a new listener for the modifications of the color scheme.
206 *
207 * @param l
208 * The listener to be added to the list.
209 */
210 public void addTableColorModelListener(TableColorModelListener l) {
211 this.listenerList.add(TableColorModelListener.class, l);
212 }
213
214 /***
215 * Unregisters the given listener ffor the modifications of the color
216 * scheme.
217 *
218 * @param l
219 * The listener to be removed from the list.
220 */
221 public void removeTableColorModelListener(TableColorModelListener l) {
222 this.listenerList.remove(TableColorModelListener.class, l);
223 }
224
225 /***
226 * Fires a TableColorModelEvent telling registered listeners that the color
227 * scheme has changed.
228 *
229 * @param e
230 * The event describing the changes.
231 */
232 public void fireTableColorModelChanged(TableColorModelEvent e) {
233 Object[] listeners = this.listenerList.getListenerList();
234 for (int i = listeners.length - 2; i >= 0; i -= 2) {
235 if (listeners[i] == TableColorModelListener.class) {
236 ((TableColorModelListener) listeners[i + 1])
237 .tableColorModelChanged(e);
238 }
239 }
240 }
241
242 /***
243 * Gets the list of the listeners registered for the modifications of the
244 * color scheme.
245 *
246 * @param listenerType
247 * The class of listeners to retrieve.
248 * @return The list of all listeners of the given type registered in the
249 * list.
250 */
251 public EventListener[] getListeners(Class listenerType) {
252 return this.listenerList.getListeners(listenerType);
253 }
254
255
256 /***
257 * @see org.jcreme.swing.table.TableColorModel#setTableModel(javax.swing.table.TableModel)
258 */
259 public void setTableModel(TableModel model) {
260 if (this.tableModel != null) {
261 this.tableModel.removeTableModelListener(this.underlyingModelListener);
262 }
263 this.tableModel = model;
264 if (this.tableModel != null) {
265 this.tableModel.addTableModelListener(this.underlyingModelListener);
266 }
267 }
268
269
270 /***
271 * @return Returns the backgroundColor.
272 */
273 public Color getBackgroundColor() {
274 return this.backgroundColor;
275 }
276 /***
277 * @param backgroundColor The backgroundColor to set.
278 */
279 public void setBackgroundColor(Color backgroundColor) {
280 this.backgroundColor = backgroundColor;
281 }
282 /***
283 * @return Returns the foregroundColor.
284 */
285 public Color getForegroundColor() {
286 return this.foregroundColor;
287 }
288 /***
289 * @param foregroundColor The foregroundColor to set.
290 */
291 public void setForegroundColor(Color foregroundColor) {
292 this.foregroundColor = foregroundColor;
293 }
294 /***
295 * @return Returns the selectedBackgroundColor.
296 */
297 public Color getSelectedBackgroundColor() {
298 return this.selectedBackgroundColor;
299 }
300 /***
301 * @param selectedBackgroundColor The selectedBackgroundColor to set.
302 */
303 public void setSelectedBackgroundColor(Color selectedBackgroundColor) {
304 this.selectedBackgroundColor = selectedBackgroundColor;
305 }
306 /***
307 * @return Returns the selectedForegroundColor.
308 */
309 public Color getSelectedForegroundColor() {
310 return this.selectedForegroundColor;
311 }
312 /***
313 * @param selectedForegroundColor The selectedForegroundColor to set.
314 */
315 public void setSelectedForegroundColor(Color selectedForegroundColor) {
316 this.selectedForegroundColor = selectedForegroundColor;
317 }
318 /***
319 * @return Returns the selectedThresholdColor.
320 */
321 public Color getSelectedThresholdColor() {
322 return this.selectedThresholdColor;
323 }
324 /***
325 * @param selectedThresholdColor The selectedThresholdColor to set.
326 */
327 public void setSelectedThresholdColor(Color selectedThresholdColor) {
328 this.selectedThresholdColor = selectedThresholdColor;
329 }
330 /***
331 * @return Returns the thresholdColor.
332 */
333 public Color getThresholdColor() {
334 return this.thresholdColor;
335 }
336 /***
337 * @param thresholdColor The thresholdColor to set.
338 */
339 public void setThresholdColor(Color thresholdColor) {
340 this.thresholdColor = thresholdColor;
341 }
342 }