01: /*
02: * Created on 02.06.2006
03: *
04: */
05: package org.jdesktop.swingx.table;
06:
07: import java.awt.Component;
08:
09: import javax.swing.JTable;
10: import javax.swing.table.DefaultTableCellRenderer;
11: import javax.swing.table.DefaultTableModel;
12: import javax.swing.table.TableCellRenderer;
13: import javax.swing.table.TableModel;
14:
15: import org.jdesktop.swingx.InteractiveTestCase;
16: import org.jdesktop.swingx.JXTable;
17: import org.jdesktop.test.PropertyChangeReport;
18:
19: public class ColumnFactoryTest extends InteractiveTestCase {
20:
21: /**
22: * Issue #315-swingx: pack doesn't pass column index to headerRenderer.
23: * This bug shows only if the renderer relies on the column index (default doesn't).
24: */
25: public void testPackColumnIndexToHeaderRenderer() {
26: final int special = 1;
27: TableCellRenderer renderer = new DefaultTableCellRenderer() {
28:
29: @Override
30: public Component getTableCellRendererComponent(
31: JTable table, Object value, boolean isSelected,
32: boolean hasFocus, int row, int column) {
33: if (column == special) {
34: value = "exxxxttteeeed" + value;
35: }
36: super .getTableCellRendererComponent(table, value,
37: isSelected, hasFocus, row, column);
38: return this ;
39: }
40:
41: };
42: JXTable table = new JXTable(1, 2);
43: table.getTableHeader().setDefaultRenderer(renderer);
44: TableColumnExt columnExt = table.getColumnExt(special);
45: table.packAll();
46: int realPrefWidth = 2 * 4 // magic value - JXTable's default margin,
47: // needs to be made configurable, see Issue ??
48: + renderer.getTableCellRendererComponent(table,
49: columnExt.getHeaderValue(), false, false, -1, special)
50: .getPreferredSize().width;
51: assertEquals(realPrefWidth, columnExt.getPreferredWidth());
52:
53: }
54:
55: /**
56: * Issue #266-swingx: support customization of pack margin.
57: *
58: * added property to ColumnFactory.
59: *
60: */
61: public void testPackMargin() {
62: final int special = 1;
63: JXTable table = new JXTable(1, 2);
64: ColumnFactory factory = new ColumnFactory();
65: table.setColumnFactory(factory);
66: table.setValueAt("something that's wider than 75", 0, special);
67: TableColumnExt columnExt = table.getColumnExt(special);
68: table.packAll();
69: TableCellRenderer renderer = table.getCellRenderer(0, special);
70: Component comp = table.prepareRenderer(renderer, 0, special);
71: int realPrefWidth = 2 * factory.getDefaultPackMargin() // magic value - JXTable's default margin,
72: // needs to be made configurable, see Issue 266
73: + comp.getPreferredSize().width;
74: // sanity - default margin kicks in
75: assertEquals(realPrefWidth, columnExt.getPreferredWidth());
76:
77: int margin = 10;
78: factory.setDefaultPackMargin(margin);
79: table.packAll();
80: table.prepareRenderer(renderer, 0, special);
81: int otherPrefWidth = 2 * margin + comp.getPreferredSize().width;
82: assertEquals(otherPrefWidth, columnExt.getPreferredWidth());
83:
84: }
85:
86: }
|