KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > table > AbstractTableModel


1 /*
2  * @(#)AbstractTableModel.java 1.41 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.table;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import java.io.Serializable JavaDoc;
13 import java.util.EventListener JavaDoc;
14
15 /**
16  * This abstract class provides default implementations for most of
17  * the methods in the <code>TableModel</code> interface. It takes care of
18  * the management of listeners and provides some conveniences for generating
19  * <code>TableModelEvents</code> and dispatching them to the listeners.
20  * To create a concrete <code>TableModel</code> as a subclass of
21  * <code>AbstractTableModel</code> you need only provide implementations
22  * for the following three methods:
23  *
24  * <pre>
25  * public int getRowCount();
26  * public int getColumnCount();
27  * public Object getValueAt(int row, int column);
28  * </pre>
29  * <p>
30  * <strong>Warning:</strong>
31  * Serialized objects of this class will not be compatible with
32  * future Swing releases. The current serialization support is
33  * appropriate for short term storage or RMI between applications running
34  * the same version of Swing. As of 1.4, support for long term storage
35  * of all JavaBeans<sup><font size="-2">TM</font></sup>
36  * has been added to the <code>java.beans</code> package.
37  * Please see {@link java.beans.XMLEncoder}.
38  *
39  * @version 1.41 05/05/04
40  * @author Alan Chung
41  * @author Philip Milne
42  */

43 public abstract class AbstractTableModel implements TableModel JavaDoc, Serializable JavaDoc
44 {
45 //
46
// Instance Variables
47
//
48

49     /** List of listeners */
50     protected EventListenerList listenerList = new EventListenerList();
51
52 //
53
// Default Implementation of the Interface
54
//
55

56     /**
57      * Returns a default name for the column using spreadsheet conventions:
58      * A, B, C, ... Z, AA, AB, etc. If <code>column</code> cannot be found,
59      * returns an empty string.
60      *
61      * @param column the column being queried
62      * @return a string containing the default name of <code>column</code>
63      */

64     public String JavaDoc getColumnName(int column) {
65     String JavaDoc result = "";
66     for (; column >= 0; column = column / 26 - 1) {
67         result = (char)((char)(column%26)+'A') + result;
68     }
69         return result;
70     }
71
72     /**
73      * Returns a column given its name.
74      * Implementation is naive so this should be overridden if
75      * this method is to be called often. This method is not
76      * in the <code>TableModel</code> interface and is not used by the
77      * <code>JTable</code>.
78      *
79      * @param columnName string containing name of column to be located
80      * @return the column with <code>columnName</code>, or -1 if not found
81      */

82     public int findColumn(String JavaDoc columnName) {
83         for (int i = 0; i < getColumnCount(); i++) {
84             if (columnName.equals(getColumnName(i))) {
85                 return i;
86             }
87         }
88         return -1;
89     }
90
91     /**
92      * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
93      *
94      * @param columnIndex the column being queried
95      * @return the Object.class
96      */

97     public Class JavaDoc<?> getColumnClass(int columnIndex) {
98     return Object JavaDoc.class;
99     }
100
101     /**
102      * Returns false. This is the default implementation for all cells.
103      *
104      * @param rowIndex the row being queried
105      * @param columnIndex the column being queried
106      * @return false
107      */

108     public boolean isCellEditable(int rowIndex, int columnIndex) {
109     return false;
110     }
111
112     /**
113      * This empty implementation is provided so users don't have to implement
114      * this method if their data model is not editable.
115      *
116      * @param aValue value to assign to cell
117      * @param rowIndex row of cell
118      * @param columnIndex column of cell
119      */

120     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
121     }
122
123
124 //
125
// Managing Listeners
126
//
127

128     /**
129      * Adds a listener to the list that's notified each time a change
130      * to the data model occurs.
131      *
132      * @param l the TableModelListener
133      */

134     public void addTableModelListener(TableModelListener l) {
135     listenerList.add(TableModelListener.class, l);
136     }
137
138     /**
139      * Removes a listener from the list that's notified each time a
140      * change to the data model occurs.
141      *
142      * @param l the TableModelListener
143      */

144     public void removeTableModelListener(TableModelListener l) {
145     listenerList.remove(TableModelListener.class, l);
146     }
147
148     /**
149      * Returns an array of all the table model listeners
150      * registered on this model.
151      *
152      * @return all of this model's <code>TableModelListener</code>s
153      * or an empty
154      * array if no table model listeners are currently registered
155      *
156      * @see #addTableModelListener
157      * @see #removeTableModelListener
158      *
159      * @since 1.4
160      */

161     public TableModelListener[] getTableModelListeners() {
162         return (TableModelListener[])listenerList.getListeners(
163                 TableModelListener.class);
164     }
165
166 //
167
// Fire methods
168
//
169

