2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 David "Niki" ROULET
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author David ROULET [niki@nikiroo.be]
29 package be
.nikiroo
.jexer
;
31 import java
.util
.ArrayList
;
32 import java
.util
.Arrays
;
33 import java
.util
.Collection
;
35 import javax
.swing
.event
.TableModelListener
;
36 import javax
.swing
.table
.AbstractTableModel
;
37 import javax
.swing
.table
.TableModel
;
40 * The model of a {@link TTable}. It contains the data of the table and allows
43 * Note that you don't need to send it the representation of the data, but the
44 * data itself; {@link TTableCellRenderer} is the class responsible of
45 * representing that data (you can change the headers renderer on a
46 * {@link TTable} and the cells renderer on each of its {@link TTableColumn}).
48 * It works in a similar way to the Java Swing version of it.
52 public class TTableModel
implements TableModel
{
53 private TableModel model
;
56 * Create a new {@link TTableModel} with the given data inside.
61 public TTableModel(Object
[][] data
) {
66 * Create a new {@link TTableModel} with the given data inside.
72 final Collection
<?
extends Collection
<?
extends Object
>> data
) {
74 int maxItemsPerRow
= 0;
75 for (Collection
<?
extends Object
> rowOfData
: data
) {
76 maxItemsPerRow
= Math
.max(maxItemsPerRow
, rowOfData
.size());
80 final Object
[][] odata
= new Object
[data
.size()][maxItemsPerRow
];
81 for (Collection
<?
extends Object
> rowOfData
: data
) {
82 odata
[i
] = new String
[maxItemsPerRow
];
84 for (Object pieceOfData
: rowOfData
) {
85 odata
[i
][j
] = pieceOfData
;
91 final int maxItemsPerRowFinal
= maxItemsPerRow
;
92 this.model
= new AbstractTableModel() {
93 private static final long serialVersionUID
= 1L;
96 public Object
getValueAt(int rowIndex
, int columnIndex
) {
97 return odata
[rowIndex
][columnIndex
];
101 public int getRowCount() {
106 public int getColumnCount() {
107 return maxItemsPerRowFinal
;
113 public int getRowCount() {
114 return model
.getRowCount();
118 public int getColumnCount() {
119 return model
.getColumnCount();
123 public String
getColumnName(int columnIndex
) {
124 return model
.getColumnName(columnIndex
);
128 public Class
<?
> getColumnClass(int columnIndex
) {
129 return model
.getColumnClass(columnIndex
);
133 public boolean isCellEditable(int rowIndex
, int columnIndex
) {
134 return model
.isCellEditable(rowIndex
, columnIndex
);
138 public Object
getValueAt(int rowIndex
, int columnIndex
) {
139 return model
.getValueAt(rowIndex
, columnIndex
);
143 public void setValueAt(Object aValue
, int rowIndex
, int columnIndex
) {
144 model
.setValueAt(aValue
, rowIndex
, columnIndex
);
148 public void addTableModelListener(TableModelListener l
) {
149 model
.addTableModelListener(l
);
153 public void removeTableModelListener(TableModelListener l
) {
154 model
.removeTableModelListener(l
);
158 * Helper method to convert an array to a collection.
165 * @return the data in another format
167 static <T
> Collection
<Collection
<T
>> convert(T
[][] data
) {
168 Collection
<Collection
<T
>> dataCollection
= new ArrayList
<Collection
<T
>>(
170 for (T pieceOfData
[] : data
) {
171 dataCollection
.add(Arrays
.asList(pieceOfData
));
174 return dataCollection
;