1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import javax.help.CSH;
26 import javax.swing.JMenu;
27 import javax.swing.JTable;
28 import javax.swing.table.DefaultTableColumnModel;
29
30 import org.xnap.gui.menu.TableColumnsMenu;
31 import org.xnap.util.TablePreferencesProvider;
32
33 /***
34 * Provides a sortable table model with dynamic column support.
35 */
36 public abstract class AbstractColumnTableModel
37 extends AbstractSortableTableModel implements ColumnModel
38 {
39
40
41
42 protected List columns = new LinkedList();
43
44
45
46 public AbstractColumnTableModel()
47 {
48 }
49
50
51
52 /***
53 * Creates a table from this model.
54 */
55 public JTable createTable(TablePreferencesProvider tpp, String table)
56 {
57 DefaultTableColumnModel dtcm = new DefaultTableColumnModel();
58 JTable jta = new ColoredTable(this, dtcm);
59 jta.setShowGrid(false);
60 jta.setDefaultRenderer(Integer.class, new NumberCellRenderer());
61 jta.setDefaultRenderer(String.class, new StringCellRenderer());
62 CSH.setHelpIDString(jta, "tables");
63
64 TablePreferencesHandler
65 tph = new TablePreferencesHandler(tpp, table, this, dtcm);
66 TableColumnsMenu tcm
67 = new TableColumnsMenu(null, this, tpp, table);
68 TableHeaderHandler thh
69 = TableHeaderHandler.install(jta, tcm.getPopupMenu(), this);
70 thh.setListener(tph);
71
72 return jta;
73 }
74
75 public JMenu createColumnMenu(String name, TablePreferencesProvider tpp,
76 String table)
77 {
78 return new TableColumnsMenu(name, this, tpp, table);
79 }
80
81 public void addColumn(Column c)
82 {
83 c.setModelIndex(columns.size());
84 columns.add(c);
85 }
86
87 public void addColumns(Column[] columns)
88 {
89 for (int i = 0; i < columns.length; i++) {
90 addColumn(columns[i]);
91 }
92 }
93
94 /***
95 * Returns the column at index <code>i</code>.
96 */
97 public Column getColumnAt(int i)
98 {
99 return (Column)columns.get(i);
100 }
101
102 /***
103 * Returns the class of the data displayed by the column at index
104 * <code>i</code>.
105 */
106 public Class getColumnClass(int i)
107 {
108 return ((Column)columns.get(i)).getDataType();
109 }
110
111 /***
112 * Returns the number of columns.
113 */
114 public int getColumnCount()
115 {
116 return columns.size();
117 }
118
119 /***
120 * Returns the name of the column at index <code>i</code>.
121 */
122 public String getColumnName(int i)
123 {
124 return ((Column)columns.get(i)).getName();
125 }
126
127 }