1 29 30 package nextapp.echo2.app.table; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 38 public class DefaultTableModel extends AbstractTableModel { 39 40 private List rows; 41 private List columnNames; 42 43 46 public DefaultTableModel() { 47 super(); 48 49 columnNames = new ArrayList (); 50 rows = new ArrayList (); 51 } 52 53 59 public DefaultTableModel(int columns, int rows) { 60 this(); 61 62 setRowCount(rows); 63 setColumnCount(columns); 64 } 65 66 74 public DefaultTableModel(Object [][] data, Object [] names) { 75 super(); 76 77 if (data == null) { 78 columnNames = new ArrayList (); 79 rows = new ArrayList (); 80 } else { 81 ArrayList rowList; 82 int height = data.length; 83 int width = 0; 84 if (height > 0 && data[0] != null) { 85 width = data[0].length; 86 } 87 88 columnNames = new ArrayList (width); 90 for (int column = 0; column < width; ++column) { 91 columnNames.add(names[column]); 92 } 93 94 rows = new ArrayList (height); 96 for (int row = 0; row < height; ++row) { 97 if (width != 0) { 98 rowList = new ArrayList (width); 99 for (int column = 0; column < width; ++column) { 100 rowList.add(data[row][column]); 101 } 102 rows.add(rowList); 103 } 104 } 105 } 106 } 107 108 113 public void addRow(Object [] rowData) { 114 insertRow(rows.size(), rowData); 115 } 116 117 122 public void deleteRow(int row) { 123 rows.remove(row); 124 fireTableRowsDeleted(row, row); 125 } 126 127 130 public int getColumnCount() { 131 return columnNames.size(); 132 } 133 134 137 public String getColumnName(int column) { 138 String name = null; 139 if (column < columnNames.size()) { 140 name = (String ) columnNames.get(column); 141 } 142 if (name == null) { 143 name = super.getColumnName(column); 144 } 145 return name; 146 } 147 148 151 public int getRowCount() { 152 return rows.size(); 153 } 154 155 158 public Object getValueAt(int column, int row) { 159 Object value; 160 List rowList; 161 162 if (row < rows.size()) { 163 if (column < columnNames.size()) { 164 rowList = (List ) rows.get(row); 165 if (rowList == null) { 166 value = null; 167 } else { 168 value = rowList.get(column); 169 } 170 } else { 171 throw new ArrayIndexOutOfBoundsException ("Table column " + column + " does not exist."); 172 } 173 } else { 174 throw new ArrayIndexOutOfBoundsException ("Table row " + row + " does not exist."); 175 } 176 177 return value; 178 } 179 180 186 public void insertRow(int row, Object [] rowData) { 187 int maxIndex = rowData.length > columnNames.size() ? columnNames.size() : rowData.length; 188 List rowList = new ArrayList (columnNames.size()); 189 190 for (int index = 0; index < maxIndex; ++index) { 191 rowList.add(rowData[index]); 192 } 193 194 rows.add(row, rowList); 195 196 fireTableRowsInserted(row, row); 197 } 198 199 207 public void setColumnCount(int newValue) { 208 while (columnNames.size() > newValue) { 209 columnNames.remove(columnNames.size() - 1); 210 } 211 212 while (columnNames.size() < newValue) { 213 columnNames.add(null); 214 } 215 216 fireTableStructureChanged(); 217 } 218 219 225 public void setColumnName(int column, String columnName) { 226 columnNames.set(column, columnName); 227 } 228 229 237 public void setRowCount(int newValue) { 238 while (rows.size() > newValue) { 240 rows.remove(rows.size() - 1); 241 } 242 243 while (rows.size() < newValue) { 244 rows.add(null); 245 } 246 247 fireTableDataChanged(); 248 } 249 250 259 public void setValueAt(Object newValue, int column, int row) { 260 if (rows.size() < row || columnNames.size() < column) { 261 throw new ArrayIndexOutOfBoundsException ("Table coordinate (" + column + ", " + row + ") does not exist"); 262 } 263 264 List rowList = (List ) rows.get(row); 265 if (rowList == null && newValue != null) { 266 rowList = new ArrayList (columnNames.size()); 267 rows.set(row, rowList); 268 } 269 270 while (rowList.size() <= column) { 271 rowList.add(null); 272 } 273 274 rowList.set(column, newValue); 275 276 fireTableCellUpdated(column, row); 277 } 278 } 279 | Popular Tags |