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.Arrays;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import javax.swing.JMenu;
28 import javax.swing.table.DefaultTableColumnModel;
29 import javax.swing.tree.TreePath;
30
31 import org.xnap.gui.menu.TableColumnsMenu;
32 import org.xnap.util.TablePreferencesProvider;
33
34 /***
35 * Provides a sortable table model with dynamic column support.
36 * FIX: maintainSortOrder
37 */
38 public abstract class AbstractColumnTreeTableModel
39 extends AbstractTreeTableModel implements ColumnModel, SortableModel
40 {
41
42
43
44 protected List columns = new LinkedList();
45
46
47
48 /***
49 *
50 */
51 public AbstractColumnTreeTableModel()
52 {
53 }
54
55
56
57 /***
58 * Creates a table from this model.
59 */
60 public JTreeTable createTreeTable(TablePreferencesProvider tpp,
61 String table)
62 {
63 DefaultTableColumnModel dtcm = new DefaultTableColumnModel();
64 JTreeTable jtt = new JTreeTable(this, dtcm);
65
66 jtt.getTree().setRootVisible(false);
67 jtt.getTree().setShowsRootHandles(true);
68
69 boolean firstIsFixed = getColumnAt(0).getKey().equals("icon");
70 if (firstIsFixed) {
71
72 LinkedList visibleKeys
73 = new LinkedList(Arrays.asList(tpp.getTableColumns(table)));
74 for (Iterator i = visibleKeys.iterator(); i.hasNext();) {
75 Object key = i.next();
76 if (key.equals(getColumnAt(0).getKey())
77 || key.equals(getColumnAt(1).getKey())) {
78 i.remove();
79 }
80 }
81 visibleKeys.addFirst("icon");
82 tpp.setTableColumns(table,
83 (String[])visibleKeys.toArray(new String[0]));
84 }
85
86 TablePreferencesHandler
87 tph = new TablePreferencesHandler(tpp, table, this, dtcm);
88 TableColumnsMenu tcm
89 = new TableColumnsMenu(null, this, tpp, table,
90 (firstIsFixed) ? 2 : 0);
91 TableHeaderHandler thh
92 = TableHeaderHandler.install(jtt, tcm.getPopupMenu(), this);
93 thh.setListener(tph);
94 thh.setTree(jtt.getTree());
95
96 jtt.getTree().expandPath(new TreePath(this.getRoot()));
97
98 return jtt;
99 }
100
101 public JMenu createColumnMenu(String name, TablePreferencesProvider tpp,
102 String table)
103 {
104 return new TableColumnsMenu(name, this, tpp, table);
105 }
106
107 public void addColumn(Column c)
108 {
109 c.setModelIndex(columns.size());
110 columns.add(c);
111 }
112
113 public void addColumns(Column[] columns)
114 {
115 for (int i = 0; i < columns.length; i++) {
116 addColumn(columns[i]);
117 }
118 }
119
120 /***
121 * Returns the column at index <code>i</code>.
122 */
123 public Column getColumnAt(int i)
124 {
125 return (Column)columns.get(i);
126 }
127
128 /***
129 * Returns the class of the data displayed by the column at index
130 * <code>i</code>.
131 */
132 public Class getColumnClass(int i)
133 {
134 return ((Column)columns.get(i)).getDataType();
135 }
136
137 /***
138 * Returns the number of columns.
139 */
140 public int getColumnCount()
141 {
142 return columns.size();
143 }
144
145 /***
146 * Returns the name of the column at index <code>i</code>.
147 */
148 public String getColumnName(int i)
149 {
150 return ((Column)columns.get(i)).getName();
151 }
152
153 public boolean isLeaf(Object node) {
154 return node != getRoot() && getChildCount(node) == 0;
155 }
156
157 }