libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / swing / table / AbstractTableModel.java
blob743e5d4dc2169e022a8345e426d0a9a806bbf89c
1 /* AbstractTableModel.java --
2 Copyright (C) 2002, 2004, 2005, 2006, 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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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 gnu.java.lang.CPStringBuilder;
43 import java.io.Serializable;
44 import java.util.EventListener;
46 import javax.swing.event.EventListenerList;
47 import javax.swing.event.TableModelEvent;
48 import javax.swing.event.TableModelListener;
50 /**
51 * A base class that can be used to create implementations of the
52 * {@link TableModel} interface.
54 * @author Andrew Selkirk
56 public abstract class AbstractTableModel implements TableModel, Serializable
58 static final long serialVersionUID = -5798593159423650347L;
60 /**
61 * Storage for the listeners registered with this model.
63 protected EventListenerList listenerList = new EventListenerList();
65 /**
66 * Creates a default instance.
68 public AbstractTableModel()
70 // no setup required here
73 /**
74 * Returns the name of the specified column. This method generates default
75 * names in a sequence (starting with column 0): A, B, C, ..., Z, AA, AB,
76 * AC, ..., AZ, BA, BB, BC, and so on. Subclasses may override this method
77 * to allow column names to be specified on some other basis.
79 * @param columnIndex the column index.
81 * @return The name of the column.
83 public String getColumnName(int columnIndex)
85 CPStringBuilder buffer = new CPStringBuilder();
86 while (columnIndex >= 0)
88 buffer.insert(0, (char) ('A' + columnIndex % 26));
89 columnIndex = columnIndex / 26 - 1;
91 return buffer.toString();
94 /**
95 * Return the index of the specified column, or <code>-1</code> if there is
96 * no column with the specified name.
98 * @param columnName the name of the column (<code>null</code> not permitted).
100 * @return The index of the column, -1 if not found.
102 * @see #getColumnName(int)
103 * @throws NullPointerException if <code>columnName</code> is
104 * <code>null</code>.
106 public int findColumn(String columnName)
108 int count = getColumnCount();
110 for (int index = 0; index < count; index++)
112 String name = getColumnName(index);
114 if (columnName.equals(name))
115 return index;
118 // Unable to locate.
119 return -1;
123 * Returns the <code>Class</code> for all <code>Object</code> instances
124 * in the specified column.
126 * @param columnIndex the column index.
128 * @return The class.
130 public Class<?> getColumnClass(int columnIndex)
132 return Object.class;
136 * Returns <code>true</code> if the specified cell is editable, and
137 * <code>false</code> if it is not. This implementation returns
138 * <code>false</code> for all arguments, subclasses should override the
139 * method if necessary.
141 * @param rowIndex the row index of the cell.
142 * @param columnIndex the column index of the cell.
144 * @return <code>false</code>.
146 public boolean isCellEditable(int rowIndex, int columnIndex)
148 return false;
152 * Sets the value of the given cell. This implementation ignores all
153 * arguments and does nothing, subclasses should override the
154 * method if necessary.
156 * @param value the new value (<code>null</code> permitted).
157 * @param rowIndex the row index of the cell.
158 * @param columnIndex the column index of the cell.
160 public void setValueAt(Object value, int rowIndex, int columnIndex)
162 // Do nothing...
166 * Adds a listener to the table model. The listener will receive notification
167 * of all changes to the table model.
169 * @param listener the listener.
171 public void addTableModelListener(TableModelListener listener)
173 listenerList.add(TableModelListener.class, listener);
177 * Removes a listener from the table model so that it will no longer receive
178 * notification of changes to the table model.
180 * @param listener the listener to remove.
182 public void removeTableModelListener(TableModelListener listener)
184 listenerList.remove(TableModelListener.class, listener);
188 * Returns an array containing the listeners that have been added to the
189 * table model.
191 * @return Array of {@link TableModelListener} objects.
193 * @since 1.4
195 public TableModelListener[] getTableModelListeners()
197 return (TableModelListener[])
198 listenerList.getListeners(TableModelListener.class);
202 * Sends a {@link TableModelEvent} to all registered listeners to inform
203 * them that the table data has changed.
205 public void fireTableDataChanged()
207 fireTableChanged(new TableModelEvent(this, 0, Integer.MAX_VALUE));
211 * Sends a {@link TableModelEvent} to all registered listeners to inform
212 * them that the table structure has changed.
214 public void fireTableStructureChanged()
216 fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
220 * Sends a {@link TableModelEvent} to all registered listeners to inform
221 * them that some rows have been inserted into the model.
223 * @param firstRow the index of the first row.
224 * @param lastRow the index of the last row.
226 public void fireTableRowsInserted(int firstRow, int lastRow)
228 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
229 TableModelEvent.ALL_COLUMNS,
230 TableModelEvent.INSERT));
234 * Sends a {@link TableModelEvent} to all registered listeners to inform
235 * them that some rows have been updated.
237 * @param firstRow the index of the first row.
238 * @param lastRow the index of the last row.
240 public void fireTableRowsUpdated(int firstRow, int lastRow)
242 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
243 TableModelEvent.ALL_COLUMNS,
244 TableModelEvent.UPDATE));
248 * Sends a {@link TableModelEvent} to all registered listeners to inform
249 * them that some rows have been deleted from the model.
251 * @param firstRow the index of the first row.
252 * @param lastRow the index of the last row.
254 public void fireTableRowsDeleted(int firstRow, int lastRow)
256 fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
257 TableModelEvent.ALL_COLUMNS,
258 TableModelEvent.DELETE));
262 * Sends a {@link TableModelEvent} to all registered listeners to inform
263 * them that a single cell has been updated.
265 * @param row the row index.
266 * @param column the column index.
268 public void fireTableCellUpdated(int row, int column)
270 fireTableChanged(new TableModelEvent(this, row, row, column));
274 * Sends the specified event to all registered listeners.
276 * @param event the event to send.
278 public void fireTableChanged(TableModelEvent event)
280 int index;
281 TableModelListener listener;
282 Object[] list = listenerList.getListenerList();
284 for (index = 0; index < list.length; index += 2)
286 listener = (TableModelListener) list [index + 1];
287 listener.tableChanged(event);
292 * Returns an array of listeners of the given type that are registered with
293 * this model.
295 * @param listenerType the listener class.
297 * @return An array of listeners (possibly empty).
299 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
301 return listenerList.getListeners(listenerType);