170     /**
171      * Notifies all listeners that all cell values in the table's
172      * rows may have changed. The number of rows may also have changed
173      * and the <code>JTable</code> should redraw the
174      * table from scratch. The structure of the table (as in the order of the
175      * columns) is assumed to be the same.
176      *
177      * @see TableModelEvent
178      * @see EventListenerList
179      * @see javax.swing.JTable#tableChanged(TableModelEvent)
180      */

181     public void fireTableDataChanged() {
182         fireTableChanged(new TableModelEvent(this));
183     }
184
185     /**
186      * Notifies all listeners that the table's structure has changed.
187      * The number of columns in the table, and the names and types of
188      * the new columns may be different from the previous state.
189      * If the <code>JTable</code> receives this event and its
190      * <code>autoCreateColumnsFromModel</code>
191      * flag is set it discards any table columns that it had and reallocates
192      * default columns in the order they appear in the model. This is the
193      * same as calling <code>setModel(TableModel)</code> on the
194      * <code>JTable</code>.
195      *
196      * @see TableModelEvent
197      * @see EventListenerList
198      */

199     public void fireTableStructureChanged() {
200         fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
201     }
202
203     /**
204      * Notifies all listeners that rows in the range
205      * <code>[firstRow, lastRow]</code>, inclusive, have been inserted.
206      *
207      * @param firstRow the first row
208      * @param lastRow the last row
209      *
210      * @see TableModelEvent
211      * @see EventListenerList
212      *
213      */

214     public void fireTableRowsInserted(int firstRow, int lastRow) {
215         fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
216                              TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
217     }
218
219     /**
220      * Notifies all listeners that rows in the range
221      * <code>[firstRow, lastRow]</code>, inclusive, have been updated.
222      *
223      * @param firstRow the first row
224      * @param lastRow the last row
225      *
226      * @see TableModelEvent
227      * @see EventListenerList
228      */

229     public void fireTableRowsUpdated(int firstRow, int lastRow) {
230         fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
231                              TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
232     }
233
234     /**
235      * Notifies all listeners that rows in the range
236      * <code>[firstRow, lastRow]</code>, inclusive, have been deleted.
237      *
238      * @param firstRow the first row
239      * @param lastRow the last row
240      *
241      * @see TableModelEvent
242      * @see EventListenerList
243      */

244     public void fireTableRowsDeleted(int firstRow, int lastRow) {
245         fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
246                              TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
247     }
248
249     /**
250      * Notifies all listeners that the value of the cell at
251      * <code>[row, column]</code> has been updated.
252      *
253      * @param row row of cell which has been updated
254      * @param column column of cell which has been updated
255      * @see TableModelEvent
256      * @see EventListenerList
257      */

258     public void fireTableCellUpdated(int row, int column) {
259         fireTableChanged(new TableModelEvent(this, row, row, column));
260     }
261
262     /**
263      * Forwards the given notification event to all
264      * <code>TableModelListeners</code> that registered
265      * themselves as listeners for this table model.
266      *
267      * @param e the event to be forwarded
268      *
269      * @see #addTableModelListener
270      * @see TableModelEvent
271      * @see EventListenerList
272      */

273     public void fireTableChanged(TableModelEvent e) {
274     // Guaranteed to return a non-null array
275
Object JavaDoc[] listeners = listenerList.getListenerList();
276     // Process the listeners last to first, notifying
277
// those that are interested in this event
278
for (int i = listeners.length-2; i>=0; i-=2) {
279         if (listeners[i]==TableModelListener.class) {
280         ((TableModelListener)listeners[i+1]).tableChanged(e);
281         }
282     }
283     }
284
285     /**
286      * Returns an array of all the objects currently registered
287      * as <code><em>Foo</em>Listener</code>s
288      * upon this <code>AbstractTableModel</code>.
289      * <code><em>Foo</em>Listener</code>s are registered using the
290      * <code>add<em>Foo</em>Listener</code> method.
291      *
292      * <p>
293      *
294      * You can specify the <code>listenerType</code> argument
295      * with a class literal,
296      * such as
297      * <code><em>Foo</em>Listener.class</code>.
298      * For example, you can query a
299      * model <code>m</code>
300      * for its table model listeners with the following code:
301      *
302      * <pre>TableModelListener[] tmls = (TableModelListener[])(m.getListeners(TableModelListener.class));</pre>
303      *
304      * If no such listeners exist, this method returns an empty array.
305      *
306      * @param listenerType the type of listeners requested; this parameter
307      * should specify an interface that descends from
308      * <code>java.util.EventListener</code>
309      * @return an array of all objects registered as
310      * <code><em>Foo</em>Listener</code>s on this component,
311      * or an empty array if no such
312      * listeners have been added
313      * @exception ClassCastException if <code>listenerType</code>
314      * doesn't specify a class or interface that implements
315      * <code>java.util.EventListener</code>
316      *
317      * @see #getTableModelListeners
318      *
319      * @since 1.3
320      */

321     public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
322     return listenerList.getListeners(listenerType);
323     }
324 } // End of class AbstractTableModel
325
Popular Tags