2
* $Id: TableRendererTest.java,v 1.8 2007/08/23 13:54:29 kleopatra Exp $
4
* Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx.renderer;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.io.Serializable;
27
import java.text.DateFormat;
28
import java.util.Date;
29
import java.util.logging.Logger;
31
import javax.swing.Icon;
32
import javax.swing.ImageIcon;
33
import javax.swing.JComponent;
34
import javax.swing.JLabel;
35
import javax.swing.JTable;
36
import javax.swing.UIManager;
37
import javax.swing.border.Border;
38
import javax.swing.table.DefaultTableCellRenderer;
39
import javax.swing.table.DefaultTableModel;
40
import javax.swing.table.TableCellRenderer;
41
import javax.swing.table.TableModel;
43
import org.jdesktop.swingx.InteractiveTestCase;
44
import org.jdesktop.swingx.JXTable;
45
import org.jdesktop.swingx.test.XTestUtils;
46
import org.jdesktop.test.SerializableSupport;
49
* Tests behaviour of SwingX renderers. Currently: mostly characterization to
50
* guarantee that they behave similar to the standard.
52
* @author Jeanette Winzenburg
54
public class TableRendererTest extends InteractiveTestCase {
56
private static final Logger LOG = Logger.getLogger(TableRendererTest.class
60
private int coreColumn;
61
private DefaultTableCellRenderer coreTableRenderer;
63
private DefaultTableRenderer xTableRenderer;
67
protected void setUp() throws Exception {
69
table = new JTable(10, 2);
71
coreTableRenderer = new DefaultTableCellRenderer();
72
table.getColumnModel().getColumn(coreColumn).setCellRenderer(coreTableRenderer);
74
xTableRenderer = new DefaultTableRenderer();
75
table.getColumnModel().getColumn(xColumn).setCellRenderer(xTableRenderer);
79
* Test constructors: here convenience with alignment and converter
82
public void testConstructor() {
83
FormatStringValue sv = new FormatStringValue(DateFormat.getTimeInstance());
84
int align = JLabel.RIGHT;
85
DefaultTableRenderer renderer = new DefaultTableRenderer(sv, align);
86
assertEquals(sv, renderer.componentController.getToStringConverter());
87
assertEquals(align, renderer.componentController.getHorizontalAlignment());
90
* test if icon handling is the same for core default and
94
public void testIcon() {
95
TableModel model = createTableModelWithDefaultTypes();
98
assertTrue(Icon.class.isAssignableFrom(model.getColumnClass(iconColumn)));
99
Icon icon = (Icon) model.getValueAt(0, iconColumn);
100
// default uses a different class for icon rendering
101
DefaultTableCellRenderer coreIconRenderer = (DefaultTableCellRenderer) table.getDefaultRenderer(Icon.class);
102
// core default can't cope with null component - can't really compare behaviour
103
coreIconRenderer.getTableCellRendererComponent(table, icon, false, false, -1, -1);
104
assertEquals(icon, coreIconRenderer.getIcon());
105
assertEquals("", coreIconRenderer.getText());
106
JXTable xTable = new JXTable();
107
TableCellRenderer xIconRenderer = xTable.getDefaultRenderer(Icon.class);
108
JLabel label = (JLabel) xIconRenderer.getTableCellRendererComponent(null, icon, false, false, -1, -1);
109
assertEquals(icon, label.getIcon());
110
assertEquals("", label.getText());
111
// wrong assumption after fix of #591-swingx - default icon renderer
112
// no longer tries to be clever
113
// label = (JLabel) xIconRenderer.getTableCellRendererComponent(null, "dummy", false, false, -1, -1);
114
// assertNull(label.getIcon());
115
// assertEquals("dummy", label.getText());
120
private TableModel createTableModelWithDefaultTypes() {
121
String[] names = {"Object", "Number", "Double", "Date", "ImageIcon", "Boolean"};
122
final Class[] types = {Object.class, Number.class, Double.class, Date.class, ImageIcon.class, Boolean.class};
123
DefaultTableModel model = new DefaultTableModel(names, 0) {
126
public Class<?> getColumnClass(int columnIndex) {
127
return types[columnIndex];
131
Date today = new Date();
132
Icon icon = XTestUtils.loadDefaultIcon();
133
for (int i = 0; i < 10; i++) {
134
Object[] values = new Object[] {"row " + i, i, Math.random() * 100,
135
new Date(today.getTime() + i * 1000000), icon, i % 2 == 0};
136
model.addRow(values);
144
* test serializable of default renderer.
147
public void testSerializeTableRenderer() {
148
TableCellRenderer xListRenderer = new DefaultTableRenderer();
150
SerializableSupport.serialize(xListRenderer);
151
} catch (Exception e) {
152
fail("not serializable " + e);
158
* base interaction with table: focused, selected uses UI border.
160
public void testTableFocusSelectedBorder() {
161
// sanity to see test test validity
162
// UIManager.put("Table.focusSelectedCellHighlightBorder", new LineBorder(Color.red));
164
Border selectedFocusBorder = getFocusBorder(true);
166
if (selectedFocusBorder == null) {
167
LOG.info("cannot run focusSelectedBorder - UI has no selected focus border");
171
// need to prepare directly - focus is true only if table is focusowner
172
JComponent coreComponent = (JComponent) coreTableRenderer.getTableCellRendererComponent(table,
173
null, true, true, 0, coreColumn);
174
// sanity: known standard behaviour
175
assertEquals(selectedFocusBorder, coreComponent.getBorder());
177
JComponent xComponent = (JComponent) xTableRenderer.getTableCellRendererComponent(table,
178
null, true, true, 0, xColumn);
179
// assert behaviour same as standard
180
assertEquals(coreComponent.getBorder(), xComponent.getBorder());
183
private Border getFocusBorder(boolean lookup) {
184
Border selectedFocusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
185
if (lookup && (selectedFocusBorder == null)) {
186
selectedFocusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
188
return selectedFocusBorder;
192
* base interaction with table: focused, not-selected uses UI border.
196
public void testTableFocusBorder() {
198
Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
199
// Border selectedFocusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
201
assertNotNull(focusBorder);
202
// need to prepare directly - focus is true only if table is focusowner
203
JComponent coreComponent = (JComponent) coreTableRenderer.getTableCellRendererComponent(table,
204
null, false, true, 0, coreColumn);
205
// sanity: known standard behaviour
206
assertEquals(focusBorder, coreComponent.getBorder());
208
JComponent xComponent = (JComponent) xTableRenderer.getTableCellRendererComponent(table,
209
null, false, true, 0, xColumn);
210
// assert behaviour same as standard
211
assertEquals(coreComponent.getBorder(), xComponent.getBorder());
214
* base interaction with table: focused, not-selected and editable
218
public void testTableRendererExtFocusedNotSelectedEditable() {
220
assertTrue(table.isCellEditable(0, coreColumn));
222
Color uiForeground = UIManager.getColor("Table.focusCellForeground");
223
Color uiBackground = UIManager.getColor("Table.focusCellBackground");
225
assertNotNull(uiForeground);
226
assertNotNull(uiBackground);
227
Color background = Color.MAGENTA;
228
Color foreground = Color.YELLOW;
230
coreTableRenderer.setBackground(background);
231
coreTableRenderer.setForeground(foreground);
232
// need to prepare directly - focus is true only if table is focusowner
233
Component coreComponent = coreTableRenderer.getTableCellRendererComponent(table,
234
null, false, true, 0, coreColumn);
235
// sanity: known standard behaviour
236
assertEquals(uiBackground, coreComponent.getBackground());
237
assertEquals(uiForeground, coreComponent.getForeground());
239
xTableRenderer.setBackground(background);
240
xTableRenderer.setForeground(foreground);
241
Component xComponent = xTableRenderer.getTableCellRendererComponent(table,
242
null, false, true, 0, xColumn);
243
// assert behaviour same as standard
244
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
245
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
249
* base interaction with table: custom color of renderer precedes
253
public void testTableRendererExtCustomColor() {
254
Color background = Color.MAGENTA;
255
Color foreground = Color.YELLOW;
257
coreTableRenderer.setBackground(background);
258
coreTableRenderer.setForeground(foreground);
259
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
260
// sanity: known standard behaviour
261
assertEquals(background, coreComponent.getBackground());
262
assertEquals(foreground, coreComponent.getForeground());
264
xTableRenderer.setBackground(background);
265
xTableRenderer.setForeground(foreground);
266
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
267
// assert behaviour same as standard
268
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
269
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
273
* base interaction with table: renderer uses table's selection color.
276
public void testTableRendererExtSelectedColors() {
278
table.setRowSelectionInterval(0, 0);
280
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
281
// sanity: known standard behaviour
282
assertEquals(table.getSelectionBackground(), coreComponent.getBackground());
283
assertEquals(table.getSelectionForeground(), coreComponent.getForeground());
285
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
286
// assert behaviour same as standard
287
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
288
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
292
* base interaction with table: renderer uses table's custom selection color.
295
public void testTableRendererExtTableSelectedColors() {
296
Color background = Color.MAGENTA;
297
Color foreground = Color.YELLOW;
298
table.setSelectionBackground(background);
299
table.setSelectionForeground(foreground);
301
table.setRowSelectionInterval(0, 0);
303
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
304
// sanity: known standard behaviour
305
assertEquals(table.getSelectionBackground(), coreComponent.getBackground());
306
assertEquals(table.getSelectionForeground(), coreComponent.getForeground());
308
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
309
// assert behaviour same as standard
310
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
311
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
315
* base interaction with table: renderer uses table's unselected colors.
318
public void testTableRendererExtColors() {
320
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
321
// sanity: known standard behaviour
322
assertEquals(table.getBackground(), coreComponent.getBackground());
323
assertEquals(table.getForeground(), coreComponent.getForeground());
325
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
326
// assert behaviour same as standard
327
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
328
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
332
* base interaction with table: renderer uses table's unselected custom colors
336
public void testTableRendererExtTableColors() {
337
Color background = Color.MAGENTA;
338
Color foreground = Color.YELLOW;
339
table.setBackground(background);
340
table.setForeground(foreground);
342
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
343
// sanity: known standard behaviour
344
assertEquals(table.getBackground(), coreComponent.getBackground());
345
assertEquals(table.getForeground(), coreComponent.getForeground());
347
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
348
// assert behaviour same as standard
349
assertEquals(coreComponent.getBackground(), xComponent.getBackground());
350
assertEquals(coreComponent.getForeground(), xComponent.getForeground());
354
* characterize opaqueness of rendering components.
357
public void testTableOpaqueRenderer() {
359
assertFalse(new JLabel().isOpaque());
360
assertTrue(coreTableRenderer.isOpaque());
361
// assertTrue(xTableRenderer.getRendererComponent().isOpaque());
365
* characterize opaqueness of rendering components.
367
* that's useless: the opaque magic only applies if parent != null
369
public void testTableOpaqueRendererComponent() {
371
assertFalse(new JLabel().isOpaque());
372
Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
374
assertTrue(coreComponent.isOpaque());
375
Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
376
assertTrue(xComponent.isOpaque());
381
* base existence/type tests while adding DefaultTableCellRendererExt.
384
public void testTableRendererExt() {
385
DefaultTableRenderer renderer = new DefaultTableRenderer();
386
assertTrue(renderer instanceof TableCellRenderer);
387
assertTrue(renderer instanceof Serializable);