-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathDataTable.java
More file actions
1515 lines (1413 loc) · 49.3 KB
/
DataTable.java
File metadata and controls
1515 lines (1413 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
/**
* DataTable displays multiple TableModels in a table. The first TableModel
* usually contains the independent variable for the other TableModel so that
* the visibility of column[0] can be set to false for subsequent TableModels.
*
* @author Joshua Gould
* @author Wolfgang Christian
* @created February 21, 2002
* @version 1.0
*/
public class DataTable extends JTable implements ActionListener {
static final Color PANEL_BACKGROUND = javax.swing.UIManager.getColor("Panel.background"); //$NON-NLS-1$
final static Color LIGHT_BLUE = new Color(204, 204, 255);
static final String NO_PATTERN
= DisplayRes.getString("DataTable.FormatDialog.NoFormat"); //$NON-NLS-1$
public static String rowName = DisplayRes.getString("DataTable.Header.Row"); //$NON-NLS-1$
private final SortDecorator decorator;
protected HashMap<String, PrecisionRenderer> precisionRenderersByColumnName
= new HashMap<String, PrecisionRenderer>();
protected HashMap<String, UnitRenderer> unitRenderersByColumnName
= new HashMap<String, UnitRenderer>();
DataTableModel dataTableModel;
protected RowNumberRenderer rowNumberRenderer;
int maximumFractionDigits = 3;
int refreshDelay = 0; // time in ms to delay refresh events
javax.swing.Timer refreshTimer = new javax.swing.Timer(refreshDelay, this); // delay for refreshTable
protected int labelColumnWidth=40, minimumDataColumnWidth=24;
protected NumberFormatDialog formatDialog;
protected int clickCountToSort = 1;
/**
* Constructs a DatTable with a default data model
*/
public DataTable() {
this(new DefaultDataTableModel());
}
/**
* Constructs a DatTable with the specified data model
*
* @param model data model
*/
public DataTable(DataTableModel model) {
super();
refreshTimer.setRepeats(false);
refreshTimer.setCoalesce(true);
setModel(model);
setColumnSelectionAllowed(true);
setGridColor(Color.blue);
setSelectionBackground(LIGHT_BLUE);
JTableHeader header = getTableHeader();
header.setForeground(Color.blue); // set text color
TableCellRenderer headerRenderer = new HeaderRenderer(getTableHeader().getDefaultRenderer());
getTableHeader().setDefaultRenderer(headerRenderer);
setSelectionForeground(Color.red); // foreground color for selected cells
setColumnModel(new DataTableColumnModel());
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
setColumnSelectionAllowed(true);
// add column sorting using a SortDecorator
decorator = new SortDecorator(getModel());
setModel(decorator);
header.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(!OSPRuntime.isPopupTrigger(e)
&& !e.isControlDown()
&& !e.isShiftDown()
&& e.getClickCount()==clickCountToSort) {
TableColumnModel tcm = getColumnModel();
int vc = tcm.getColumnIndexAtX(e.getX());
int mc = convertColumnIndexToModel(vc);
decorator.sort(mc);
}
}
});
}
/**
* Sets the maximum number of fraction digits to display in a named column
*
* @param maximumFractionDigits maximum number of fraction digits to display
* @param columnName name of the column
*/
public void setMaximumFractionDigits(String columnName, int maximumFractionDigits) {
precisionRenderersByColumnName.put(columnName, new PrecisionRenderer(maximumFractionDigits));
}
/**
* Sets the formatting pattern for a named column
*
* @param pattern the pattern
* @param columnName name of the column
*/
public void setFormatPattern(String columnName, String pattern) {
if((pattern==null)||pattern.equals("")) { //$NON-NLS-1$
precisionRenderersByColumnName.remove(columnName);
} else {
precisionRenderersByColumnName.put(columnName, new PrecisionRenderer(pattern));
}
}
/**
* Sets the units and tooltip for a named column.
*
* @param columnName name of the column
* @param units the units string (may be null)
* @param tootip the tooltip (may be null)
*/
public void setUnits(String columnName, String units, String tooltip) {
if(units==null) {
unitRenderersByColumnName.remove(columnName);
}
else {
TableCellRenderer renderer = getDefaultRenderer(Double.class);
for (String next: precisionRenderersByColumnName.keySet()) {
if(next.equals(columnName)) {
renderer = precisionRenderersByColumnName.get(columnName);
}
}
UnitRenderer unitRenderer = new UnitRenderer(renderer, units, tooltip);
unitRenderersByColumnName.put(columnName, unitRenderer);
}
}
/**
* Gets the formatting pattern for a named column
*
* @param columnName name of the column
* @return the pattern
*/
public String getFormatPattern(String columnName) {
PrecisionRenderer r = precisionRenderersByColumnName.get(columnName);
return (r==null) ? "" : r.pattern; //$NON-NLS-1$
}
/**
* Gets the names of formatted columns
* Added by D Brown 24 Apr 2011
*
* @return array of names of columns with non-null formats
*/
public String[] getFormattedColumnNames() {
return precisionRenderersByColumnName.keySet().toArray(new String[0]);
}
/**
* Gets the formatted value at a given row and column.
* Added by D Brown 6 Oct 2010
*
* @param row the row number
* @param col the column number
* @return the value formatted as displayed in the table
*/
public Object getFormattedValueAt(int row, int col) {
Object value = getValueAt(row, col);
if (value==null)
return null;
TableCellRenderer renderer = getCellRenderer(row, col);
Component c = renderer.getTableCellRendererComponent(DataTable.this, value, false, false, 0, 0);
if (c instanceof JLabel) {
String s = ((JLabel)c).getText().trim();
// strip units, if any
if (renderer instanceof UnitRenderer) {
String units = ((UnitRenderer)renderer).units;
if (!"".equals(units)) { //$NON-NLS-1$
int n = s.lastIndexOf(units);
if (n>-1)
s = s.substring(0, n);
}
}
return s;
}
return value;
}
/**
* Gets the format setter dialog.
*
* @param names the column name choices
* @param selected the initially selected names
* @return the format setter dialog
*/
public NumberFormatDialog getFormatDialog(String[] names, String[] selected) {
if(formatDialog==null) {
formatDialog = new NumberFormatDialog();
// center on screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width - formatDialog.getBounds().width) / 2;
int y = (dim.height - formatDialog.getBounds().height) / 2;
formatDialog.setLocation(x, y);
}
formatDialog.setColumns(names, selected);
return formatDialog;
}
/**
* Sorts the table using the given column.
* @param col int
*/
public void sort(int col) {
decorator.sort(col);
}
/**
* Gets the sorted column. Added by D Brown 2010-10-24.
* @return the
*/
public int getSortedColumn() {
return decorator.getSortedColumn();
}
/**
* Sets the maximum number of fraction digits to display for cells that have
* type Double
*
* @param maximumFractionDigits - maximum number of fraction digits to display
*/
public void setMaximumFractionDigits(int maximumFractionDigits) {
this.maximumFractionDigits = maximumFractionDigits;
setDefaultRenderer(Double.class, new PrecisionRenderer(maximumFractionDigits));
}
/**
* Gets the maximum number of digits in the table.
* @return int
*/
public int getMaximumFractionDigits() {
return maximumFractionDigits;
}
/**
* Sets the display row number flag. Table displays row number.
*
* @param _rowNumberVisible <code>true<\code> if table display row number
*/
public void setRowNumberVisible(boolean _rowNumberVisible) {
if(dataTableModel.isRowNumberVisible()!=_rowNumberVisible) {
if(_rowNumberVisible&&(rowNumberRenderer==null)) {
rowNumberRenderer = new RowNumberRenderer(this);
}
dataTableModel.setRowNumberVisible(_rowNumberVisible);
}
}
/**
* Sets the model for this data table;
*
* @param _model
*/
public void setModel(DataTableModel _model) {
super.setModel(_model);
dataTableModel = _model;
}
/**
* Sets the stride of a TableModel in the DataTable.
*
* @param tableModel
* @param stride
*/
public void setStride(TableModel tableModel, int stride) {
dataTableModel.setStride(tableModel, stride);
}
/**
* Sets the visibility of a column of a TableModel in the DataTable.
*
* @param tableModel
* @param columnIndex
* @param b
*/
public void setColumnVisible(TableModel tableModel, int columnIndex, boolean b) {
dataTableModel.setColumnVisible(tableModel, columnIndex, b);
}
/**
* Gets the display row number flag.
*
* @return The rowNumberVisible value
*/
public boolean isRowNumberVisible() {
return dataTableModel.isRowNumberVisible();
}
/**
* Returns an appropriate renderer for the cell specified by this row and
* column. If the <code>TableColumn</code> for this column has a non-null
* renderer, returns that. If the <code>TableColumn</code> for this column has
* the same name as a name specified in the setMaximumFractionDigits method,
* returns the appropriate renderer. If not, finds the class of the data in
* this column (using <code>getColumnClass</code>) and returns the default
* renderer for this type of data.
*
* @param row Description of Parameter
* @param column Description of Parameter
* @return The cellRenderer value
*/
public TableCellRenderer getCellRenderer(int row, int column) {
int i = convertColumnIndexToModel(column);
if((i==0)&&dataTableModel.isRowNumberVisible()) {
return rowNumberRenderer;
}
UnitRenderer unitRenderer = null;
TableCellRenderer baseRenderer = null;
try {
// find units renderer
TableColumn tableColumn = getColumnModel().getColumn(column);
Iterator<String> keys = unitRenderersByColumnName.keySet().iterator();
while(keys.hasNext()) {
String columnName = keys.next();
if(tableColumn.getHeaderValue().equals(columnName)) {
unitRenderer = unitRenderersByColumnName.get(columnName);
break;
}
}
// find base renderer
baseRenderer = tableColumn.getCellRenderer();
if (baseRenderer==null) {
keys = precisionRenderersByColumnName.keySet().iterator();
while(keys.hasNext()) {
String columnName = keys.next();
if(tableColumn.getHeaderValue().equals(columnName)) {
baseRenderer = precisionRenderersByColumnName.get(columnName);
break;
}
}
}
} catch(Exception ex) {}
// if no precision base renderer, use default
if (baseRenderer==null)
baseRenderer = getDefaultRenderer(getColumnClass(column));
// return unit renderer if defined
if (unitRenderer!=null) {
unitRenderer.setBaseRenderer(baseRenderer);
return unitRenderer;
}
return baseRenderer;
}
/**
* Gets the precision renderer, if any, for a given columnn name.
* Added by D Brown Dec 2010
*
* @param columnName the name
* @return the PrecisionRenderer, or null if none
*/
public TableCellRenderer getPrecisionRenderer(String columnName) {
return precisionRenderersByColumnName.get(columnName);
}
/**
* Sets the delay time for table refresh timer.
*
* @param delay the delay in millisecond
*/
public void setRefreshDelay(int delay) {
if(delay>0) {
refreshTimer.setDelay(delay);
refreshTimer.setInitialDelay(delay);
} else if(delay<=0) {
refreshTimer.stop();
}
refreshDelay = delay;
}
/**
* Refresh the data in the DataTable, as well as other changes to the table,
* such as row number visibility. Changes to the TableModels displayed in the
* table will not be visible until this method is called.
*/
public void refreshTable() {
if(refreshDelay>0) {
refreshTimer.start();
} else {
Runnable doRefreshTable = new Runnable() {
public synchronized void run() {
actionPerformed(null);
}
};
if(SwingUtilities.isEventDispatchThread()) {
doRefreshTable.run();
} else {
SwingUtilities.invokeLater(doRefreshTable);
}
}
}
/**
* Performs the action for the refresh timer and refreshTable() method
* by refreshing the data in the DataTable.
*
* @param evt
*/
public void actionPerformed(ActionEvent evt) {
// code added by D Brown to maintain column order and widths (Mar 2014)
TableColumnModel model = this.getColumnModel();
int colCount = model.getColumnCount();
int[] modelIndexes = new int[colCount];
int[] columnWidths = new int[colCount];
ArrayList<Object> columnNames = new ArrayList<Object>();
// save current order, widths and column names
for (int i=0; i<colCount; i++) {
TableColumn column = model.getColumn(i);
modelIndexes[i] = column.getModelIndex();
columnWidths[i] = column.getWidth();
columnNames.add(column.getHeaderValue());
}
// refresh table--this lays out columns in default order and widths
tableChanged(new TableModelEvent(dataTableModel, TableModelEvent.HEADER_ROW));
// deal with added and/or removed columns
int newCount = model.getColumnCount();
// create list of new column names
ArrayList<Object> newColumnNames = new ArrayList<Object>();
for (int i=0; i<newCount; i++) {
TableColumn column = model.getColumn(i);
newColumnNames.add(column.getHeaderValue());
}
// determine which column(s) were removed
TreeSet<Integer> removedIndexes = new TreeSet<Integer>();
for (int i=0; i<colCount; i++) {
if (!newColumnNames.contains(columnNames.get(i))) {
removedIndexes.add(modelIndexes[i]);
}
}
// determine which column(s) were added
TreeSet<Integer> addedIndexes = new TreeSet<Integer>();
for (int i=0; i<newCount; i++) {
if (!columnNames.contains(newColumnNames.get(i))) {
addedIndexes.add(i);
}
}
// rebuild modelIndex and columnWidth arrays
while (!removedIndexes.isEmpty()) {
int n = removedIndexes.last();
removedIndexes.remove(n);
int[] newModelIndexes = new int[colCount-1];
int[] newColumnWidths = new int[colCount-1];
int k = 0;
for (int i=0; i<colCount; i++) {
if (modelIndexes[i]==n) continue;
if (modelIndexes[i]>n) {
newModelIndexes[k] = modelIndexes[i]-1;
}
else {
newModelIndexes[k] = modelIndexes[i];
}
newColumnWidths[k] = columnWidths[i];
k++;
}
modelIndexes = newModelIndexes;
columnWidths = newColumnWidths;
colCount = modelIndexes.length;
}
while (!addedIndexes.isEmpty()) {
int n = addedIndexes.first();
addedIndexes.remove(n);
int[] newModelIndexes = new int[colCount+1];
int[] newColumnWidths = new int[colCount+1];
for (int i=0; i<colCount; i++) {
if (modelIndexes[i]>=n) {
newModelIndexes[i] = modelIndexes[i]+1;
}
else {
newModelIndexes[i] = modelIndexes[i];
}
newColumnWidths[i] = columnWidths[i];
}
// add new columns at end and assign them the default width
newModelIndexes[colCount] = n;
newColumnWidths[colCount] = model.getColumn(n).getWidth();
modelIndexes = newModelIndexes;
columnWidths = newColumnWidths;
colCount = modelIndexes.length;
}
// restore column order
outer: for (int targetIndex=0; targetIndex<colCount; targetIndex++) {
// find column with modelIndex and move to targetIndex
for (int i=0; i<colCount; i++) {
if (model.getColumn(i).getModelIndex()==modelIndexes[targetIndex]) {
model.moveColumn(i, targetIndex);
continue outer;
}
}
}
// restore column widths
for (int i=0; i<columnWidths.length; i++) {
model.getColumn(i).setPreferredWidth(columnWidths[i]);
model.getColumn(i).setWidth(columnWidths[i]);
}
}
/**
* Add a TableModel object to the table model list.
*
* @param tableModel
*/
public void add(TableModel tableModel) {
dataTableModel.add(tableModel);
}
/**
* Remove a TableModel object from the table model list.
*
* @param tableModel
*/
public void remove(TableModel tableModel) {
dataTableModel.remove(tableModel);
}
/**
* Remove all TableModels from the table model list.
*/
public void clear() {
dataTableModel.clear();
}
private static class DataTableElement {
TableModel tableModel;
boolean columnVisibilities[]; // boolean values indicating if a column is visible
int stride = 1; // data stride in the DataTable view
/**
* Constructor DataTableElement
*
* @param t
*/
public DataTableElement(TableModel t) {
tableModel = t;
}
/**
* Method setStride
*
* @param _stride
*/
public void setStride(int _stride) {
stride = _stride;
}
/**
* Method setColumnVisible
*
* @param columnIndex
* @param visible
*/
public void setColumnVisible(int columnIndex, boolean visible) {
ensureCapacity(columnIndex+1);
columnVisibilities[columnIndex] = visible;
}
/**
* Method getStride
*
* @return
*/
public int getStride() {
return stride;
}
/**
* Method getColumnVisibilities
*
* @return
*/
public boolean[] getColumnVisibilities() {
return columnVisibilities;
}
/**
* Method getColumnCount
*
* @return
*/
public int getColumnCount() {
int count = 0;
int numberOfColumns = tableModel.getColumnCount();
ensureCapacity(numberOfColumns);
for(int i = 0; i<numberOfColumns; i++) {
boolean visible = columnVisibilities[i];
if(visible) {
count++;
}
}
return count;
}
/**
* Method getValueAt
*
* @param rowIndex
* @param columnIndex
* @return
*/
public Object getValueAt(int rowIndex, int columnIndex) {
return tableModel.getValueAt(rowIndex, columnIndex);
}
/**
* Method getColumnName
*
* @param columnIndex
* @return
*/
public String getColumnName(int columnIndex) {
return tableModel.getColumnName(columnIndex);
}
/**
* Method getColumnClass
*
* @param columnIndex
* @return
*/
public Class<?> getColumnClass(int columnIndex) {
return tableModel.getColumnClass(columnIndex);
}
/**
* Method getRowCount
*
* @return
*/
public int getRowCount() {
return tableModel.getRowCount();
}
private void ensureCapacity(int minimumCapacity) {
if(columnVisibilities==null) {
columnVisibilities = new boolean[(minimumCapacity*3)/2+1];
Arrays.fill(columnVisibilities, true);
} else if(columnVisibilities.length<minimumCapacity) {
boolean[] temp = columnVisibilities;
columnVisibilities = new boolean[(minimumCapacity*3)/2+1];
System.arraycopy(temp, 0, columnVisibilities, 0, temp.length);
Arrays.fill(columnVisibilities, temp.length, columnVisibilities.length, true);
}
}
}
/*
* DefaultDataTableModel acts on behalf of the TableModels that the DataTable contains. It combines
* data from these multiple sources and allows the DataTable to display data
* is if the data were from a single source.
*
* @author jgould
* @created February 21, 2002
*/
protected static class DefaultDataTableModel implements DataTableModel {
ArrayList<DataTableElement> dataTableElements = new ArrayList<DataTableElement>();
boolean rowNumberVisible = false;
/**
* Method setColumnVisible
*
* @param tableModel
* @param columnIndex
* @param b
*/
public void setColumnVisible(TableModel tableModel, int columnIndex, boolean b) {
DataTableElement dte = findElementContaining(tableModel);
dte.setColumnVisible(columnIndex, b);
}
/**
* Method setStride
*
* @param tableModel
* @param stride
*/
public void setStride(TableModel tableModel, int stride) {
DataTableElement dte = findElementContaining(tableModel);
dte.setStride(stride);
}
/**
* Method setRowNumberVisible
*
* @param _rowNumberVisible
*/
public void setRowNumberVisible(boolean _rowNumberVisible) {
rowNumberVisible = _rowNumberVisible;
}
/**
* Method setValueAt modified by Doug Brown 12/19/2013
*
* @param value
* @param rowIndex
* @param columnIndex
*/
public void setValueAt(Object value, int rowIndex, int columnIndex) {
if(dataTableElements.size()==0) {
return;
}
if(rowNumberVisible) {
if(columnIndex==0) {
return;
}
}
ModelFilterResult mfr = ModelFilterResult.find(rowNumberVisible, dataTableElements, columnIndex);
DataTableElement dte = mfr.tableElement;
int stride = dte.getStride();
rowIndex = rowIndex*stride;
if(rowIndex>=dte.getRowCount()) {
return;
}
dte.tableModel.setValueAt(value, rowIndex, mfr.column);
}
/**
* Method isRowNumberVisible
*
* @return
*/
public boolean isRowNumberVisible() {
return rowNumberVisible;
}
/**
* Method getColumnName
*
* @param columnIndex
* @return the name
*/
public String getColumnName(int columnIndex) {
if((dataTableElements.size()==0)&&!rowNumberVisible) {
return null;
}
if(rowNumberVisible) {
if(columnIndex==0) {
return rowName;
}
}
ModelFilterResult mfr = ModelFilterResult.find(rowNumberVisible, dataTableElements, columnIndex);
DataTableElement dte = mfr.tableElement;
String name = dte.getColumnName(mfr.column);
return name;
}
/**
* Method getRowCount
*
* @return
*/
public int getRowCount() {
int rowCount = 0;
for(int i = 0; i<dataTableElements.size(); i++) {
DataTableElement dte = dataTableElements.get(i);
int stride = dte.getStride();
rowCount = Math.max(rowCount, (dte.getRowCount()+stride-1)/stride);
}
return rowCount;
}
/**
* Method getColumnCount
*
* @return
*/
public int getColumnCount() {
int columnCount = 0;
for(int i = 0; i<dataTableElements.size(); i++) {
DataTableElement dte = dataTableElements.get(i);
columnCount += dte.getColumnCount();
}
if(rowNumberVisible) {
columnCount++;
}
return columnCount;
}
/**
* Method getValueAt
*
* @param rowIndex
* @param columnIndex
* @return
*/
public Object getValueAt(int rowIndex, int columnIndex) {
if(dataTableElements.size()==0) {
return null;
}
if(rowNumberVisible) {
if(columnIndex==0) {
return new Integer(rowIndex);
}
}
ModelFilterResult mfr = ModelFilterResult.find(rowNumberVisible, dataTableElements, columnIndex);
DataTableElement dte = mfr.tableElement;
int stride = dte.getStride();
rowIndex = rowIndex*stride;
if(rowIndex>=dte.getRowCount()) {
return null;
}
return dte.getValueAt(rowIndex, mfr.column);
}
/**
* Method getColumnClass
*
* @param columnIndex
* @return
*/
public Class<?> getColumnClass(int columnIndex) {
if(rowNumberVisible) {
if(columnIndex==0) {
return Integer.class;
}
}
if((columnIndex==0)&&rowNumberVisible) {
columnIndex--;
}
ModelFilterResult mfr = ModelFilterResult.find(rowNumberVisible, dataTableElements, columnIndex);
DataTableElement dte = mfr.tableElement;
return dte.getColumnClass(mfr.column);
}
/**
* Method isCellEditable
*
* @param rowIndex
* @param columnIndex
* @return true if editable
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
/**
* Method remove
*
* @param tableModel
*/
public void remove(TableModel tableModel) {
DataTableElement dte = findElementContaining(tableModel);
dataTableElements.remove(dte);
}
/**
* Method clear
*/
public void clear() {
dataTableElements.clear();
}
/**
* Method add
*
* @param tableModel
*/
public void add(TableModel tableModel) {
dataTableElements.add(new DataTableElement(tableModel));
}
/**
* Method addTableModelListener
*
* @param l
*/
public void addTableModelListener(TableModelListener l) {}
/**
* Method removeTableModelListener
*
* @param l
*/
public void removeTableModelListener(TableModelListener l) {}
/**
* returns the DataTableElement that contains the specified TableModel
*
* @param tableModel
* @return Description of the Returned Value
*/
private DataTableElement findElementContaining(TableModel tableModel) {
for(int i = 0; i<dataTableElements.size(); i++) {
DataTableElement dte = dataTableElements.get(i);
if(dte.tableModel==tableModel) {
return dte;
}
}
return null;
}
}
private static class ModelFilterResult {
DataTableElement tableElement;
int column;
/**
* Constructor ModelFilterResult
*
* @param _tableElement
* @param _column
*/
public ModelFilterResult(DataTableElement _tableElement, int _column) {
tableElement = _tableElement;
column = _column;
}
/**
* Method find
*
* @param rowNumberVisible
* @param dataTableElements
* @param tableColumnIndex
* @return
*/
public static ModelFilterResult find(boolean rowNumberVisible, ArrayList<DataTableElement> dataTableElements, int tableColumnIndex) {
if(rowNumberVisible) {
tableColumnIndex--;
}
int totalColumns = 0;
for(int i = 0; i<dataTableElements.size(); i++) {
DataTableElement dte = dataTableElements.get(i);
dte.ensureCapacity(tableColumnIndex);
int columnCount = dte.getColumnCount();
totalColumns += columnCount;
if(totalColumns>tableColumnIndex) {
// int columnIndex = Math.abs(totalColumns - columnCount - tableColumnIndex);
int columnIndex = (columnCount+tableColumnIndex)-totalColumns;
boolean visible[] = dte.getColumnVisibilities();
for(int j = 0; j<tableColumnIndex; j++) {
if(!visible[j]) {
columnIndex++;
}
}
return new ModelFilterResult(dte, columnIndex);
}
}
return null; // this shouldn't happen
}
}
private class DataTableColumnModel extends DefaultTableColumnModel {
/**