1 package com
.googlecode
.lanterna
.gui2
.table
;
6 * A {@code TableModel} contains the data model behind a table, here is where all the action cell values and header
11 public class TableModel
<V
> {
12 private final List
<String
> columns
;
13 private final List
<List
<V
>> rows
;
16 * Default constructor, creates a new model with same number of columns as labels supplied
17 * @param columnLabels Labels for the column headers
19 public TableModel(String
... columnLabels
) {
20 this.columns
= new ArrayList
<String
>(Arrays
.asList(columnLabels
));
21 this.rows
= new ArrayList
<List
<V
>>();
25 * Returns the number of columns in the model
26 * @return Number of columns in the model
28 public synchronized int getColumnCount() {
29 return columns
.size();
33 * Returns number of rows in the model
34 * @return Number of rows in the model
36 public synchronized int getRowCount() {
41 * Returns all rows in the model as a list of lists containing the data as elements
42 * @return All rows in the model as a list of lists containing the data as elements
44 public synchronized List
<List
<V
>> getRows() {
45 List
<List
<V
>> copy
= new ArrayList
<List
<V
>>();
46 for(List
<V
> row
: rows
) {
47 copy
.add(new ArrayList
<V
>(row
));
53 * Returns all column header label as a list of strings
54 * @return All column header label as a list of strings
56 public synchronized List
<String
> getColumnLabels() {
57 return new ArrayList
<String
>(columns
);
61 * Returns a row from the table as a list of the cell data
62 * @param index Index of the row to return
63 * @return Row from the table as a list of the cell data
65 public synchronized List
<V
> getRow(int index
) {
66 return new ArrayList
<V
>(rows
.get(index
));
70 * Adds a new row to the table model at the end
71 * @param values Data to associate with the new row, mapped column by column in order
74 public synchronized TableModel
<V
> addRow(V
... values
) {
75 addRow(Arrays
.asList(values
));
80 * Adds a new row to the table model at the end
81 * @param values Data to associate with the new row, mapped column by column in order
84 public synchronized TableModel
<V
> addRow(Collection
<V
> values
) {
85 insertRow(getRowCount(), values
);
90 * Inserts a new row to the table model at a particular index
91 * @param index Index the new row should have, 0 means the first row and <i>row count</i> will append the row at the
93 * @param values Data to associate with the new row, mapped column by column in order
96 public synchronized TableModel
<V
> insertRow(int index
, Collection
<V
> values
) {
97 ArrayList
<V
> list
= new ArrayList
<V
>(values
);
98 rows
.add(index
, list
);
103 * Removes a row at a particular index from the table model
104 * @param index Index of the row to remove
107 public synchronized TableModel
<V
> removeRow(int index
) {
113 * Returns the label of a column header
114 * @param index Index of the column to retrieve the header label for
115 * @return Label of the column selected
117 public synchronized String
getColumnLabel(int index
) {
118 return columns
.get(index
);
122 * Updates the label of a column header
123 * @param index Index of the column to update the header label for
124 * @param newLabel New label to assign to the column header
127 public synchronized TableModel
<V
> setColumnLabel(int index
, String newLabel
) {
128 columns
.set(index
, newLabel
);
133 * Adds a new column into the table model as the last column. You can optionally supply values for the existing rows
134 * through the {@code newColumnValues}.
135 * @param label Label for the header of the new column
136 * @param newColumnValues Optional values to assign to the existing rows, where the first element in the array will
137 * be the value of the first row and so on...
140 public synchronized TableModel
<V
> addColumn(String label
, V
[] newColumnValues
) {
141 return insertColumn(getColumnCount(), label
, newColumnValues
);
145 * Adds a new column into the table model at a specified index. You can optionally supply values for the existing
146 * rows through the {@code newColumnValues}.
147 * @param index Index for the new column
148 * @param label Label for the header of the new column
149 * @param newColumnValues Optional values to assign to the existing rows, where the first element in the array will
150 * be the value of the first row and so on...
153 public synchronized TableModel
<V
> insertColumn(int index
, String label
, V
[] newColumnValues
) {
154 columns
.add(index
, label
);
155 for(int i
= 0; i
< rows
.size(); i
++) {
156 List
<V
> row
= rows
.get(i
);
158 //Pad row with null if necessary
159 for(int j
= row
.size(); j
< index
; j
++) {
163 if(newColumnValues
!= null && i
< newColumnValues
.length
&& newColumnValues
[i
] != null) {
164 row
.add(index
, newColumnValues
[i
]);
167 row
.add(index
, null);
174 * Removes a column from the table model
175 * @param index Index of the column to remove
178 public synchronized TableModel
<V
> removeColumn(int index
) {
179 columns
.remove(index
);
180 for(List
<V
> row
: rows
) {
187 * Returns the cell value stored at a specific column/row coordinate.
188 * @param columnIndex Column index of the cell
189 * @param rowIndex Row index of the cell
190 * @return The data value stored in this cell
192 public synchronized V
getCell(int columnIndex
, int rowIndex
) {
193 if(rowIndex
< 0 || columnIndex
< 0) {
194 throw new IndexOutOfBoundsException("Invalid row or column index: " + rowIndex
+ " " + columnIndex
);
196 else if (rowIndex
>= getRowCount()) {
197 throw new IndexOutOfBoundsException("TableModel has " + getRowCount() + " rows, invalid access at rowIndex " + rowIndex
);
199 if(columnIndex
>= getColumnCount()) {
200 throw new IndexOutOfBoundsException("TableModel has " + columnIndex
+ " columns, invalid access at columnIndex " + columnIndex
);
202 return rows
.get(rowIndex
).get(columnIndex
);
206 * Updates the call value stored at a specific column/row coordinate.
207 * @param columnIndex Column index of the cell
208 * @param rowIndex Row index of the cell
209 * @param value New value to assign to the cell
212 public synchronized TableModel
<V
> setCell(int columnIndex
, int rowIndex
, V value
) {
213 getCell(columnIndex
, rowIndex
);
214 List
<V
> row
= rows
.get(rowIndex
);
216 //Pad row with null if necessary
217 for(int j
= row
.size(); j
< columnIndex
; j
++) {
221 V existingValue
= row
.get(columnIndex
);
222 if(existingValue
== value
) {
225 row
.set(columnIndex
, value
);