Source for javax.swing.table.AbstractTableModel

   1: /* AbstractTableModel.java --
   2:    Copyright (C) 2002, 2004, 2005, 2006,  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing.table;
  40: 
  41: import java.io.Serializable;
  42: import java.util.EventListener;
  43: 
  44: import javax.swing.event.EventListenerList;
  45: import javax.swing.event.TableModelEvent;
  46: import javax.swing.event.TableModelListener;
  47: 
  48: /**
  49:  * A base class that can be used to create implementations of the 
  50:  * {@link TableModel} interface.
  51:  * 
  52:  * @author Andrew Selkirk
  53:  */
  54: public abstract class AbstractTableModel implements TableModel, Serializable
  55: {
  56:   static final long serialVersionUID = -5798593159423650347L;
  57: 
  58:   /**
  59:    * Storage for the listeners registered with this model.
  60:    */
  61:   protected EventListenerList listenerList = new EventListenerList();
  62: 
  63:   /**
  64:    * Creates a default instance.
  65:    */
  66:   public AbstractTableModel()
  67:   {
  68:     // no setup required here
  69:   }
  70: 
  71:   /**
  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. 
  76:    *
  77:    * @param columnIndex  the column index.
  78:    *
  79:    * @return The name of the column.
  80:    */
  81:   public String getColumnName(int columnIndex)
  82:   {
  83:     StringBuffer buffer = new StringBuffer();
  84:     while (columnIndex >= 0)
  85:       {
  86:         buffer.insert(0, (char) ('A' + columnIndex % 26));
  87:         columnIndex = columnIndex / 26 - 1;
  88:       }
  89:     return buffer.toString();
  90:   }
  91: 
  92:   /**
  93:    * Return the index of the specified column, or <code>-1</code> if there is
  94:    * no column with the specified name.
  95:    *
  96:    * @param columnName  the name of the column (<code>null</code> not permitted).
  97:    *
  98:    * @return The index of the column, -1 if not found.
  99:    * 
 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: }