1 /* AbstractTableModel.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.table;
41 import java.io.Serializable;
42 import java.util.EventListener;
44 import javax.swing.event.EventListenerList;
45 import javax.swing.event.TableModelEvent;
46 import javax.swing.event.TableModelListener;
49 * A base class that can be used to create implementations of the
50 * {@link TableModel} interface.
52 * @author Andrew Selkirk
54 public abstract class AbstractTableModel implements TableModel, Serializable
56 static final long serialVersionUID = -5798593159423650347L;
59 * Storage for the listeners registered with this model.
61 protected EventListenerList listenerList = new EventListenerList();
64 * Creates a default instance.
66 public AbstractTableModel()
68 // no setup required here
72 * Returns the name of the specified column. This method generates default
73 * names in a sequence (starting with column 0): A, B, C, ..., Z, AA, AB,
74 * AC, ..., AZ, BA, BB, BC, and so on. Subclasses may override this method
75 * to allow column names to be specified on some other basis.
77 * @param columnIndex the column index.
79 * @return The name of the column.
81 public String getColumnName(int columnIndex)
83 StringBuffer buffer = new StringBuffer();
84 while (columnIndex >= 0)
86 buffer.insert (0, (char) ('A' + columnIndex % 26));
87 columnIndex = columnIndex / 26 - 1;
89 return buffer.toString();
93 * Return the index of the specified column, or <code>-1</code> if there is
94 * no column with the specified name.
96 * @param columnName the name of the column (<code>null</code> not permitted).
98 * @return The index of the column, -1 if not found.
100 * @see #getColumnName(int)
101 * @throws NullPointerException if <code>columnName</code> is
104 public int findColumn(String columnName)
106 int count = getColumnCount();
108 for (int index = 0; index < count; index++)
110 String name = getColumnName(index);
112 if (columnName.equals(name))
121 * Returns the <code>Class</code> for all <code>Object</code> instances
122 * in the specified column.
124 * @param columnIndex the column index.
128 public Class getColumnClass(int columnIndex)
134 * Returns <code>true</code> if the specified cell is editable, and
135 * <code>false</code> if it is not. This implementation returns
136 * <code>false</code> for all arguments, subclasses should override the
137 * method if necessary.
139 * @param rowIndex the row index of the cell.
140 * @param columnIndex the column index of the cell.
142 * @return <code>false</code>.
144 public boolean isCellEditable(int rowIndex, int columnIndex)
150 * Sets the value of the given cell. This implementation ignores all
151 * arguments and does nothing, subclasses should override the
152 * method if necessary.
154 * @param value the new value (<code>null</code> permitted).
155 * @param rowIndex the row index of the cell.
156 * @param columnIndex the column index of the cell.
158 public void setValueAt(Object value, int rowIndex, int columnIndex)
164 * Adds a listener to the table model. The listener will receive notification
165 * of all changes to the table model.
167 * @param listener the listener.
169 public void addTableModelListener(TableModelListener listener)
171 listenerList.add(TableModelListener.class, listener);
175 * Removes a listener from the table model so that it will no longer receive
176 * notification of changes to the table model.
178 * @param listener the listener to remove.
180 public void removeTableModelListener(TableModelListener listener)
182 listenerList.remove(TableModelListener.class, listener);
186 * Returns an array containing the listeners that have been added to the
189 * @return Array of {@link TableModelListener} objects.
193 public TableModelListener[] getTableModelListeners()
195 return (TableModelListener[])
196 listenerList.getListeners(TableModelListener.class);
200 * Sends a {@link TableModelEvent} to all registered listeners to inform
201 * them that the table data has changed.
203 public void fireTableDataChanged()
205 fireTableChanged(new TableModelEvent(this, 0, Integer.MAX_VALUE));
209 * Sends a {@link TableModelEvent} to all registered listeners to inform
210 * them that the table structure has changed.
212 public void fireTableStructureChanged()
214 fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
218 * Sends a {@link TableModelEvent} to all registered listeners to inform
219 * them that some rows have been inserted into the model.
221 * @param firstRow the index of the first row.
222 * @param lastRow the index of the last row.
224 public void fireTableRowsInserted (int firstRow, int lastRow)
226 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
227 TableModelEvent.ALL_COLUMNS,
228 TableModelEvent.INSERT));
232 * Sends a {@link TableModelEvent} to all registered listeners to inform
233 * them that some rows have been updated.
235 * @param firstRow the index of the first row.
236 * @param lastRow the index of the last row.
238 public void fireTableRowsUpdated (int firstRow, int lastRow)
240 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
241 TableModelEvent.ALL_COLUMNS,
242 TableModelEvent.UPDATE));
246 * Sends a {@link TableModelEvent} to all registered listeners to inform
247 * them that some rows have been deleted from the model.
249 * @param firstRow the index of the first row.
250 * @param lastRow the index of the last row.
252 public void fireTableRowsDeleted(int firstRow, int lastRow)
254 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
255 TableModelEvent.ALL_COLUMNS,
256 TableModelEvent.DELETE));
260 * Sends a {@link TableModelEvent} to all registered listeners to inform
261 * them that a single cell has been updated.
263 * @param row the row index.
264 * @param column the column index.
266 public void fireTableCellUpdated (int row, int column)
268 fireTableChanged(new TableModelEvent(this, row, row, column));
272 * Sends the specified event to all registered listeners.
274 * @param event the event to send.
276 public void fireTableChanged(TableModelEvent event)
279 TableModelListener listener;
280 Object[] list = listenerList.getListenerList();
282 for (index = 0; index < list.length; index += 2)
284 listener = (TableModelListener) list [index + 1];
285 listener.tableChanged (event);
290 * Returns an array of listeners of the given type that are registered with
293 * @param listenerType the listener class.
295 * @return An array of listeners (possibly empty).
297 public EventListener[] getListeners(Class listenerType)
299 return listenerList.getListeners(listenerType);