1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import com.cosylab.gui.components.introspection.EditorsHelper;
23 import com.cosylab.gui.components.table.QueueTable;
24 import com.cosylab.gui.components.table.cells.DeviceCell;
25 import com.cosylab.gui.components.table.cells.DoubleCell;
26 import com.cosylab.gui.components.table.cells.LongCell;
27 import com.cosylab.gui.components.table.cells.LongEnumCell;
28 import com.cosylab.gui.components.table.cells.PatternCell;
29 import com.cosylab.gui.components.table.cells.StringCell;
30 import com.cosylab.gui.components.table.cells.TableCell;
31 import com.cosylab.gui.components.table.renderers.DateRenderer;
32 import com.cosylab.gui.components.table.renderers.DefaultTableCellRenderer;
33 import com.cosylab.gui.components.table.renderers.DeviceCellRenderer;
34 import com.cosylab.gui.components.table.renderers.DoubleCellEditor;
35 import com.cosylab.gui.components.table.renderers.DoubleCellRenderer;
36 import com.cosylab.gui.components.table.renderers.EmptyCellRenderer;
37 import com.cosylab.gui.components.table.renderers.LongCellEditor;
38 import com.cosylab.gui.components.table.renderers.LongCellRenderer;
39 import com.cosylab.gui.components.table.renderers.LongEnumCellEditor;
40 import com.cosylab.gui.components.table.renderers.LongEnumCellRenderer;
41 import com.cosylab.gui.components.table.renderers.PatternCellRenderer;
42 import com.cosylab.gui.components.table.renderers.StringCellRenderer;
43
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.MouseAdapter;
47 import java.awt.event.MouseEvent;
48
49 import java.util.ArrayList;
50 import java.util.Date;
51
52 import javax.swing.JCheckBoxMenuItem;
53 import javax.swing.JMenuItem;
54 import javax.swing.JPopupMenu;
55 import javax.swing.table.TableCellEditor;
56 import javax.swing.table.TableCellRenderer;
57 import javax.swing.table.TableColumn;
58 import javax.swing.table.TableColumnModel;
59 import javax.swing.table.TableModel;
60
61
62
63
64
65
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 public class ObjectTable extends QueueTable
94 {
95 private static final long serialVersionUID = 1L;
96 private ArrayList<TableColumn> hiddenColumns = new ArrayList<TableColumn>();
97 private boolean allowColumnHide = true;
98 private TableCellRenderer emptyCellRenderer = new EmptyCellRenderer();
99 private boolean lastSortDescending = false;
100 private int lastComlumnSorted = 1;
101
102 private class HeaderMouseListenerImpl extends MouseAdapter
103 {
104
105
106
107
108
109 private void performPopup(MouseEvent e)
110 {
111 if (e.isPopupTrigger()) {
112 (new ColumnChooser()).showPopup(e);
113 e.consume();
114 }
115 }
116
117
118
119
120 public void mouseReleased(MouseEvent e)
121 {
122 performPopup(e);
123 }
124
125
126
127
128 public void mouseClicked(MouseEvent e)
129 {
130 performPopup(e);
131 }
132
133
134
135
136 public void mousePressed(MouseEvent e)
137 {
138 performPopup(e);
139 }
140 }
141
142 private class ColumnChooser extends JPopupMenu
143 {
144 private static final long serialVersionUID = 1L;
145
146
147
148
149 private class ActionListenerImpl implements ActionListener
150 {
151 private int index;
152 private ObjectTable table;
153 private boolean newState;
154
155
156
157
158
159
160
161
162 public ActionListenerImpl(int mi, ObjectTable ot, boolean how)
163 {
164 table = ot;
165 index = mi;
166 newState = how;
167 }
168
169
170
171
172
173
174 public void actionPerformed(ActionEvent e)
175 {
176 if (newState) {
177 table.showColumn(index);
178 } else {
179 table.hideColumn(index);
180 }
181 }
182 }
183
184 private int col;
185
186
187
188
189 public ColumnChooser()
190 {
191 ObjectTable owner = ObjectTable.this;
192 TableModel model = owner.getModel();
193
194 if (model instanceof ObjectTableModel) {
195 JMenuItem item = new JMenuItem("Sort ascending");
196 item.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e)
198 {
199 TableModel model = getModel();
200
201 if (col > -1 && (model instanceof ObjectTableModel)) {
202 ((ObjectTableModel)model).getRowModel()
203 .sortRows(col, false);
204 }
205
206 lastSortDescending = false;
207 lastComlumnSorted = col;
208 }
209 });
210 add(item);
211 item = new JMenuItem("Sort decending");
212 item.addActionListener(new ActionListener() {
213 public void actionPerformed(ActionEvent e)
214 {
215 TableModel model = getModel();
216
217 if (col > -1 && (model instanceof ObjectTableModel)) {
218 ((ObjectTableModel)model).getRowModel()
219 .sortRows(col, true);
220 }
221
222 lastSortDescending = true;
223 lastComlumnSorted = col;
224 }
225 });
226 add(item);
227 add(new Separator());
228 }
229
230 int n = model.getColumnCount();
231
232 for (int i = 0; i < n; i++) {
233 JCheckBoxMenuItem item = new JCheckBoxMenuItem(model
234 .getColumnName(i));
235
236 boolean isHidden = (owner.findHiddenColumn(i) != null);
237
238 item.setSelected(!isHidden);
239
240 item.addActionListener(new ActionListenerImpl(i, owner, isHidden));
241 add(item);
242 }
243 }
244
245
246
247
248
249
250 public void showPopup(MouseEvent ev)
251 {
252 col = getTableHeader().columnAtPoint(ev.getPoint());
253
254 if (col > -1) {
255 col = getTableHeader().getColumnModel().getColumn(col)
256 .getModelIndex();
257 }
258
259 show(ev.getComponent(), ev.getX(), ev.getY());
260 }
261 }
262
263
264
265
266 public ObjectTable()
267 {
268 super();
269 getTableHeader().addMouseListener(new HeaderMouseListenerImpl());
270 }
271
272
273
274
275
276
277 public ObjectTable(TableModel dm)
278 {
279 super(dm);
280 getTableHeader().addMouseListener(new HeaderMouseListenerImpl());
281 }
282
283
284
285
286
287
288
289 public ObjectTable(TableModel dm, TableColumnModel cm)
290 {
291 super(dm, cm);
292 getTableHeader().addMouseListener(new HeaderMouseListenerImpl());
293 }
294
295
296
297
298
299
300
301
302 @SuppressWarnings("unchecked")
303 protected void createDefaultRenderers()
304 {
305 super.createDefaultRenderers();
306
307 defaultRenderersByColumnClass.put(DoubleCell.class,
308 new DoubleCellRenderer());
309
310 defaultRenderersByColumnClass.put(LongCell.class, new LongCellRenderer());
311
312 defaultRenderersByColumnClass.put(LongEnumCell.class,
313 new LongEnumCellRenderer());
314
315 defaultRenderersByColumnClass.put(StringCell.class,
316 new StringCellRenderer());
317
318 defaultRenderersByColumnClass.put(PatternCell.class,
319 new PatternCellRenderer());
320
321 defaultRenderersByColumnClass.put(DeviceCell.class,
322 new DeviceCellRenderer());
323
324 defaultRenderersByColumnClass.put(Date.class, new DateRenderer());
325
326 defaultRenderersByColumnClass.put(TableCell.class,
327 new DefaultTableCellRenderer());
328
329
330 }
331
332
333
334
335
336
337
338
339 @SuppressWarnings("unchecked")
340 protected void createDefaultEditors()
341 {
342 super.createDefaultEditors();
343
344 defaultEditorsByColumnClass.put(DoubleCell.class, new DoubleCellEditor());
345
346 defaultEditorsByColumnClass.put(LongCell.class, new LongCellEditor());
347
348 defaultEditorsByColumnClass.put(LongEnumCell.class,
349 new LongEnumCellEditor());
350
351 defaultEditorsByColumnClass.put(DeviceCell.class,
352 new DeviceCellRenderer());
353 }
354
355
356
357
358
359
360
361 public TableCellRenderer getCellRenderer(int row, int column)
362 {
363 TableColumn tableColumn = getColumnModel().getColumn(column);
364
365
366 TableCellRenderer renderer = tableColumn.getCellRenderer();
367
368 if (renderer!=null) {
369 return renderer;
370 }
371
372
373
374
375 Class cl= getColumnClass(column);
376 if (cl!=Object.class) {
377 renderer= getDefaultRenderer(cl);
378 }
379
380 if (renderer!=null) {
381 return renderer;
382 }
383
384
385 int c = convertColumnIndexToModel(column);
386 Object cell = getModel().getValueAt(row, c);
387
388 if (cell == null) {
389 return emptyCellRenderer;
390 }
391
392 renderer = getDefaultRenderer(cell.getClass());
393
394
395 if (renderer == null) {
396 renderer = EditorsHelper.getTableCellRendererForClass(cell.getClass());
397 }
398
399 if (renderer == null) {
400 renderer = super.getCellRenderer(row, column);
401 }
402
403 return renderer;
404 }
405
406
407
408
409
410
411
412 public TableCellEditor getCellEditor(int row, int column)
413 {
414 TableColumn tableColumn = getColumnModel().getColumn(column);
415
416
417 TableCellEditor editor = tableColumn.getCellEditor();
418
419 if (editor!=null) {
420 return editor;
421 }
422
423
424
425
426 Class cl= getColumnClass(column);
427 if (cl!=Object.class) {
428 editor= getDefaultEditor(cl);
429 }
430
431 if (editor!=null) {
432 return editor;
433 }
434
435
436 int c = convertColumnIndexToModel(column);
437 Object cell = getModel().getValueAt(row, c);
438
439 if (cell == null) {
440 return super.getCellEditor(row, column);
441 }
442
443 editor = getDefaultEditor(cell.getClass());
444
445
446 if (editor == null) {
447 editor = EditorsHelper.getTableCellEditorForClass(cell.getClass());
448 }
449
450 if (editor == null) {
451 editor = super.getCellEditor(row, column);
452 }
453
454 return editor;
455
456 }
457
458 protected boolean validColumnIndex(int index)
459 {
460 return ((index >= 0) && (index < getModel().getColumnCount()));
461 }
462
463
464
465
466
467
468 public void hideColumn(int index)
469 {
470 if (validColumnIndex(index) && allowColumnHide) {
471 TableColumn tc = getColumn(getModel().getColumnName(index));
472 getColumnModel().removeColumn(tc);
473 hiddenColumns.add(tc);
474 }
475 }
476
477
478
479
480
481
482
483
484 protected TableColumn findHiddenColumn(int modelIndex)
485 {
486 int n = hiddenColumns.size();
487
488 for (int i = 0; i < n; i++) {
489 TableColumn tc = hiddenColumns.get(i);
490
491 if (tc.getModelIndex() == modelIndex) {
492 return tc;
493 }
494 }
495
496 return null;
497 }
498
499
500
501
502
503
504 public void showColumn(int index)
505 {
506 if (validColumnIndex(index) && allowColumnHide) {
507 TableColumn tc = findHiddenColumn(index);
508
509 if (tc == null) {
510 return;
511 }
512
513 getColumnModel().addColumn(tc);
514 hiddenColumns.remove(tc);
515 getColumnModel().moveColumn(getColumnCount() - 1, index);
516 }
517 }
518
519
520
521
522 protected synchronized void showHiddenColumns()
523 {
524 for (int i = 0; i < hiddenColumns.size(); i++) {
525 getColumnModel().addColumn(hiddenColumns.get(i));
526 }
527
528 hiddenColumns.clear();
529 }
530
531
532
533
534
535
536 public boolean getAllowColumnHide()
537 {
538 return allowColumnHide;
539 }
540
541
542
543
544
545
546 public void setAllowColumnHide(boolean value)
547 {
548 allowColumnHide = value;
549 }
550
551
552
553
554 public void createDefaultColumnsFromModel()
555 {
556 if (hiddenColumns != null) {
557 hiddenColumns.clear();
558 }
559
560 super.createDefaultColumnsFromModel();
561 }
562
563
564
565
566
567 public void repeatLastSort()
568 {
569 TableModel model = getModel();
570
571 if (lastComlumnSorted > -1 && (model instanceof ObjectTableModel)) {
572 ((ObjectTableModel)model).getRowModel().sortRows(lastComlumnSorted,
573 lastSortDescending);
574 }
575 }
576 }
577
578