KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
33 import java.util.EventListener JavaDoc;
34
35 import nextapp.echo2.app.event.EventListenerList;
36 import nextapp.echo2.app.event.TableModelEvent;
37 import nextapp.echo2.app.event.TableModelListener;
38
39 /**
40  * An abstract implementation of a <code>TableModel</code>.
41  * This class provides the following conveniences for <code>TableModel</code>
42  * implementation development:
43  * <ul>
44  * <li>event listener management and notification</li>
45  * <li>a generic implementation of <code>getColumnClass()</code> which
46  * returns <code>Object.class</code></li>
47  * <li>a generic implementation of <code>getColumnName()</code> which
48  * returns "spreadsheet-style" column names, i.e.,
49  * A, B, C...Y, Z, AA, AB, AC....</li>
50  * </ul>
51  *
52  * @see DefaultTableModel
53  */

54 public abstract class AbstractTableModel
55 implements Serializable JavaDoc, TableModel {
56
57     private EventListenerList listenerList = new EventListenerList();
58     
59     /**
60      * Default constructor.
61      */

62     public AbstractTableModel() {
63         super();
64     }
65     
66     /**
67      * @see nextapp.echo2.app.table.TableModel#addTableModelListener(nextapp.echo2.app.event.TableModelListener)
68      */

69     public void addTableModelListener(TableModelListener l) {
70         listenerList.addListener(TableModelListener.class, l);
71     }
72     
73     /**
74      * Notifies <code>TableModelListener</code>s that the contents of the cell
75      * at the specified coordinate were changed.
76      *
77      * @param column the column index
78      * @param row the row index
79      */

80     public void fireTableCellUpdated(int column, int row) {
81         fireTableChanged(new TableModelEvent(this, column, row, row, TableModelEvent.UPDATE));
82     }
83     
84     /**
85      * Notifies <code>TableModelListener</code>s that the content of the table
86      * (possibly including the number of rows) was changed, but that the table's
87      * structure has remained intact.
88      */

89     public void fireTableDataChanged() {
90         fireTableChanged(new TableModelEvent(this));
91     }
92     
93     /**
94      * Notifies <code>TableModelListener</code>s that rows from
95      * <code>firstRow</code> to <code>lastRow</code> were deleted.
96      *
97      * @param firstRow the index of the first deleted row
98      * @param lastRow the index of the last deleted row
99      */

100     public void fireTableRowsDeleted(int firstRow, int lastRow) {
101         fireTableChanged(new TableModelEvent(this, TableModelEvent.ALL_COLUMNS, firstRow, lastRow, TableModelEvent.DELETE));
102     }
103     
104     /**
105      * Notifies <code>TableModelListener</code>s that the rows from
106      * <code>firstRow</code> to <code>lastRow</code> were inserted.
107      *
108      * @param firstRow the index of the first inserted row
109      * @param lastRow the index of the last inserted row
110      */

111     public void fireTableRowsInserted(int firstRow, int lastRow) {
112         fireTableChanged(new TableModelEvent(this, TableModelEvent.ALL_COLUMNS, firstRow, lastRow, TableModelEvent.INSERT));
113     }
114     
115     /**
116      * Notifies <code>TableModelListener</code>s that the data in the rows
117      * from <code>firstRow</code> to <code>lastRow</code> was updated.
118      * in the specified rows was updated.
119      *
120      * @param firstRow the index of the first inserted row
121      * @param lastRow the index of the last inserted row
122      */

123     public void fireTableRowsUpdated(int firstRow, int lastRow) {
124         fireTableChanged(new TableModelEvent(this, TableModelEvent.ALL_COLUMNS, firstRow, lastRow, TableModelEvent.UPDATE));
125     }
126     
127     /**
128      * Notifies <code>TableModelListener</code> that all data in the table may
129      * have changed, including the size and structure of the table.
130      */

131     public void fireTableStructureChanged() {
132         fireTableChanged(new TableModelEvent(this, TableModelEvent.ALL_COLUMNS, TableModelEvent.HEADER_ROW,
133                 TableModelEvent.HEADER_ROW, TableModelEvent.STRUCTURE_CHANGED));
134     }
135     
136     /**
137      * Notifies <code>TableModelListener</code>s of the specified event.
138      *
139      * @param e the event
140      */

141     public void fireTableChanged(TableModelEvent e) {
142         EventListener JavaDoc[] listeners = listenerList.getListeners(TableModelListener.class);
143         
144         for (int index = 0; index < listeners.length; ++index) {
145             ((TableModelListener) listeners[index]).tableChanged(e);
146         }
147     }
148
149     /**
150      * Returns <code>Object.class</code>
151      *
152      * @see nextapp.echo2.app.table.TableModel#getColumnClass(int)
153      */

154     public Class JavaDoc getColumnClass(int column) {
155         return Object JavaDoc.class;
156     }
157
158     /**
159      * Returns column names using a "spreadsheet-style" convention, i.e.,
160      * A, B, C...Y, Z, AA, AB, AC...
161      *
162      * @see nextapp.echo2.app.table.TableModel#getColumnName(int)
163      */

164     public String JavaDoc getColumnName(int column) {
165         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
166         int value = column;
167         do {
168             int digit = value % 26;
169             sb.insert(0, (char) ('A' + digit));
170             value = value / 26 - 1;
171         } while (value >= 0);
172         
173         return sb.toString();
174     }
175     
176     /**
177      * Returns the <code>EventListenerList</code> used to register listeners.
178      *
179      * @return the <code>EventListenerList</code>
180      */

181     public EventListenerList getEventListenerList() {
182         return listenerList;
183     }
184     
185     /**
186      * @see nextapp.echo2.app.table.TableModel#removeTableModelListener(nextapp.echo2.app.event.TableModelListener)
187      */

188     public void removeTableModelListener(TableModelListener l) {
189         listenerList.removeListener(TableModelListener.class, l);
190     }
191 }
192
Popular Tags