66 |
private static final long serialVersionUID = 6580012493508960512L; |
private static final long serialVersionUID = 6580012493508960512L; |
67 |
|
|
68 |
/** |
/** |
69 |
* tableColumns |
* Columns that this model keeps track of. |
70 |
*/ |
*/ |
71 |
protected Vector tableColumns; |
protected Vector tableColumns; |
72 |
|
|
73 |
/** |
/** |
74 |
* selectionModel |
* Selection Model that keeps track of columns selection |
75 |
*/ |
*/ |
76 |
protected ListSelectionModel selectionModel; |
protected ListSelectionModel selectionModel; |
77 |
|
|
78 |
/** |
/** |
79 |
* columnMargin |
* Space between two columns. By default it is set to 1 |
80 |
*/ |
*/ |
81 |
protected int columnMargin; |
protected int columnMargin; |
82 |
|
|
83 |
/** |
/** |
84 |
* listenerList |
* listenerList keeps track of all listeners registered with this model |
85 |
*/ |
*/ |
86 |
protected EventListenerList listenerList = new EventListenerList(); |
protected EventListenerList listenerList = new EventListenerList(); |
87 |
|
|
88 |
/** |
/** |
89 |
* changeEvent |
* changeEvent is fired when change occurs in one of the columns properties |
90 |
*/ |
*/ |
91 |
protected transient ChangeEvent changeEvent = new ChangeEvent(this); |
protected transient ChangeEvent changeEvent = new ChangeEvent(this); |
92 |
|
|
93 |
/** |
/** |
94 |
* columnSelectionAllowed |
* Indicates whether columns can be selected |
95 |
*/ |
*/ |
96 |
protected boolean columnSelectionAllowed; |
protected boolean columnSelectionAllowed; |
97 |
|
|
98 |
/** |
/** |
99 |
* totalColumnWidth |
* Total width of all the columns in this model |
100 |
*/ |
*/ |
101 |
protected int totalColumnWidth; |
protected int totalColumnWidth; |
102 |
|
|
106 |
public DefaultTableColumnModel() |
public DefaultTableColumnModel() |
107 |
{ |
{ |
108 |
tableColumns = new Vector(); |
tableColumns = new Vector(); |
109 |
setSelectionModel(new DefaultListSelectionModel()); |
setSelectionModel(createSelectionModel()); |
110 |
columnMargin = 1; |
columnMargin = 1; |
111 |
columnSelectionAllowed = false; |
columnSelectionAllowed = false; |
112 |
} |
} |
113 |
|
|
114 |
/** |
/** |
115 |
* addColumn |
* addColumn adds column to the model. This method fires ColumnAdded |
116 |
* @param value0 TODO |
* event to model's registered TableColumnModelListeners. |
117 |
|
* |
118 |
|
* @param col column to add |
119 |
*/ |
*/ |
120 |
public void addColumn(TableColumn col) |
public void addColumn(TableColumn col) |
121 |
{ |
{ |
122 |
tableColumns.add(col); |
tableColumns.add(col); |
123 |
invalidateWidthCache(); |
invalidateWidthCache(); |
124 |
|
fireColumnAdded(new TableColumnModelEvent(this,0,tableColumns.size())); |
125 |
} |
} |
126 |
|
|
127 |
/** |
/** |
128 |
* removeColumn |
* removeColumn removes table column from the model. This method fires |
129 |
* @param value0 TODO |
* ColumnRemoved event to model's registered TableColumnModelListeners. |
130 |
|
* |
131 |
|
* @param col column to be removed |
132 |
*/ |
*/ |
133 |
public void removeColumn(TableColumn col) |
public void removeColumn(TableColumn col) |
134 |
{ |
{ |
135 |
|
int index = getColumnIndex(col); |
136 |
|
fireColumnRemoved(new TableColumnModelEvent(this,index,0)); |
137 |
tableColumns.remove(col); |
tableColumns.remove(col); |
138 |
invalidateWidthCache(); |
invalidateWidthCache(); |
139 |
} |
} |
140 |
|
|
141 |
/** |
/** |
142 |
* moveColumn |
* moveColumn moves column at index i to index j. This method fires |
143 |
* @param value0 TODO |
* ColumnMoved event to model's registered TableColumnModelListeners. |
144 |
* @param value1 TODO |
* |
145 |
|
* @param i index of the column that will be moved |
146 |
|
* @param j index of column's new location |
147 |
*/ |
*/ |
148 |
public void moveColumn(int i, int j) |
public void moveColumn(int i, int j) |
149 |
{ |
{ |
150 |
Object tmp = tableColumns.get(i); |
Object tmp = tableColumns.get(i); |
151 |
tableColumns.set(i, tableColumns.get(j)); |
tableColumns.set(i, tableColumns.get(j)); |
152 |
tableColumns.set(j, tmp); |
tableColumns.set(j, tmp); |
153 |
|
fireColumnAdded(new TableColumnModelEvent(this,i,j)); |
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
157 |
* setColumnMargin |
* setColumnMargin sets margin of the columns. |
158 |
* @param value0 TODO |
* @param m new column margin |
159 |
*/ |
*/ |
160 |
public void setColumnMargin(int m) |
public void setColumnMargin(int m) |
161 |
{ |
{ |
162 |
columnMargin = m; |
columnMargin = m; |
163 |
|
fireColumnMarginChanged(); |
164 |
} |
} |
165 |
|
|
166 |
/** |
/** |
167 |
* getColumnCount |
* getColumnCount returns number of columns in the model |
168 |
* @return int |
* @return int number of columns in the model |
169 |
*/ |
*/ |
170 |
public int getColumnCount() |
public int getColumnCount() |
171 |
{ |
{ |
182 |
} |
} |
183 |
|
|
184 |
/** |
/** |
185 |
* getColumnIndex |
* getColumnIndex returns index of the specified column |
186 |
* @param value0 TODO |
* |
187 |
* @return int |
* @param identifier identifier of the column |
188 |
|
* @return int index of the given column |
189 |
*/ |
*/ |
190 |
public int getColumnIndex(Object obj) |
public int getColumnIndex(Object identifier) |
191 |
{ |
{ |
192 |
return tableColumns.indexOf(obj, 0); |
return tableColumns.indexOf(identifier, 0); |
193 |
} |
} |
194 |
|
|
195 |
/** |
/** |
196 |
* getColumn |
* getColumn returns column at the specified index |
197 |
* @param value0 TODO |
* @param i index of the column |
198 |
* @return TableColumn |
* @return TableColumn column at the specified index |
199 |
*/ |
*/ |
200 |
public TableColumn getColumn(int i) |
public TableColumn getColumn(int i) |
201 |
{ |
{ |
203 |
} |
} |
204 |
|
|
205 |
/** |
/** |
206 |
* getColumnMargin |
* getColumnMargin returns column margin |
207 |
* @return int |
* @return int column margin |
208 |
*/ |
*/ |
209 |
public int getColumnMargin() |
public int getColumnMargin() |
210 |
{ |
{ |
212 |
} |
} |
213 |
|
|
214 |
/** |
/** |
215 |
* getColumnIndexAtX |
* getColumnIndexAtX returns column that contains specified x-coordinate. |
216 |
* @param value0 TODO |
* @param x x-coordinate that column should contain |
217 |
* @return int |
* @return int index of the column that contains specified x-coordinate relative |
218 |
|
* to this column model |
219 |
*/ |
*/ |
220 |
public int getColumnIndexAtX(int x) |
public int getColumnIndexAtX(int x) |
221 |
{ |
{ |
231 |
} |
} |
232 |
|
|
233 |
/** |
/** |
234 |
* getTotalColumnWidth |
* getTotalColumnWidth returns total width of all the columns including |
235 |
* @return int |
* column's margins. |
236 |
|
* |
237 |
|
* @return total width of all the columns |
238 |
*/ |
*/ |
239 |
public int getTotalColumnWidth() |
public int getTotalColumnWidth() |
240 |
{ |
{ |
244 |
} |
} |
245 |
|
|
246 |
/** |
/** |
247 |
* setSelectionModel |
* setSelectionModel sets selection model that will be used by this ColumnTableModel |
248 |
* @param model TODO |
* to keep track of currently selected columns |
249 |
|
* |
250 |
|
* @param model new selection model |
251 |
* @exception IllegalArgumentException if model is null |
* @exception IllegalArgumentException if model is null |
252 |
*/ |
*/ |
253 |
public void setSelectionModel(ListSelectionModel model) |
public void setSelectionModel(ListSelectionModel model) |
260 |
} |
} |
261 |
|
|
262 |
/** |
/** |
263 |
* getSelectionModel |
* getSelectionModel returns selection model |
264 |
* @return ListSelectionModel |
* @return ListSelectionModel selection model |
265 |
*/ |
*/ |
266 |
public ListSelectionModel getSelectionModel() |
public ListSelectionModel getSelectionModel() |
267 |
{ |
{ |
269 |
} |
} |
270 |
|
|
271 |
/** |
/** |
272 |
* setColumnSelectionAllowed |
* setColumnSelectionAllowed sets whether column selection is allowed |
273 |
* @param value0 TODO |
* or not. |
274 |
|
* |
275 |
|
* @param flag true if column selection is allowed and false otherwise |
276 |
*/ |
*/ |
277 |
public void setColumnSelectionAllowed(boolean a) |
public void setColumnSelectionAllowed(boolean flag) |
278 |
{ |
{ |
279 |
columnSelectionAllowed = a; |
columnSelectionAllowed = flag; |
280 |
} |
} |
281 |
|
|
282 |
/** |
/** |
283 |
* getColumnSelectionAllowed |
* getColumnSelectionAllowed indicates whether column selection is |
284 |
* @return boolean |
* allowed or not. |
285 |
|
* |
286 |
|
* @return boolean true if column selection is allowed and false otherwise. |
287 |
*/ |
*/ |
288 |
public boolean getColumnSelectionAllowed() |
public boolean getColumnSelectionAllowed() |
289 |
{ |
{ |
291 |
} |
} |
292 |
|
|
293 |
/** |
/** |
294 |
* getSelectedColumns |
* getSelectedColumns returns array containing indexes of currently |
295 |
* @return int[] |
* selected columns |
296 |
|
* |
297 |
|
* @return int[] array containing indexes of currently selected columns |
298 |
*/ |
*/ |
299 |
public int[] getSelectedColumns() |
public int[] getSelectedColumns() |
300 |
{ |
{ |
301 |
return null; // TODO |
// FIXME: Implementation of this method was taken from private method |
302 |
|
// JTable.getSelections(), which is used in various places in JTable |
303 |
|
// including selected row calculations and cannot be simply removed. |
304 |
|
// This design should be improved to illuminate duplication of code. |
305 |
|
|
306 |
|
ListSelectionModel lsm = this.selectionModel; |
307 |
|
int sz = getSelectedColumnCount(); |
308 |
|
int [] ret = new int[sz]; |
309 |
|
|
310 |
|
int lo = lsm.getMinSelectionIndex(); |
311 |
|
int hi = lsm.getMaxSelectionIndex(); |
312 |
|
int j = 0; |
313 |
|
java.util.ArrayList ls = new java.util.ArrayList(); |
314 |
|
if (lo != -1 && hi != -1) |
315 |
|
{ |
316 |
|
switch (lsm.getSelectionMode()) |
317 |
|
{ |
318 |
|
case ListSelectionModel.SINGLE_SELECTION: |
319 |
|
ret[0] = lo; |
320 |
|
break; |
321 |
|
|
322 |
|
case ListSelectionModel.SINGLE_INTERVAL_SELECTION: |
323 |
|
for (int i = lo; i <= hi; ++i) |
324 |
|
ret[j++] = i; |
325 |
|
break; |
326 |
|
|
327 |
|
case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: |
328 |
|
for (int i = lo; i <= hi; ++i) |
329 |
|
if (lsm.isSelectedIndex(i)) |
330 |
|
ret[j++] = i; |
331 |
|
break; |
332 |
|
} |
333 |
|
} |
334 |
|
return ret; |
335 |
} |
} |
336 |
|
|
337 |
/** |
/** |
338 |
* getSelectedColumnCount |
* getSelectedColumnCount returns number of currently selected columns |
339 |
* @return int |
* @return int number of currently selected columns |
340 |
*/ |
*/ |
341 |
public int getSelectedColumnCount() |
public int getSelectedColumnCount() |
342 |
{ |
{ |
343 |
return 0; // TODO |
// FIXME: Implementation of this method was taken from private method |
344 |
|
// JTable.countSelections(), which is used in various places in JTable |
345 |
|
// including selected row calculations and cannot be simply removed. |
346 |
|
// This design should be improved to illuminate duplication of code. |
347 |
|
|
348 |
|
ListSelectionModel lsm = this.selectionModel; |
349 |
|
int lo = lsm.getMinSelectionIndex(); |
350 |
|
int hi = lsm.getMaxSelectionIndex(); |
351 |
|
int sum = 0; |
352 |
|
|
353 |
|
if (lo != -1 && hi != -1) |
354 |
|
{ |
355 |
|
switch (lsm.getSelectionMode()) |
356 |
|
{ |
357 |
|
case ListSelectionModel.SINGLE_SELECTION: |
358 |
|
sum = 1; |
359 |
|
break; |
360 |
|
|
361 |
|
case ListSelectionModel.SINGLE_INTERVAL_SELECTION: |
362 |
|
sum = hi - lo + 1; |
363 |
|
break; |
364 |
|
|
365 |
|
case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: |
366 |
|
for (int i = lo; i <= hi; ++i) |
367 |
|
if (lsm.isSelectedIndex(i)) |
368 |
|
++sum; |
369 |
|
break; |
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
|
return sum; |
374 |
} |
} |
375 |
|
|
376 |
/** |
/** |
377 |
* addColumnModelListener |
* addColumnModelListener adds specified listener to the model's |
378 |
|
* listener list |
379 |
|
* |
380 |
* @param listener the listener to add |
* @param listener the listener to add |
381 |
*/ |
*/ |
382 |
public void addColumnModelListener(TableColumnModelListener listener) |
public void addColumnModelListener(TableColumnModelListener listener) |
385 |
} |
} |
386 |
|
|
387 |
/** |
/** |
388 |
* removeColumnModelListener |
* removeColumnModelListener removes specified listener from the model's |
389 |
|
* listener list. |
390 |
|
* |
391 |
* @param listener the listener to remove |
* @param listener the listener to remove |
392 |
*/ |
*/ |
393 |
public void removeColumnModelListener(TableColumnModelListener listener) |
public void removeColumnModelListener(TableColumnModelListener listener) |
405 |
} |
} |
406 |
|
|
407 |
/** |
/** |
408 |
* fireColumnAdded |
* fireColumnAdded fires TableColumnModelEvent to registered |
409 |
* @param value0 TODO |
* TableColumnModelListeners to indicate that column was added |
410 |
|
* |
411 |
|
* @param e TableColumnModelEvent |
412 |
*/ |
*/ |
413 |
protected void fireColumnAdded(TableColumnModelEvent value0) |
protected void fireColumnAdded(TableColumnModelEvent e) |
414 |
{ |
{ |
415 |
// TODO |
TableColumnModelListener[] listeners = getColumnModelListeners(); |
416 |
|
|
417 |
|
for (int i=0; i< listeners.length; i++) |
418 |
|
listeners[i].columnAdded(e); |
419 |
} |
} |
420 |
|
|
421 |
/** |
/** |
422 |
* fireColumnRemoved |
* fireColumnAdded fires TableColumnModelEvent to registered |
423 |
* @param value0 TODO |
* TableColumnModelListeners to indicate that column was removed |
424 |
|
* |
425 |
|
* @param e TableColumnModelEvent |
426 |
*/ |
*/ |
427 |
protected void fireColumnRemoved(TableColumnModelEvent value0) |
protected void fireColumnRemoved(TableColumnModelEvent e) |
428 |
{ |
{ |
429 |
// TODO |
TableColumnModelListener[] listeners = getColumnModelListeners(); |
430 |
|
|
431 |
|
for (int i=0; i< listeners.length; i++) |
432 |
|
listeners[i].columnRemoved(e); |
433 |
} |
} |
434 |
|
|
435 |
/** |
/** |
436 |
* fireColumnMoved |
* fireColumnAdded fires TableColumnModelEvent to registered |
437 |
* @param value0 TODO |
* TableColumnModelListeners to indicate that column was moved |
438 |
|
* |
439 |
|
* @param e TableColumnModelEvent |
440 |
*/ |
*/ |
441 |
protected void fireColumnMoved(TableColumnModelEvent value0) |
protected void fireColumnMoved(TableColumnModelEvent e) |
442 |
{ |
{ |
443 |
// TODO |
TableColumnModelListener[] listeners = getColumnModelListeners(); |
444 |
|
|
445 |
|
for (int i=0; i< listeners.length; i++) |
446 |
|
listeners[i].columnMoved(e); |
447 |
} |
} |
448 |
|
|
449 |
/** |
/** |
450 |
* fireColumnSelectionChanged |
* fireColumnSelectionChanged fires TableColumnModelEvent to model's |
451 |
* @param value0 TODO |
* registered TableColumnModelListeners to indicate that different column |
452 |
|
* was selected. |
453 |
|
* |
454 |
|
* @param evt ListSelectionEvent |
455 |
*/ |
*/ |
456 |
protected void fireColumnSelectionChanged(ListSelectionEvent evt) |
protected void fireColumnSelectionChanged(ListSelectionEvent evt) |
457 |
{ |
{ |
461 |
} |
} |
462 |
|
|
463 |
/** |
/** |
464 |
* fireColumnMarginChanged |
* fireColumnMarginChanged fires TableColumnModelEvent to model's |
465 |
|
* registered TableColumnModelListeners to indicate that column margin |
466 |
|
* was changed. |
467 |
*/ |
*/ |
468 |
protected void fireColumnMarginChanged() |
protected void fireColumnMarginChanged() |
469 |
{ |
{ |
470 |
// TODO |
EventListener [] listeners = getListeners(TableColumnModelListener.class); |
471 |
|
for (int i = 0; i < listeners.length; ++i) |
472 |
|
((TableColumnModelListener)listeners[i]).columnMarginChanged(changeEvent); |
473 |
} |
} |
474 |
|
|
475 |
/** |
/** |
476 |
* getListeners |
* getListeners returns currently registered listeners with this model. |
477 |
* @param value0 TODO |
* @param listenerType type of listeners to return |
478 |
* @return EventListener[] |
* |
479 |
|
* @return EventListener[] array of model's listeners of the specified type |
480 |
*/ |
*/ |
481 |
public EventListener[] getListeners(Class klass) |
public EventListener[] getListeners(Class listenerType) |
482 |
{ |
{ |
483 |
return listenerList.getListeners(klass); |
return listenerList.getListeners(listenerType); |
484 |
} |
} |
485 |
|
|
486 |
/** |
/** |
487 |
* propertyChange |
* propertyChange handles changes occuring in the properties of the |
488 |
* @param value0 TODO |
* model's columns. |
489 |
|
* |
490 |
|
* @param evt PropertyChangeEvent |
491 |
*/ |
*/ |
492 |
public void propertyChange(PropertyChangeEvent value0) |
public void propertyChange(PropertyChangeEvent evt) |
493 |
{ |
{ |
494 |
// TODO |
if (evt.getPropertyName().equals(TableColumn.COLUMN_WIDTH_PROPERTY)) |
495 |
|
invalidateWidthCache(); |
496 |
} |
} |
497 |
|
|
498 |
/** |
/** |
499 |
* valueChanged |
* valueChanged handles changes in the selectionModel. |
500 |
* @param value0 TODO |
* @param e ListSelectionEvent |
501 |
*/ |
*/ |
502 |
public void valueChanged(ListSelectionEvent value0) |
public void valueChanged(ListSelectionEvent e) |
503 |
{ |
{ |
504 |
fireColumnSelectionChanged(value0); |
fireColumnSelectionChanged(e); |
505 |
} |
} |
506 |
|
|
507 |
/** |
/** |
508 |
* createSelectionModel |
* createSelectionModel creates selection model that will keep track |
509 |
* @return ListSelectionModel |
* of currently selected column(s) |
510 |
|
* |
511 |
|
* @return ListSelectionModel selection model of the columns |
512 |
*/ |
*/ |
513 |
protected ListSelectionModel createSelectionModel() |
protected ListSelectionModel createSelectionModel() |
514 |
{ |
{ |
515 |
return null; // TODO |
return new DefaultListSelectionModel(); |
516 |
} |
} |
517 |
|
|
518 |
/** |
/** |
519 |
* recalcWidthCache |
* recalcWidthCache calculates total width of the columns. |
520 |
|
* If the current cache of the total width is in invalidated state, |
521 |
|
* then width is recalculated. Otherwise nothing is done. |
522 |
*/ |
*/ |
523 |
protected void recalcWidthCache() |
protected void recalcWidthCache() |
524 |
{ |
{ |