001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.table;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * The default <code>TableModel</code> implementation.
037: */
038: public class DefaultTableModel extends AbstractTableModel {
039:
040: private List rows;
041: private List columnNames;
042:
043: /**
044: * Creates a new table model of 0x0 size.
045: */
046: public DefaultTableModel() {
047: super ();
048:
049: columnNames = new ArrayList();
050: rows = new ArrayList();
051: }
052:
053: /**
054: * Creates a new table model with the specified dimensions.
055: *
056: * @param columns the initial number of columns
057: * @param rows the initial number of rows
058: */
059: public DefaultTableModel(int columns, int rows) {
060: this ();
061:
062: setRowCount(rows);
063: setColumnCount(columns);
064: }
065:
066: /**
067: * Creates a new Table Model with the specified data and column names.
068: *
069: * @param data a two dimensional array containing the table data
070: * (the first index of the array represents the column index,
071: * and the second index represents the row index)
072: * @param names the column names
073: */
074: public DefaultTableModel(Object[][] data, Object[] names) {
075: super ();
076:
077: if (data == null) {
078: columnNames = new ArrayList();
079: rows = new ArrayList();
080: } else {
081: ArrayList rowList;
082: int height = data.length;
083: int width = 0;
084: if (height > 0 && data[0] != null) {
085: width = data[0].length;
086: }
087:
088: // Add column names
089: columnNames = new ArrayList(width);
090: for (int column = 0; column < width; ++column) {
091: columnNames.add(names[column]);
092: }
093:
094: // Add table data
095: rows = new ArrayList(height);
096: for (int row = 0; row < height; ++row) {
097: if (width != 0) {
098: rowList = new ArrayList(width);
099: for (int column = 0; column < width; ++column) {
100: rowList.add(data[row][column]);
101: }
102: rows.add(rowList);
103: }
104: }
105: }
106: }
107:
108: /**
109: * Adds a row containing the provided data to the end of the model.
110: *
111: * @param rowData the row data
112: */
113: public void addRow(Object[] rowData) {
114: insertRow(rows.size(), rowData);
115: }
116:
117: /**
118: * Deletes the specified row.
119: *
120: * @param row the row to delete
121: */
122: public void deleteRow(int row) {
123: rows.remove(row);
124: fireTableRowsDeleted(row, row);
125: }
126:
127: /**
128: * @see nextapp.echo2.app.table.TableModel#getColumnCount()
129: */
130: public int getColumnCount() {
131: return columnNames.size();
132: }
133:
134: /**
135: * @see nextapp.echo2.app.table.TableModel#getColumnName(int)
136: */
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: /**
149: * @see nextapp.echo2.app.table.TableModel#getRowCount()
150: */
151: public int getRowCount() {
152: return rows.size();
153: }
154:
155: /**
156: * @see nextapp.echo2.app.table.TableModel#getValueAt(int, int)
157: */
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(
172: "Table column " + column + " does not exist.");
173: }
174: } else {
175: throw new ArrayIndexOutOfBoundsException("Table row " + row
176: + " does not exist.");
177: }
178:
179: return value;
180: }
181:
182: /**
183: * Inserts a row containing the provided data.
184: *
185: * @param row the insertion index
186: * @param rowData the row data
187: */
188: public void insertRow(int row, Object[] rowData) {
189: int maxIndex = rowData.length > columnNames.size() ? columnNames
190: .size()
191: : rowData.length;
192: List rowList = new ArrayList(columnNames.size());
193:
194: for (int index = 0; index < maxIndex; ++index) {
195: rowList.add(rowData[index]);
196: }
197:
198: rows.add(row, rowList);
199:
200: fireTableRowsInserted(row, row);
201: }
202:
203: /**
204: * Sets the number of columns in the table.
205: * Empty columns will be added at the end of the table if the new column
206: * count exceeds the number of existing columns. Existing columns will be
207: * hidden if the number of existing columns exceeds the new column count.
208: *
209: * @param newValue the new column count
210: */
211: public void setColumnCount(int newValue) {
212: while (columnNames.size() > newValue) {
213: columnNames.remove(columnNames.size() - 1);
214: }
215:
216: while (columnNames.size() < newValue) {
217: columnNames.add(null);
218: }
219:
220: fireTableStructureChanged();
221: }
222:
223: /**
224: * Sets the name of the specified column.
225: *
226: * @param column the column index
227: * @param columnName the new column name
228: */
229: public void setColumnName(int column, String columnName) {
230: columnNames.set(column, columnName);
231: }
232:
233: /**
234: * Sets the number of rows in the table.
235: * Empty rows will be added at the end of the table if the new row
236: * count exceeds the number of existing rows. Existing rows will be
237: * hidden if the number of existing rows exceeds the new row count.
238: *
239: * @param newValue the new row count
240: */
241: public void setRowCount(int newValue) {
242: // Remove excess rows
243: while (rows.size() > newValue) {
244: rows.remove(rows.size() - 1);
245: }
246:
247: while (rows.size() < newValue) {
248: rows.add(null);
249: }
250:
251: fireTableDataChanged();
252: }
253:
254: /**
255: * Sets the contents of the table cell at the specified coordinate.
256: *
257: * @param newValue the new value
258: * @param column the column index
259: * @param row the row index
260: * @throws ArrayIndexOutOfBoundsException if the column or row index
261: * exceed the column or row count
262: */
263: public void setValueAt(Object newValue, int column, int row) {
264: if (rows.size() < row || columnNames.size() < column) {
265: throw new ArrayIndexOutOfBoundsException(
266: "Table coordinate (" + column + ", " + row
267: + ") does not exist");
268: }
269:
270: List rowList = (List) rows.get(row);
271: if (rowList == null && newValue != null) {
272: rowList = new ArrayList(columnNames.size());
273: rows.set(row, rowList);
274: }
275:
276: while (rowList.size() <= column) {
277: rowList.add(null);
278: }
279:
280: rowList.set(column, newValue);
281:
282: fireTableCellUpdated(column, row);
283: }
284: }
|