001 /* AbstractTableModel.java -- 002 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package javax.swing.table; 040 041 import java.io.Serializable; 042 import java.util.EventListener; 043 044 import javax.swing.event.EventListenerList; 045 import javax.swing.event.TableModelEvent; 046 import javax.swing.event.TableModelListener; 047 048 /** 049 * A base class that can be used to create implementations of the 050 * {@link TableModel} interface. 051 * 052 * @author Andrew Selkirk 053 */ 054 public abstract class AbstractTableModel implements TableModel, Serializable 055 { 056 static final long serialVersionUID = -5798593159423650347L; 057 058 /** 059 * Storage for the listeners registered with this model. 060 */ 061 protected EventListenerList listenerList = new EventListenerList(); 062 063 /** 064 * Creates a default instance. 065 */ 066 public AbstractTableModel() 067 { 068 // no setup required here 069 } 070 071 /** 072 * Returns the name of the specified column. This method generates default 073 * names in a sequence (starting with column 0): A, B, C, ..., Z, AA, AB, 074 * AC, ..., AZ, BA, BB, BC, and so on. Subclasses may override this method 075 * to allow column names to be specified on some other basis. 076 * 077 * @param columnIndex the column index. 078 * 079 * @return The name of the column. 080 */ 081 public String getColumnName(int columnIndex) 082 { 083 StringBuffer buffer = new StringBuffer(); 084 while (columnIndex >= 0) 085 { 086 buffer.insert(0, (char) ('A' + columnIndex % 26)); 087 columnIndex = columnIndex / 26 - 1; 088 } 089 return buffer.toString(); 090 } 091 092 /** 093 * Return the index of the specified column, or <code>-1</code> if there is 094 * no column with the specified name. 095 * 096 * @param columnName the name of the column (<code>null</code> not permitted). 097 * 098 * @return The index of the column, -1 if not found. 099 * 100 * @see #getColumnName(int) 101 * @throws NullPointerException if <code>columnName</code> is 102 * <code>null</code>. 103 */ 104 public int findColumn(String columnName) 105 { 106 int count = getColumnCount(); 107 108 for (int index = 0; index < count; index++) 109 { 110 String name = getColumnName(index); 111 112 if (columnName.equals(name)) 113 return index; 114 } 115 116 // Unable to locate. 117 return -1; 118 } 119 120 /** 121 * Returns the <code>Class</code> for all <code>Object</code> instances 122 * in the specified column. 123 * 124 * @param columnIndex the column index. 125 * 126 * @return The class. 127 */ 128 public Class<?> getColumnClass(int columnIndex) 129 { 130 return Object.class; 131 } 132 133 /** 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. 138 * 139 * @param rowIndex the row index of the cell. 140 * @param columnIndex the column index of the cell. 141 * 142 * @return <code>false</code>. 143 */ 144 public boolean isCellEditable(int rowIndex, int columnIndex) 145 { 146 return false; 147 } 148 149 /** 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. 153 * 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. 157 */ 158 public void setValueAt(Object value, int rowIndex, int columnIndex) 159 { 160 // Do nothing... 161 } 162 163 /** 164 * Adds a listener to the table model. The listener will receive notification 165 * of all changes to the table model. 166 * 167 * @param listener the listener. 168 */ 169 public void addTableModelListener(TableModelListener listener) 170 { 171 listenerList.add(TableModelListener.class, listener); 172 } 173 174 /** 175 * Removes a listener from the table model so that it will no longer receive 176 * notification of changes to the table model. 177 * 178 * @param listener the listener to remove. 179 */ 180 public void removeTableModelListener(TableModelListener listener) 181 { 182 listenerList.remove(TableModelListener.class, listener); 183 } 184 185 /** 186 * Returns an array containing the listeners that have been added to the 187 * table model. 188 * 189 * @return Array of {@link TableModelListener} objects. 190 * 191 * @since 1.4 192 */ 193 public TableModelListener[] getTableModelListeners() 194 { 195 return (TableModelListener[]) 196 listenerList.getListeners(TableModelListener.class); 197 } 198 199 /** 200 * Sends a {@link TableModelEvent} to all registered listeners to inform 201 * them that the table data has changed. 202 */ 203 public void fireTableDataChanged() 204 { 205 fireTableChanged(new TableModelEvent(this, 0, Integer.MAX_VALUE)); 206 } 207 208 /** 209 * Sends a {@link TableModelEvent} to all registered listeners to inform 210 * them that the table structure has changed. 211 */ 212 public void fireTableStructureChanged() 213 { 214 fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW)); 215 } 216 217 /** 218 * Sends a {@link TableModelEvent} to all registered listeners to inform 219 * them that some rows have been inserted into the model. 220 * 221 * @param firstRow the index of the first row. 222 * @param lastRow the index of the last row. 223 */ 224 public void fireTableRowsInserted(int firstRow, int lastRow) 225 { 226 fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 227 TableModelEvent.ALL_COLUMNS, 228 TableModelEvent.INSERT)); 229 } 230 231 /** 232 * Sends a {@link TableModelEvent} to all registered listeners to inform 233 * them that some rows have been updated. 234 * 235 * @param firstRow the index of the first row. 236 * @param lastRow the index of the last row. 237 */ 238 public void fireTableRowsUpdated(int firstRow, int lastRow) 239 { 240 fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 241 TableModelEvent.ALL_COLUMNS, 242 TableModelEvent.UPDATE)); 243 } 244 245 /** 246 * Sends a {@link TableModelEvent} to all registered listeners to inform 247 * them that some rows have been deleted from the model. 248 * 249 * @param firstRow the index of the first row. 250 * @param lastRow the index of the last row. 251 */ 252 public void fireTableRowsDeleted(int firstRow, int lastRow) 253 { 254 fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 255 TableModelEvent.ALL_COLUMNS, 256 TableModelEvent.DELETE)); 257 } 258 259 /** 260 * Sends a {@link TableModelEvent} to all registered listeners to inform 261 * them that a single cell has been updated. 262 * 263 * @param row the row index. 264 * @param column the column index. 265 */ 266 public void fireTableCellUpdated(int row, int column) 267 { 268 fireTableChanged(new TableModelEvent(this, row, row, column)); 269 } 270 271 /** 272 * Sends the specified event to all registered listeners. 273 * 274 * @param event the event to send. 275 */ 276 public void fireTableChanged(TableModelEvent event) 277 { 278 int index; 279 TableModelListener listener; 280 Object[] list = listenerList.getListenerList(); 281 282 for (index = 0; index < list.length; index += 2) 283 { 284 listener = (TableModelListener) list [index + 1]; 285 listener.tableChanged(event); 286 } 287 } 288 289 /** 290 * Returns an array of listeners of the given type that are registered with 291 * this model. 292 * 293 * @param listenerType the listener class. 294 * 295 * @return An array of listeners (possibly empty). 296 */ 297 public <T extends EventListener> T[] getListeners(Class<T> listenerType) 298 { 299 return listenerList.getListeners(listenerType); 300 } 301 }