KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > table > DefaultTableModel


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.table;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * The default <code>TableModel</code> implementation.
37  */

38 public class DefaultTableModel extends AbstractTableModel {
39     
40     private List JavaDoc rows;
41     private List JavaDoc columnNames;
42     
43     /**
44      * Creates a new table model of 0x0 size.
45      */

46     public DefaultTableModel() {
47         super();
48         
49         columnNames = new ArrayList JavaDoc();
50         rows = new ArrayList JavaDoc();
51     }
52     
53     /**
54      * Creates a new table model with the specified dimensions.
55      *
56      * @param columns the initial number of columns
57      * @param rows the initial number of rows
58      */

59     public DefaultTableModel(int columns, int rows) {
60         this();
61         
62         setRowCount(rows);
63         setColumnCount(columns);
64     }
65     
66     /**
67      * Creates a new Table Model with the specified data and column names.
68      *
69      * @param data a two dimensional array containing the table data
70      * (the first index of the array represents the column index,
71      * and the second index represents the row index)
72      * @param names the column names
73      */

74     public DefaultTableModel(Object JavaDoc[][] data, Object JavaDoc[] names) {
75         super();
76         
77         if (data == null) {
78             columnNames = new ArrayList JavaDoc();
79             rows = new ArrayList JavaDoc();
80         } else {
81             ArrayList JavaDoc 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             // Add column names
89
columnNames = new ArrayList JavaDoc(width);
90             for (int column = 0; column < width; ++column) {
91                 columnNames.add(names[column]);
92             }
93             
94             // Add table data
95
rows = new ArrayList JavaDoc(height);
96             for (int row = 0; row < height; ++row) {
97                 if (width != 0) {
98                     rowList = new ArrayList JavaDoc(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     /**
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 JavaDoc[] 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 JavaDoc getColumnName(int column) {
138         String JavaDoc name = null;
139         if (column < columnNames.size()) {
140             name = (String JavaDoc) 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 JavaDoc getValueAt(int column, int row) {
159         Object JavaDoc value;
160         List JavaDoc rowList;
161     
162         if (row < rows.size()) {
163             if (column < columnNames.size()) {
164                 rowList = (List JavaDoc) rows.get(row);
165                 if (rowList == null) {
166                     value = null;
167                 } else {
168                     value = rowList.get(column);
169                 }
170             } else {
171                 throw new ArrayIndexOutOfBoundsException JavaDoc("Table column " + column + " does not exist.");
172             }
173         } else {
174             throw new ArrayIndexOutOfBoundsException JavaDoc("Table row " + row + " does not exist.");
175         }
176         
177         return value;
178     }
179     
180     /**
181      * Inserts a row containing the provided data.
182      *
183      * @param row the insertion index
184      * @param rowData the row data
185      */

186     public void insertRow(int row, Object JavaDoc[] rowData) {
187         int maxIndex = rowData.length > columnNames.size() ? columnNames.size() : rowData.length;
188         List JavaDoc rowList = new ArrayList JavaDoc(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     /**
200      * Sets the number of columns in the table.
201      * Empty columns will be added at the end of the table if the new column
202      * count exceeds the number of existing columns. Existing columns will be
203      * hidden if the number of existing columns exceeds the new column count.
204      *
205      * @param newValue the new column count
206      */

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     /**
220      * Sets the name of the specified column.
221      *
222      * @param column the column index
223      * @param columnName the new column name
224      */

225     public void setColumnName(int column, String JavaDoc columnName) {
226         columnNames.set(column, columnName);
227     }
228
229     /**
230      * Sets the number of rows in the table.
231      * Empty rows will be added at the end of the table if the new row
232      * count exceeds the number of existing rows. Existing rows will be
233      * hidden if the number of existing rows exceeds the new row count.
234      *
235      * @param newValue the new row count
236      */

237     public void setRowCount(int newValue) {
238         // Remove excess rows
239
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     /**
251      * Sets the contents of the table cell at the specified coordinate.
252      *
253      * @param newValue the new value
254      * @param column the column index
255      * @param row the row index
256      * @throws ArrayIndexOutOfBoundsException if the column or row index
257      * exceed the column or row count
258      */

259     public void setValueAt(Object JavaDoc newValue, int column, int row) {
260         if (rows.size() < row || columnNames.size() < column) {
261             throw new ArrayIndexOutOfBoundsException JavaDoc("Table coordinate (" + column + ", " + row + ") does not exist");
262         }
263
264         List JavaDoc rowList = (List JavaDoc) rows.get(row);
265         if (rowList == null && newValue != null) {
266             rowList = new ArrayList JavaDoc(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