001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.table;
027
028 import javax.swing.*;
029 import javax.swing.event.*;
030 import java.io.Serializable;
031 import java.util.EventListener;
032
033 /**
034 * This abstract class provides default implementations for most of
035 * the methods in the <code>TableModel</code> interface. It takes care of
036 * the management of listeners and provides some conveniences for generating
037 * <code>TableModelEvents</code> and dispatching them to the listeners.
038 * To create a concrete <code>TableModel</code> as a subclass of
039 * <code>AbstractTableModel</code> you need only provide implementations
040 * for the following three methods:
041 *
042 * <pre>
043 * public int getRowCount();
044 * public int getColumnCount();
045 * public Object getValueAt(int row, int column);
046 * </pre>
047 * <p>
048 * <strong>Warning:</strong>
049 * Serialized objects of this class will not be compatible with
050 * future Swing releases. The current serialization support is
051 * appropriate for short term storage or RMI between applications running
052 * the same version of Swing. As of 1.4, support for long term storage
053 * of all JavaBeans<sup><font size="-2">TM</font></sup>
054 * has been added to the <code>java.beans</code> package.
055 * Please see {@link java.beans.XMLEncoder}.
056 *
057 * @version 1.48 05/05/07
058 * @author Alan Chung
059 * @author Philip Milne
060 */
061 public abstract class AbstractTableModel implements TableModel,
062 Serializable {
063 //
064 // Instance Variables
065 //
066
067 /** List of listeners */
068 protected EventListenerList listenerList = new EventListenerList();
069
070 //
071 // Default Implementation of the Interface
072 //
073
074 /**
075 * Returns a default name for the column using spreadsheet conventions:
076 * A, B, C, ... Z, AA, AB, etc. If <code>column</code> cannot be found,
077 * returns an empty string.
078 *
079 * @param column the column being queried
080 * @return a string containing the default name of <code>column</code>
081 */
082 public String getColumnName(int column) {
083 String result = "";
084 for (; column >= 0; column = column / 26 - 1) {
085 result = (char) ((char) (column % 26) + 'A') + result;
086 }
087 return result;
088 }
089
090 /**
091 * Returns a column given its name.
092 * Implementation is naive so this should be overridden if
093 * this method is to be called often. This method is not
094 * in the <code>TableModel</code> interface and is not used by the
095 * <code>JTable</code>.
096 *
097 * @param columnName string containing name of column to be located
098 * @return the column with <code>columnName</code>, or -1 if not found
099 */
100 public int findColumn(String columnName) {
101 for (int i = 0; i < getColumnCount(); i++) {
102 if (columnName.equals(getColumnName(i))) {
103 return i;
104 }
105 }
106 return -1;
107 }
108
109 /**
110 * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
111 *
112 * @param columnIndex the column being queried
113 * @return the Object.class
114 */
115 public Class<?> getColumnClass(int columnIndex) {
116 return Object.class;
117 }
118
119 /**
120 * Returns false. This is the default implementation for all cells.
121 *
122 * @param rowIndex the row being queried
123 * @param columnIndex the column being queried
124 * @return false
125 */
126 public boolean isCellEditable(int rowIndex, int columnIndex) {
127 return false;
128 }
129
130 /**
131 * This empty implementation is provided so users don't have to implement
132 * this method if their data model is not editable.
133 *
134 * @param aValue value to assign to cell
135 * @param rowIndex row of cell
136 * @param columnIndex column of cell
137 */
138 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
139 }
140
141 //
142 // Managing Listeners
143 //
144
145 /**
146 * Adds a listener to the list that's notified each time a change
147 * to the data model occurs.
148 *
149 * @param l the TableModelListener
150 */
151 public void addTableModelListener(TableModelListener l) {
152 listenerList.add(TableModelListener.class, l);
153 }
154
155 /**
156 * Removes a listener from the list that's notified each time a
157 * change to the data model occurs.
158 *
159 * @param l the TableModelListener
160 */
161 public void removeTableModelListener(TableModelListener l) {
162 listenerList.remove(TableModelListener.class, l);
163 }
164
165 /**
166 * Returns an array of all the table model listeners
167 * registered on this model.
168 *
169 * @return all of this model's <code>TableModelListener</code>s
170 * or an empty
171 * array if no table model listeners are currently registered
172 *
173 * @see #addTableModelListener
174 * @see #removeTableModelListener
175 *
176 * @since 1.4
177 */
178 public TableModelListener[] getTableModelListeners() {
179 return (TableModelListener[]) listenerList
180 .getListeners(TableModelListener.class);
181 }
182
183 //
184 // Fire methods
185 //
186
187 /**
188 * Notifies all listeners that all cell values in the table's
189 * rows may have changed. The number of rows may also have changed
190 * and the <code>JTable</code> should redraw the
191 * table from scratch. The structure of the table (as in the order of the
192 * columns) is assumed to be the same.
193 *
194 * @see TableModelEvent
195 * @see EventListenerList
196 * @see javax.swing.JTable#tableChanged(TableModelEvent)
197 */
198 public void fireTableDataChanged() {
199 fireTableChanged(new TableModelEvent(this ));
200 }
201
202 /**
203 * Notifies all listeners that the table's structure has changed.
204 * The number of columns in the table, and the names and types of
205 * the new columns may be different from the previous state.
206 * If the <code>JTable</code> receives this event and its
207 * <code>autoCreateColumnsFromModel</code>
208 * flag is set it discards any table columns that it had and reallocates
209 * default columns in the order they appear in the model. This is the
210 * same as calling <code>setModel(TableModel)</code> on the
211 * <code>JTable</code>.
212 *
213 * @see TableModelEvent
214 * @see EventListenerList
215 */
216 public void fireTableStructureChanged() {
217 fireTableChanged(new TableModelEvent(this ,
218 TableModelEvent.HEADER_ROW));
219 }
220
221 /**
222 * Notifies all listeners that rows in the range
223 * <code>[firstRow, lastRow]</code>, inclusive, have been inserted.
224 *
225 * @param firstRow the first row
226 * @param lastRow the last row
227 *
228 * @see TableModelEvent
229 * @see EventListenerList
230 *
231 */
232 public void fireTableRowsInserted(int firstRow, int lastRow) {
233 fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
234 TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
235 }
236
237 /**
238 * Notifies all listeners that rows in the range
239 * <code>[firstRow, lastRow]</code>, inclusive, have been updated.
240 *
241 * @param firstRow the first row
242 * @param lastRow the last row
243 *
244 * @see TableModelEvent
245 * @see EventListenerList
246 */
247 public void fireTableRowsUpdated(int firstRow, int lastRow) {
248 fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
249 TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
250 }
251
252 /**
253 * Notifies all listeners that rows in the range
254 * <code>[firstRow, lastRow]</code>, inclusive, have been deleted.
255 *
256 * @param firstRow the first row
257 * @param lastRow the last row
258 *
259 * @see TableModelEvent
260 * @see EventListenerList
261 */
262 public void fireTableRowsDeleted(int firstRow, int lastRow) {
263 fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
264 TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
265 }
266
267 /**
268 * Notifies all listeners that the value of the cell at
269 * <code>[row, column]</code> has been updated.
270 *
271 * @param row row of cell which has been updated
272 * @param column column of cell which has been updated
273 * @see TableModelEvent
274 * @see EventListenerList
275 */
276 public void fireTableCellUpdated(int row, int column) {
277 fireTableChanged(new TableModelEvent(this , row, row, column));
278 }
279
280 /**
281 * Forwards the given notification event to all
282 * <code>TableModelListeners</code> that registered
283 * themselves as listeners for this table model.
284 *
285 * @param e the event to be forwarded
286 *
287 * @see #addTableModelListener
288 * @see TableModelEvent
289 * @see EventListenerList
290 */
291 public void fireTableChanged(TableModelEvent e) {
292 // Guaranteed to return a non-null array
293 Object[] listeners = listenerList.getListenerList();
294 // Process the listeners last to first, notifying
295 // those that are interested in this event
296 for (int i = listeners.length - 2; i >= 0; i -= 2) {
297 if (listeners[i] == TableModelListener.class) {
298 ((TableModelListener) listeners[i + 1]).tableChanged(e);
299 }
300 }
301 }
302
303 /**
304 * Returns an array of all the objects currently registered
305 * as <code><em>Foo</em>Listener</code>s
306 * upon this <code>AbstractTableModel</code>.
307 * <code><em>Foo</em>Listener</code>s are registered using the
308 * <code>add<em>Foo</em>Listener</code> method.
309 *
310 * <p>
311 *
312 * You can specify the <code>listenerType</code> argument
313 * with a class literal,
314 * such as
315 * <code><em>Foo</em>Listener.class</code>.
316 * For example, you can query a
317 * model <code>m</code>
318 * for its table model listeners with the following code:
319 *
320 * <pre>TableModelListener[] tmls = (TableModelListener[])(m.getListeners(TableModelListener.class));</pre>
321 *
322 * If no such listeners exist, this method returns an empty array.
323 *
324 * @param listenerType the type of listeners requested; this parameter
325 * should specify an interface that descends from
326 * <code>java.util.EventListener</code>
327 * @return an array of all objects registered as
328 * <code><em>Foo</em>Listener</code>s on this component,
329 * or an empty array if no such
330 * listeners have been added
331 * @exception ClassCastException if <code>listenerType</code>
332 * doesn't specify a class or interface that implements
333 * <code>java.util.EventListener</code>
334 *
335 * @see #getTableModelListeners
336 *
337 * @since 1.3
338 */
339 public <T extends EventListener> T[] getListeners(
340 Class<T> listenerType) {
341 return listenerList.getListeners(listenerType);
342 }
343 } // End of class AbstractTableModel
|