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