01: package org.gui4j.core.swing;
02:
03: import javax.swing.table.*;
04: import java.util.Vector;
05:
06: public class RowHeaderDefaultTableModel extends DefaultTableModel
07: implements RowHeaderTableModel {
08: protected Vector rowIdentifiers;
09:
10: public RowHeaderDefaultTableModel() {
11: super ((Vector) null, 0);
12: initRowIdentifiers(0);
13: }
14:
15: public RowHeaderDefaultTableModel(int numRows, int numColumns) {
16: super (numRows, numColumns);
17: initRowIdentifiers(numRows);
18: }
19:
20: protected void initRowIdentifiers(int numRows) {
21: rowIdentifiers = new Vector(numRows);
22: rowIdentifiers.setSize(numRows);
23: }
24:
25: public String getRowName(int row) {
26: String result;
27: if (row < rowIdentifiers.size()) {
28: Object id = rowIdentifiers.elementAt(row);
29: if (id == null) {
30: result = null;
31: } else {
32: result = id.toString();
33: }
34: } else {
35: result = null;
36: }
37: return result;
38: }
39:
40: public void setRowName(int rowNumber, Object newName) {
41: if (rowNumber < rowIdentifiers.size()) {
42: rowIdentifiers.setElementAt(newName, rowNumber);
43: } else {
44: throw new ArrayIndexOutOfBoundsException(rowNumber);
45: }
46: }
47:
48: public void addRow(Vector rowData) {
49: addRow(null, rowData);
50: }
51:
52: public void addRow(Object[] rowData) {
53: addRow(null, convertToVector(rowData));
54: }
55:
56: public void addRow(Object rowName, Vector rowData) {
57: super .addRow(rowData);
58: rowIdentifiers.addElement(rowName);
59: }
60:
61: public void insertRow(int row, Vector rowData) {
62: insertRow(row, null, rowData);
63: }
64:
65: public void insertRow(int row, Object[] rowData) {
66: insertRow(row, null, convertToVector(rowData));
67: }
68:
69: public void insertRow(int row, Object rowName, Vector rowData) {
70: super .insertRow(row, rowData);
71: rowIdentifiers.insertElementAt(rowName, row);
72: }
73:
74: public void moveRow(int startIndex, int endIndex, int toIndex) {
75: super .moveRow(startIndex, endIndex, toIndex);
76:
77: if ((startIndex <= toIndex) && (toIndex <= endIndex)) {
78: return; // Nothing to move
79: }
80:
81: boolean shift = toIndex < startIndex;
82:
83: // Do the move by first removing the row, then reinserting it
84: for (int i = startIndex; i <= endIndex; i++) {
85: Object rowName = rowIdentifiers.elementAt(i);
86: rowIdentifiers.removeElementAt(i);
87: rowIdentifiers.insertElementAt(rowName, toIndex);
88:
89: if (shift) {
90: toIndex++;
91: }
92: }
93: }
94:
95: public void removeRow(int row) {
96: super.removeRow(row);
97: rowIdentifiers.removeElementAt(row);
98: }
99: }
|