1. /*
  2. * @(#)AbstractTableModel.java 1.24 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.table;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.io.Serializable;
  11. /**
  12. * This abstract class provides default implementations for most of
  13. * the methods in the <B>TableModel</B> interface. It takes care of
  14. * the management of listners and provides some conveniences for generating
  15. * TableModelEvents and dispatching them to the listeners.
  16. * To create a concrete TableModel as a sublcass of
  17. * AbstractTableModel you need only provide implementations for the
  18. * following three methods:
  19. *
  20. * <pre>
  21. * public int getRowCount();
  22. * public int getColumnCount();
  23. * public Object getValueAt(int row, int column);
  24. * </pre>
  25. * <p>
  26. * <strong>Warning:</strong>
  27. * Serialized objects of this class will not be compatible with
  28. * future Swing releases. The current serialization support is appropriate
  29. * for short term storage or RMI between applications running the same
  30. * version of Swing. A future release of Swing will provide support for
  31. * long term persistence.
  32. *
  33. * @version 1.24 11/29/01
  34. * @author Alan Chung
  35. * @author Philip Milne
  36. */
  37. public abstract class AbstractTableModel implements TableModel, Serializable
  38. {
  39. //
  40. // Instance Variables
  41. //
  42. /** List of listeners */
  43. protected EventListenerList listenerList = new EventListenerList();
  44. //
  45. // Default Implementation of the Interface
  46. //
  47. /**
  48. * Return a default name for the column using spreadsheet conventions:
  49. * A, B, C, ... Z, AA, AB, etc.
  50. */
  51. public String getColumnName(int column) {
  52. String result = "";
  53. for (; column >= 0; column = column / 26 - 1) {
  54. result = (char)((char)(column%26)+'A') + result;
  55. }
  56. return result;
  57. }
  58. /**
  59. * Convenience method for locating columns by name.
  60. * Implementation is naive so this should be overridden if
  61. * this method is to be called often. This method is not
  62. * in the TableModel interface and is not used by the JTable.
  63. */
  64. public int findColumn(String columnName) {
  65. for (int i = 0; i < getColumnCount(); i++) {
  66. if (columnName.equals(getColumnName(i))) {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. /**
  73. * Returns Object.class by default
  74. */
  75. public Class getColumnClass(int columnIndex) {
  76. return Object.class;
  77. }
  78. /**
  79. * This default implementation returns false for all cells
  80. */
  81. public boolean isCellEditable(int rowIndex, int columnIndex) {
  82. return false;
  83. }
  84. /**
  85. * This empty implementation is provided so users don't have to implement
  86. * this method if their data model is not editable.
  87. */
  88. public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
  89. }
  90. //
  91. // Managing Listeners
  92. //
  93. /**
  94. * Add a listener to the list that's notified each time a change
  95. * to the data model occurs.
  96. *
  97. * @param l the TableModelListener
  98. */
  99. public void addTableModelListener(TableModelListener l) {
  100. listenerList.add(TableModelListener.class, l);
  101. }
  102. /**
  103. * Remove a listener from the list that's notified each time a
  104. * change to the data model occurs.
  105. *
  106. * @param l the TableModelListener
  107. */
  108. public void removeTableModelListener(TableModelListener l) {
  109. listenerList.remove(TableModelListener.class, l);
  110. }
  111. //
  112. // Fire methods
  113. //
  114. /**
  115. * Notify all listeners that all cell values in the table's rows may have changed.
  116. * The number of rows may also have changed and the JTable should redraw the
  117. * table from scratch. The structure of the table, ie. the order of the
  118. * columns is assumed to be the same.
  119. * @see TableModelEvent
  120. * @see EventListenerList
  121. */
  122. public void fireTableDataChanged() {
  123. fireTableChanged(new TableModelEvent(this));
  124. }
  125. /**
  126. * Notify all listeners that the table's structure has changed.
  127. * The number of columns in the table, and the names and types of
  128. * the new columns may be different from the previous state.
  129. * If the JTable recieves this event and its <I>autoCreateColumnsFromModel</I>
  130. * flag is set it discards any TableColumns that it had and reallocates
  131. * default ones in the order they appear in the model. This is the
  132. * same as calling <code>setModel(TableModel)</code> on the JTable.
  133. * @see TableModelEvent
  134. * @see EventListenerList
  135. */
  136. public void fireTableStructureChanged() {
  137. fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
  138. }
  139. /**
  140. * Notify all listeners that rows in the (inclusive) range
  141. * [<I>firstRow</I>, <I>lastRow</I>] have been inserted.
  142. * @see TableModelEvent
  143. * @see EventListenerList
  144. */
  145. public void fireTableRowsInserted(int firstRow, int lastRow) {
  146. fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
  147. TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
  148. }
  149. /**
  150. * Notify all listeners that rows in the (inclusive) range
  151. * [<I>firstRow</I>, <I>lastRow</I>] have been updated.
  152. * @see TableModelEvent
  153. * @see EventListenerList
  154. */
  155. public void fireTableRowsUpdated(int firstRow, int lastRow) {
  156. fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
  157. TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
  158. }
  159. /**
  160. * Notify all listeners that rows in the (inclusive) range
  161. * [<I>firstRow</I>, <I>lastRow</I>] have been deleted.
  162. * @see TableModelEvent
  163. * @see EventListenerList
  164. */
  165. public void fireTableRowsDeleted(int firstRow, int lastRow) {
  166. fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
  167. TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
  168. }
  169. /**
  170. * Notify all listeners that the value of the cell at (row, column)
  171. * has been updated.
  172. * @see TableModelEvent
  173. * @see EventListenerList
  174. */
  175. public void fireTableCellUpdated(int row, int column) {
  176. fireTableChanged(new TableModelEvent(this, row, row, column));
  177. }
  178. /**
  179. * Forward the given notification event to all TableModelListeners that registered
  180. * themselves as listeners for this table model.
  181. * @see #addTableModelListener
  182. * @see TableModelEvent
  183. * @see EventListenerList
  184. */
  185. public void fireTableChanged(TableModelEvent e) {
  186. // Guaranteed to return a non-null array
  187. Object[] listeners = listenerList.getListenerList();
  188. // Process the listeners last to first, notifying
  189. // those that are interested in this event
  190. for (int i = listeners.length-2; i>=0; i-=2) {
  191. if (listeners[i]==TableModelListener.class) {
  192. ((TableModelListener)listeners[i+1]).tableChanged(e);
  193. }
  194. }
  195. }
  196. } // End of class AbstractTableModel