1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.models;
17
18 import java.awt.Dimension;
19 import java.util.List;
20
21 import javax.swing.table.DefaultTableColumnModel;
22 import javax.swing.table.TableColumn;
23
24 import org.xulux.api.gui.IWidget;
25 import org.xulux.guilayer.swing.widgets.MenuItem;
26 import org.xulux.guilayer.swing.widgets.PopupMenu;
27 import org.xulux.guilayer.swing.widgets.Table;
28
29
30
31
32
33
34 public class NyxTableColumnModel extends DefaultTableColumnModel {
35
36
37
38
39 protected Table table;
40
41
42
43 protected NyxTableColumnModel lockedModel;
44
45
46
47 protected int lockedColumnWidth;
48
49
50
51 protected boolean initializing;
52
53
54
55 public NyxTableColumnModel() {
56 super();
57 }
58
59
60
61
62
63 public NyxTableColumnModel(Table table) {
64 setTable(table);
65 initializeColumns();
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 }
90
91
92
93
94 public void setTable(Table table) {
95 this.table = table;
96 }
97
98
99
100
101 private void initializeColumns() {
102 initializing = true;
103 int maxHeight = 0;
104 List list = table.getChildWidgets();
105 if (list == null) {
106 return;
107 }
108 lockedModel = new NyxTableColumnModel();
109 lockedModel.setTable(table);
110 for (int i = 0; i < list.size(); i++) {
111 IWidget widget = (IWidget) list.get(i);
112 if (widget instanceof PopupMenu || widget instanceof MenuItem) {
113 continue;
114 }
115 NyxTableColumn column = new NyxTableColumn(widget);
116 String headerText = widget.getProperty("text");
117 if (headerText != null && headerText.indexOf("\\n") != -1) {
118 column.setHeaderRenderer(new MultipleLineHeaderRenderer(null));
119 }
120 column.setModel(this);
121 column.setModelIndex(i);
122 column.setCellRenderer(new NyxTableCellRenderer(widget, table));
123 int height = widget.getRectangle().getHeight();
124 if (height > maxHeight) {
125 maxHeight = height;
126 }
127 addColumn(column);
128 lockedModel.addColumn(column);
129 }
130
131 table.setProperty("rowHeight", String.valueOf(maxHeight));
132 initializing = false;
133 }
134
135
136
137
138
139 public boolean hasLockedColumns() {
140 boolean lockedColumns = false;
141 for (int i = 0; i < getColumnCount(); i++) {
142 if (((NyxTableColumn) getColumn(i)).isLocked()) {
143 lockedColumns = true;
144 break;
145 }
146 }
147 return lockedColumns;
148 }
149
150
151
152
153
154 public void refresh() {
155
156 removeAllColumns();
157
158 initializeColumns();
159 }
160
161
162
163
164
165
166 protected void removeAllColumns() {
167 for (int i = 0; i < getColumnCount(); i++) {
168 removeColumn(getColumn(i));
169 }
170
171 if (lockedModel != null) {
172 lockedModel.destroy();
173 lockedModel = null;
174 }
175 }
176
177
178
179
180
181
182
183 public void removeUnlockedColumns() {
184 for (int i = 0; i < lockedModel.getColumnCount(); i++) {
185 if (i >= getColumnCount()) {
186
187 return;
188 }
189 NyxTableColumn column = (NyxTableColumn) lockedModel.getColumn(i);
190 if (!column.isLocked()) {
191 lockedModel.removeColumn(column);
192 i--;
193 }
194 }
195 }
196
197
198
199 public void removeLockedColumns() {
200 for (int i = 0; i < getColumnCount(); i++) {
201 NyxTableColumn column = (NyxTableColumn) getColumn(i);
202 if (column.isLocked()) {
203 lockedColumnWidth += column.getPreferredWidth();
204 removeColumn(column);
205 i--;
206 }
207 }
208 }
209
210
211
212
213 public Dimension getLockedColumnWidth() {
214 return new Dimension(lockedColumnWidth, 0);
215 }
216
217
218
219
220
221 public void setLockedColumnWidth(int width) {
222 lockedColumnWidth = width;
223 }
224
225
226
227
228
229 public NyxTableColumnModel getLockedColumnModel() {
230 return lockedModel;
231 }
232
233
234
235
236
237 public void destroy() {
238 table = null;
239 if (lockedModel != null) {
240 lockedModel.destroy();
241 lockedModel = null;
242 }
243 }
244
245
246
247
248 public TableColumn getColumn(int columnIndex) {
249
250 return super.getColumn(columnIndex);
251 }
252
253
254
255
256 public boolean isInitializing() {
257 return initializing;
258 }
259
260 }