001: /*
002: * $Id: TableRendererTest.java,v 1.5 2007/01/25 11:23:41 kleopatra Exp $
003: *
004: * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: *
021: */
022: package org.jdesktop.swingx.renderer;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.io.Serializable;
027: import java.util.Date;
028: import java.util.logging.Logger;
029:
030: import javax.swing.Icon;
031: import javax.swing.ImageIcon;
032: import javax.swing.JComponent;
033: import javax.swing.JLabel;
034: import javax.swing.JTable;
035: import javax.swing.UIDefaults;
036: import javax.swing.UIManager;
037: import javax.swing.border.Border;
038: import javax.swing.table.DefaultTableCellRenderer;
039: import javax.swing.table.DefaultTableModel;
040: import javax.swing.table.TableCellRenderer;
041: import javax.swing.table.TableModel;
042:
043: import org.jdesktop.swingx.InteractiveTestCase;
044: import org.jdesktop.swingx.JXTable;
045: import org.jdesktop.test.SerializableSupport;
046:
047: /**
048: * Tests behaviour of SwingX renderers. Currently: mostly characterization to
049: * guarantee that they behave similar to the standard.
050: *
051: * @author Jeanette Winzenburg
052: */
053: public class TableRendererTest extends InteractiveTestCase {
054:
055: private static final Logger LOG = Logger
056: .getLogger(TableRendererTest.class.getName());
057:
058: private JTable table;
059: private int coreColumn;
060: private DefaultTableCellRenderer coreTableRenderer;
061: private int xColumn;
062: private DefaultTableRenderer xTableRenderer;
063:
064: @Override
065: protected void setUp() throws Exception {
066: // setup table
067: table = new JTable(10, 2);
068: coreColumn = 0;
069: coreTableRenderer = new DefaultTableCellRenderer();
070: table.getColumnModel().getColumn(coreColumn).setCellRenderer(
071: coreTableRenderer);
072: xColumn = 1;
073: xTableRenderer = new DefaultTableRenderer();
074: table.getColumnModel().getColumn(xColumn).setCellRenderer(
075: xTableRenderer);
076: }
077:
078: /**
079: * test if icon handling is the same for core default and
080: * swingx.
081: *
082: */
083: public void testIcon() {
084: TableModel model = createTableModelWithDefaultTypes();
085: int iconColumn = 4;
086: // sanity
087: assertTrue(Icon.class.isAssignableFrom(model
088: .getColumnClass(iconColumn)));
089: Icon icon = (Icon) model.getValueAt(0, iconColumn);
090: // default uses a different class for icon rendering
091: DefaultTableCellRenderer coreIconRenderer = (DefaultTableCellRenderer) table
092: .getDefaultRenderer(Icon.class);
093: // core default can't cope with null component - can't really compare behaviour
094: coreIconRenderer.getTableCellRendererComponent(table, icon,
095: false, false, -1, -1);
096: assertEquals(icon, coreIconRenderer.getIcon());
097: assertEquals("", coreIconRenderer.getText());
098: JLabel label = (JLabel) xTableRenderer
099: .getTableCellRendererComponent(null, icon, false,
100: false, -1, -1);
101: assertEquals(icon, label.getIcon());
102: assertEquals("", label.getText());
103: label = (JLabel) xTableRenderer.getTableCellRendererComponent(
104: null, "dummy", false, false, -1, -1);
105: assertNull(label.getIcon());
106: assertEquals("dummy", label.getText());
107: }
108:
109: /**
110: * @return
111: */
112: private TableModel createTableModelWithDefaultTypes() {
113: String[] names = { "Object", "Number", "Double", "Date",
114: "ImageIcon", "Boolean" };
115: final Class[] types = { Object.class, Number.class,
116: Double.class, Date.class, ImageIcon.class,
117: Boolean.class };
118: DefaultTableModel model = new DefaultTableModel(names, 0) {
119:
120: @Override
121: public Class<?> getColumnClass(int columnIndex) {
122: return types[columnIndex];
123: }
124:
125: };
126: Date today = new Date();
127: Icon icon = new ImageIcon(JXTable.class
128: .getResource("resources/images/kleopatra.jpg"));
129: for (int i = 0; i < 10; i++) {
130: Object[] values = new Object[] { "row " + i, i,
131: Math.random() * 100,
132: new Date(today.getTime() + i * 1000000), icon,
133: i % 2 == 0 };
134: model.addRow(values);
135: }
136: return model;
137: }
138:
139: /**
140: * test serializable of default renderer.
141: *
142: */
143: public void testSerializeTableRenderer() {
144: TableCellRenderer xListRenderer = new DefaultTableRenderer();
145: try {
146: SerializableSupport.serialize(xListRenderer);
147: } catch (Exception e) {
148: fail("not serializable " + e);
149: }
150: }
151:
152: /**
153: * base interaction with table: focused, selected uses UI border.
154: */
155: public void testTableFocusSelectedBorder() {
156: // sanity to see test test validity
157: // UIManager.put("Table.focusSelectedCellHighlightBorder", new LineBorder(Color.red));
158: // access ui colors
159: Border selectedFocusBorder = getFocusBorder(true);
160: // sanity
161: if (selectedFocusBorder == null) {
162: LOG
163: .info("cannot run focusSelectedBorder - UI has no selected focus border");
164: return;
165:
166: }
167: // need to prepare directly - focus is true only if table is focusowner
168: JComponent coreComponent = (JComponent) coreTableRenderer
169: .getTableCellRendererComponent(table, null, true, true,
170: 0, coreColumn);
171: // sanity: known standard behaviour
172: assertEquals(selectedFocusBorder, coreComponent.getBorder());
173: // prepare extended
174: JComponent xComponent = (JComponent) xTableRenderer
175: .getTableCellRendererComponent(table, null, true, true,
176: 0, xColumn);
177: // assert behaviour same as standard
178: assertEquals(coreComponent.getBorder(), xComponent.getBorder());
179: }
180:
181: private Border getFocusBorder(boolean lookup) {
182: Border selectedFocusBorder = UIManager
183: .getBorder("Table.focusSelectedCellHighlightBorder");
184: if (lookup && (selectedFocusBorder == null)) {
185: selectedFocusBorder = UIManager
186: .getBorder("Table.focusCellHighlightBorder");
187: }
188: return selectedFocusBorder;
189: }
190:
191: /**
192: * base interaction with table: focused, not-selected uses UI border.
193: *
194: *
195: */
196: public void testTableFocusBorder() {
197: // access ui colors
198: Border focusBorder = UIManager
199: .getBorder("Table.focusCellHighlightBorder");
200: // Border selectedFocusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
201: // sanity
202: assertNotNull(focusBorder);
203: // need to prepare directly - focus is true only if table is focusowner
204: JComponent coreComponent = (JComponent) coreTableRenderer
205: .getTableCellRendererComponent(table, null, false,
206: true, 0, coreColumn);
207: // sanity: known standard behaviour
208: assertEquals(focusBorder, coreComponent.getBorder());
209: // prepare extended
210: JComponent xComponent = (JComponent) xTableRenderer
211: .getTableCellRendererComponent(table, null, false,
212: true, 0, xColumn);
213: // assert behaviour same as standard
214: assertEquals(coreComponent.getBorder(), xComponent.getBorder());
215: }
216:
217: /**
218: * base interaction with table: focused, not-selected and editable
219: * uses UI colors.
220: *
221: */
222: public void testTableRendererExtFocusedNotSelectedEditable() {
223: // sanity
224: assertTrue(table.isCellEditable(0, coreColumn));
225: // access ui colors
226: Color uiForeground = UIManager
227: .getColor("Table.focusCellForeground");
228: Color uiBackground = UIManager
229: .getColor("Table.focusCellBackground");
230: // sanity
231: assertNotNull(uiForeground);
232: assertNotNull(uiBackground);
233: Color background = Color.MAGENTA;
234: Color foreground = Color.YELLOW;
235: // prepare standard
236: coreTableRenderer.setBackground(background);
237: coreTableRenderer.setForeground(foreground);
238: // need to prepare directly - focus is true only if table is focusowner
239: Component coreComponent = coreTableRenderer
240: .getTableCellRendererComponent(table, null, false,
241: true, 0, coreColumn);
242: // sanity: known standard behaviour
243: assertEquals(uiBackground, coreComponent.getBackground());
244: assertEquals(uiForeground, coreComponent.getForeground());
245: // prepare extended
246: xTableRenderer.setBackground(background);
247: xTableRenderer.setForeground(foreground);
248: Component xComponent = xTableRenderer
249: .getTableCellRendererComponent(table, null, false,
250: true, 0, xColumn);
251: // assert behaviour same as standard
252: assertEquals(coreComponent.getBackground(), xComponent
253: .getBackground());
254: assertEquals(coreComponent.getForeground(), xComponent
255: .getForeground());
256: }
257:
258: /**
259: * base interaction with table: custom color of renderer precedes
260: * table color.
261: *
262: */
263: public void testTableRendererExtCustomColor() {
264: Color background = Color.MAGENTA;
265: Color foreground = Color.YELLOW;
266: // prepare standard
267: coreTableRenderer.setBackground(background);
268: coreTableRenderer.setForeground(foreground);
269: Component coreComponent = table.prepareRenderer(
270: coreTableRenderer, 0, coreColumn);
271: // sanity: known standard behaviour
272: assertEquals(background, coreComponent.getBackground());
273: assertEquals(foreground, coreComponent.getForeground());
274: // prepare extended
275: xTableRenderer.setBackground(background);
276: xTableRenderer.setForeground(foreground);
277: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
278: xColumn);
279: // assert behaviour same as standard
280: assertEquals(coreComponent.getBackground(), xComponent
281: .getBackground());
282: assertEquals(coreComponent.getForeground(), xComponent
283: .getForeground());
284: }
285:
286: /**
287: * base interaction with table: renderer uses table's selection color.
288: *
289: */
290: public void testTableRendererExtSelectedColors() {
291: // select first row
292: table.setRowSelectionInterval(0, 0);
293: // prepare standard
294: Component coreComponent = table.prepareRenderer(
295: coreTableRenderer, 0, coreColumn);
296: // sanity: known standard behaviour
297: assertEquals(table.getSelectionBackground(), coreComponent
298: .getBackground());
299: assertEquals(table.getSelectionForeground(), coreComponent
300: .getForeground());
301: // prepare extended
302: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
303: xColumn);
304: // assert behaviour same as standard
305: assertEquals(coreComponent.getBackground(), xComponent
306: .getBackground());
307: assertEquals(coreComponent.getForeground(), xComponent
308: .getForeground());
309: }
310:
311: /**
312: * base interaction with table: renderer uses table's custom selection color.
313: *
314: */
315: public void testTableRendererExtTableSelectedColors() {
316: Color background = Color.MAGENTA;
317: Color foreground = Color.YELLOW;
318: table.setSelectionBackground(background);
319: table.setSelectionForeground(foreground);
320: // select first row
321: table.setRowSelectionInterval(0, 0);
322: // prepare standard
323: Component coreComponent = table.prepareRenderer(
324: coreTableRenderer, 0, coreColumn);
325: // sanity: known standard behaviour
326: assertEquals(table.getSelectionBackground(), coreComponent
327: .getBackground());
328: assertEquals(table.getSelectionForeground(), coreComponent
329: .getForeground());
330: // prepare extended
331: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
332: xColumn);
333: // assert behaviour same as standard
334: assertEquals(coreComponent.getBackground(), xComponent
335: .getBackground());
336: assertEquals(coreComponent.getForeground(), xComponent
337: .getForeground());
338: }
339:
340: /**
341: * base interaction with table: renderer uses table's unselected colors.
342: *
343: */
344: public void testTableRendererExtColors() {
345: // prepare standard
346: Component coreComponent = table.prepareRenderer(
347: coreTableRenderer, 0, coreColumn);
348: // sanity: known standard behaviour
349: assertEquals(table.getBackground(), coreComponent
350: .getBackground());
351: assertEquals(table.getForeground(), coreComponent
352: .getForeground());
353: // prepare extended
354: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
355: xColumn);
356: // assert behaviour same as standard
357: assertEquals(coreComponent.getBackground(), xComponent
358: .getBackground());
359: assertEquals(coreComponent.getForeground(), xComponent
360: .getForeground());
361: }
362:
363: /**
364: * base interaction with table: renderer uses table's unselected custom colors
365: *
366: *
367: */
368: public void testTableRendererExtTableColors() {
369: Color background = Color.MAGENTA;
370: Color foreground = Color.YELLOW;
371: table.setBackground(background);
372: table.setForeground(foreground);
373: // prepare standard
374: Component coreComponent = table.prepareRenderer(
375: coreTableRenderer, 0, coreColumn);
376: // sanity: known standard behaviour
377: assertEquals(table.getBackground(), coreComponent
378: .getBackground());
379: assertEquals(table.getForeground(), coreComponent
380: .getForeground());
381: // prepare extended
382: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
383: xColumn);
384: // assert behaviour same as standard
385: assertEquals(coreComponent.getBackground(), xComponent
386: .getBackground());
387: assertEquals(coreComponent.getForeground(), xComponent
388: .getForeground());
389: }
390:
391: /**
392: * characterize opaqueness of rendering components.
393: *
394: */
395: public void testTableOpaqueRenderer() {
396: // sanity
397: assertFalse(new JLabel().isOpaque());
398: assertTrue(coreTableRenderer.isOpaque());
399: // assertTrue(xTableRenderer.getRendererComponent().isOpaque());
400: }
401:
402: /**
403: * characterize opaqueness of rendering components.
404: *
405: * that's useless: the opaque magic only applies if parent != null
406: */
407: public void testTableOpaqueRendererComponent() {
408: // sanity
409: assertFalse(new JLabel().isOpaque());
410: Component coreComponent = table.prepareRenderer(
411: coreTableRenderer, 0, coreColumn);
412: // prepare extended
413: assertTrue(coreComponent.isOpaque());
414: Component xComponent = table.prepareRenderer(xTableRenderer, 0,
415: xColumn);
416: assertTrue(xComponent.isOpaque());
417: }
418:
419: /**
420: * base existence/type tests while adding DefaultTableCellRendererExt.
421: *
422: */
423: public void testTableRendererExt() {
424: DefaultTableRenderer renderer = new DefaultTableRenderer();
425: assertTrue(renderer instanceof TableCellRenderer);
426: assertTrue(renderer instanceof Serializable);
427:
428: }
429: }
|