1 7 8 package javax.swing.table; 9 10 import java.io.Serializable ; 11 import java.util.Vector ; 12 import java.util.Enumeration ; 13 import javax.swing.event.TableModelEvent ; 14 15 16 36 public class DefaultTableModel extends AbstractTableModel implements Serializable { 37 38 42 46 protected Vector dataVector; 47 48 49 protected Vector columnIdentifiers; 50 51 55 59 public DefaultTableModel() { 60 this(0, 0); 61 } 62 63 private static Vector newVector(int size) { 64 Vector v = new Vector (size); 65 v.setSize(size); 66 return v; 67 } 68 69 79 public DefaultTableModel(int rowCount, int columnCount) { 80 this(newVector(columnCount), rowCount); 81 } 82 83 97 public DefaultTableModel(Vector columnNames, int rowCount) { 98 setDataVector(newVector(rowCount), columnNames); 99 } 100 101 115 public DefaultTableModel(Object [] columnNames, int rowCount) { 116 this(convertToVector(columnNames), rowCount); 117 } 118 119 132 public DefaultTableModel(Vector data, Vector columnNames) { 133 setDataVector(data, columnNames); 134 } 135 136 148 public DefaultTableModel(Object [][] data, Object [] columnNames) { 149 setDataVector(data, columnNames); 150 } 151 152 167 public Vector getDataVector() { 168 return dataVector; 169 } 170 171 private static Vector nonNullVector(Vector v) { 172 return (v != null) ? v : new Vector (); 173 } 174 175 195 public void setDataVector(Vector dataVector, Vector columnIdentifiers) { 196 this.dataVector = nonNullVector(dataVector); 197 this.columnIdentifiers = nonNullVector(columnIdentifiers); 198 justifyRows(0, getRowCount()); 199 fireTableStructureChanged(); 200 } 201 202 213 public void setDataVector(Object [][] dataVector, Object [] columnIdentifiers) { 214 setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers)); 215 } 216 217 223 public void newDataAvailable(TableModelEvent event) { 224 fireTableChanged(event); 225 } 226 227 231 private void justifyRows(int from, int to) { 232 dataVector.setSize(getRowCount()); 237 238 for (int i = from; i < to; i++) { 239 if (dataVector.elementAt(i) == null) { 240 dataVector.setElementAt(new Vector (), i); 241 } 242 ((Vector )dataVector.elementAt(i)).setSize(getColumnCount()); 243 } 244 } 245 246 261 public void newRowsAdded(TableModelEvent e) { 262 justifyRows(e.getFirstRow(), e.getLastRow() + 1); 263 fireTableChanged(e); 264 } 265 266 272 public void rowsRemoved(TableModelEvent event) { 273 fireTableChanged(event); 274 } 275 276 279 288 public void setNumRows(int rowCount) { 289 int old = getRowCount(); 290 if (old == rowCount) { 291 return; 292 } 293 dataVector.setSize(rowCount); 294 if (rowCount <= old) { 295 fireTableRowsDeleted(rowCount, old-1); 296 } 297 else { 298 justifyRows(old, rowCount); 299 fireTableRowsInserted(old, rowCount-1); 300 } 301 } 302 303 311 public void setRowCount(int rowCount) { 312 setNumRows(rowCount); 313 } 314 315 322 public void addRow(Vector rowData) { 323 insertRow(getRowCount(), rowData); 324 } 325 326 333 public void addRow(Object [] rowData) { 334 addRow(convertToVector(rowData)); 335 } 336 337 346 public void insertRow(int row, Vector rowData) { 347 dataVector.insertElementAt(rowData, row); 348 justifyRows(row, row+1); 349 fireTableRowsInserted(row, row); 350 } 351 352 361 public void insertRow(int row, Object [] rowData) { 362 insertRow(row, convertToVector(rowData)); 363 } 364 365 private static int gcd(int i, int j) { 366 return (j == 0) ? i : gcd(j, i%j); 367 } 368 369 private static void rotate(Vector v, int a, int b, int shift) { 370 int size = b - a; 371 int r = size - shift; 372 int g = gcd(size, r); 373 for(int i = 0; i < g; i++) { 374 int to = i; 375 Object tmp = v.elementAt(a + to); 376 for(int from = (to + r) % size; from != i; from = (to + r) % size) { 377 v.setElementAt(v.elementAt(a + from), a + to); 378 to = from; 379 } 380 v.setElementAt(tmp, a + to); 381 } 382 } 383 384 412 public void moveRow(int start, int end, int to) { 413 int shift = to - start; 414 int first, last; 415 if (shift < 0) { 416 first = to; 417 last = end; 418 } 419 else { 420 first = start; 421 last = to + end - start; 422 } 423 rotate(dataVector, first, last + 1, shift); 424 425 fireTableRowsUpdated(first, last); 426 } 427 428 435 public void removeRow(int row) { 436 dataVector.removeElementAt(row); 437 fireTableRowsDeleted(row, row); 438 } 439 440 444 457 public void setColumnIdentifiers(Vector columnIdentifiers) { 458 setDataVector(dataVector, columnIdentifiers); 459 } 460 461 474 public void setColumnIdentifiers(Object [] newIdentifiers) { 475 setColumnIdentifiers(convertToVector(newIdentifiers)); 476 } 477 478 489 public void setColumnCount(int columnCount) { 490 columnIdentifiers.setSize(columnCount); 491 justifyRows(0, getRowCount()); 492 fireTableStructureChanged(); 493 } 494 495 505 public void addColumn(Object columnName) { 506 addColumn(columnName, (Vector )null); 507 } 508 509 522 public void addColumn(Object columnName, Vector columnData) { 523 columnIdentifiers.addElement(columnName); 524 if (columnData != null) { 525 int columnSize = columnData.size(); 526 if (columnSize > getRowCount()) { 527 dataVector.setSize(columnSize); 528 } 529 justifyRows(0, getRowCount()); 530 int newColumn = getColumnCount() - 1; 531 for(int i = 0; i < columnSize; i++) { 532 Vector row = (Vector )dataVector.elementAt(i); 533 row.setElementAt(columnData.elementAt(i), newColumn); 534 } 535 } 536 else { 537 justifyRows(0, getRowCount()); 538 } 539 540 fireTableStructureChanged(); 541 } 542 543 554 public void addColumn(Object columnName, Object [] columnData) { 555 addColumn(columnName, convertToVector(columnData)); 556 } 557 558 562 566 public int getRowCount() { 567 return dataVector.size(); 568 } 569 570 574 public int getColumnCount() { 575 return columnIdentifiers.size(); 576 } 577 578 587 public String getColumnName(int column) { 588 Object id = null; 589 if (column < columnIdentifiers.size()) { 592 id = columnIdentifiers.elementAt(column); 593 } 594 return (id == null) ? super.getColumnName(column) 595 : id.toString(); 596 } 597 598 606 public boolean isCellEditable(int row, int column) { 607 return true; 608 } 609 610 620 public Object getValueAt(int row, int column) { 621 Vector rowVector = (Vector )dataVector.elementAt(row); 622 return rowVector.elementAt(column); 623 } 624 625 636 public void setValueAt(Object aValue, int row, int column) { 637 Vector rowVector = (Vector )dataVector.elementAt(row); 638 rowVector.setElementAt(aValue, column); 639 fireTableCellUpdated(row, column); 640 } 641 642 646 652 protected static Vector convertToVector(Object [] anArray) { 653 if (anArray == null) { 654 return null; 655 } 656 Vector v = new Vector (anArray.length); 657 for (int i=0; i < anArray.length; i++) { 658 v.addElement(anArray[i]); 659 } 660 return v; 661 } 662 663 669 protected static Vector convertToVector(Object [][] anArray) { 670 if (anArray == null) { 671 return null; 672 } 673 Vector v = new Vector (anArray.length); 674 for (int i=0; i < anArray.length; i++) { 675 v.addElement(convertToVector(anArray[i])); 676 } 677 return v; 678 } 679 680 } | Popular Tags |