1 /***
2 * JColoredTable.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;
27
28 import java.awt.Color;
29 import java.awt.Component;
30 import java.awt.Rectangle;
31 import java.util.Vector;
32
33 import javax.swing.JTable;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.table.DefaultTableCellRenderer;
36 import javax.swing.table.TableCellRenderer;
37 import javax.swing.table.TableColumnModel;
38 import javax.swing.table.TableModel;
39
40 /***
41 * This subclass of JTable enable to implement a coloring scheme for the whole
42 * table (not based on the renderers). The purpose is to provide a coloring
43 * scheme that is consistent from the table point of view. A modification of the
44 * renderers would have reduced the use to cell only color schemes. Instances of
45 * this class should not be opaque.
46 *
47 * @author $Author: dbregeon $
48 * @version $Revision: 1.2 $
49 */
50 public class JColoredTable extends JTable implements TableColorModelListener {
51 /***
52 * the color model used by this instance.
53 */
54 private TableColorModel tableColorModel = null;
55
56 /***
57 * Creates new JColoredTable.
58 *
59 */
60 public JColoredTable() {
61
62 }
63
64 /***
65 * Creates new JColoredTable
66 *
67 * @param numRows
68 * the number of rows in the model.
69 * @param numColumns
70 * the number of columns in the model.
71 */
72 public JColoredTable(int numRows, int numColumns) {
73 super(numRows, numColumns);
74 }
75
76 /***
77 * Creates new JColoredTable
78 *
79 * @param rowData
80 * the values that compose the model.
81 * @param columnNames
82 * the names of the columns of the model.
83 */
84 public JColoredTable(Object[][] rowData, String[] columnNames) {
85 super(rowData, columnNames);
86 }
87
88 /***
89 * Creates new JColoredTable
90 *
91 * @param dm
92 * the underlying model.
93 */
94 public JColoredTable(TableModel dm) {
95 super(dm);
96 }
97
98 /***
99 * Creates new JColoredTable
100 *
101 * @param dm
102 * the underlying model.
103 * @param cm
104 * the column model.
105 */
106 public JColoredTable(TableModel dm, TableColumnModel cm) {
107 super(dm, cm);
108 }
109
110 /***
111 * Creates new JColoredTable
112 *
113 * @param dm
114 * the underlying model.
115 * @param cm
116 * the column model.
117 * @param sm
118 * the selection model.
119 */
120 public JColoredTable(TableModel dm, TableColumnModel cm,
121 ListSelectionModel sm) {
122 super(dm, cm, sm);
123 }
124
125 /***
126 * Creates new JColoredTable
127 *
128 * @param rowData
129 * the values that compose the model.
130 * @param columnNames
131 * the names of the columns of the model.
132 */
133 public JColoredTable(Vector rowData, Vector columnNames) {
134 super(rowData, columnNames);
135 }
136
137 /***
138 * Sets the color model. The JColoredTable also becomes a listener of that
139 * model.
140 *
141 * @param model
142 * the color model that this table will use.
143 */
144 public void setTableColorModel(TableColorModel model) {
145 TableColorModel oldValue = this.tableColorModel;
146 if (oldValue != null) {
147 oldValue.removeTableColorModelListener(this);
148 oldValue.setTableModel(null);
149 }
150 this.tableColorModel = model;
151 if (this.tableColorModel != null) {
152 this.tableColorModel.setTableModel(getModel());
153
154
155
156
157
158
159 setOpaque(false);
160 this.tableColorModel.addTableColorModelListener(this);
161 } else {
162 setOpaque(true);
163 }
164 firePropertyChange("TableColorModel", oldValue, model);
165 repaint();
166 }
167
168 /***
169 * This method gives access to the color model.
170 *
171 * @return the current color model.
172 */
173 public TableColorModel getTableColorModel() {
174 return this.tableColorModel;
175 }
176
177 /***
178 * This is the method where the color scheme is applied. After the renderer
179 * has been prepared, we change its color based on the values returned by
180 * the color model of the table.
181 *
182 * @param renderer
183 * the cell renderer.
184 * @param row
185 * the cell row.
186 * @param column
187 * the cell column.
188 * @return the renderer for the given row and column.
189 */
190 public Component prepareRenderer(TableCellRenderer renderer, int row,
191 int column) {
192 if (renderer instanceof DefaultTableCellRenderer) {
193 ((DefaultTableCellRenderer) renderer).setBackground(null);
194 ((DefaultTableCellRenderer) renderer).setForeground(null);
195 }
196 Component preparedRenderer = super.prepareRenderer(renderer, row,
197 column);
198 if (this.tableColorModel != null) {
199 Color color = this.tableColorModel.getBackgroundColor(row, column,
200 isCellSelected(row, column), preparedRenderer);
201 if (color != null) {
202 preparedRenderer.setBackground(color);
203 }
204 color = this.tableColorModel.getForegroundColor(row, column,
205 isCellSelected(row, column), preparedRenderer);
206 if (color != null) {
207 preparedRenderer.setForeground(color);
208 }
209 }
210 return preparedRenderer;
211 }
212
213 /***
214 * This method is called when a TableColorModelEvent is received. It forces
215 * the repaint of the parts of the table that are interested in the change.
216 *
217 * @param event
218 * the event.
219 */
220 public void tableColorModelChanged(TableColorModelEvent event) {
221 if ((event != null) && (event.getSource() == this.tableColorModel)) {
222 int minRow = 0;
223 int maxRow = getRowCount() - 1;
224 int minColumn = 0;
225 int maxColumn = getColumnCount() - 1;
226
227 if ((event.getMinRow() == TableColorModelEvent.ALL_ROWS)
228 && (event.getColumn() == TableColorModelEvent.ALL_COLUMNS)) {
229 repaint();
230 } else {
231 if (event.getMinRow() != TableColorModelEvent.ALL_ROWS) {
232 minRow = event.getMinRow();
233 maxRow = event.getMaxRow();
234 }
235 if (event.getColumn() != TableColorModelEvent.ALL_COLUMNS) {
236 minColumn = event.getColumn();
237 maxColumn = minColumn;
238 }
239 Rectangle rect = new Rectangle();
240 for (int i = minRow; i < maxRow + 1; i++) {
241 for (int j = minColumn; j < maxColumn + 1; j++) {
242 rect.add(getCellRect(i, j, false));
243 }
244 }
245 repaint(rect);
246 }
247 }
248 }
249 }