1. /*
  2. * @(#)TableModelEvent.java 1.21 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.event;
  8. import java.util.EventObject;
  9. import javax.swing.table.*;
  10. /**
  11. * TableModelEvent is used to notify listeners that a table model
  12. * has changed. The model event describes changes to a TableModel
  13. * and all references to rows and columns are in the co-ordinate
  14. * system of the model.
  15. * Depending on the parameters used in the constructors, the TableModelevent
  16. * can be used to specify the following types of changes: <p>
  17. *
  18. * <pre>
  19. * TableModelEvent(source); // The data, ie. all rows changed
  20. * TableModelEvent(source, HEADER_ROW); // Structure change, reallocate TableColumns
  21. * TableModelEvent(source, 1); // Row 1 changed
  22. * TableModelEvent(source, 3, 6); // Rows 3 to 6 inclusive changed
  23. * TableModelEvent(source, 2, 2, 6); // Cell at (2, 6) changed
  24. * TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
  25. * TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted
  26. * </pre>
  27. *
  28. * It is possible to use other combinations of the parameters, not all of them
  29. * are meaningful. By subclassing, you can add other information, for example:
  30. * whether the event WILL happen or DID happen. This makes the specification
  31. * of rows in DELETE events more useful but has not been included in
  32. * the swing package as the JTable only needs post-event notification.
  33. * <p>
  34. * <strong>Warning:</strong>
  35. * Serialized objects of this class will not be compatible with
  36. * future Swing releases. The current serialization support is
  37. * appropriate for short term storage or RMI between applications running
  38. * the same version of Swing. As of 1.4, support for long term storage
  39. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  40. * has been added to the <code>java.beans</code> package.
  41. * Please see {@link java.beans.XMLEncoder}.
  42. *
  43. * @version 1.21 12/19/03
  44. * @author Alan Chung
  45. * @author Philip Milne
  46. * @see TableModel
  47. */
  48. public class TableModelEvent extends java.util.EventObject
  49. {
  50. /** Identifies the addtion of new rows or columns. */
  51. public static final int INSERT = 1;
  52. /** Identifies a change to existing data. */
  53. public static final int UPDATE = 0;
  54. /** Identifies the removal of rows or columns. */
  55. public static final int DELETE = -1;
  56. /** Identifies the header row. */
  57. public static final int HEADER_ROW = -1;
  58. /** Specifies all columns in a row or rows. */
  59. public static final int ALL_COLUMNS = -1;
  60. //
  61. // Instance Variables
  62. //
  63. protected int type;
  64. protected int firstRow;
  65. protected int lastRow;
  66. protected int column;
  67. //
  68. // Constructors
  69. //
  70. /**
  71. * All row data in the table has changed, listeners should discard any state
  72. * that was based on the rows and requery the <code>TableModel</code>
  73. * to get the new row count and all the appropriate values.
  74. * The <code>JTable</code> will repaint the entire visible region on
  75. * receiving this event, querying the model for the cell values that are visible.
  76. * The structure of the table ie, the column names, types and order
  77. * have not changed.
  78. */
  79. public TableModelEvent(TableModel source) {
  80. // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
  81. this(source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
  82. }
  83. /**
  84. * This row of data has been updated.
  85. * To denote the arrival of a completely new table with a different structure
  86. * use <code>HEADER_ROW</code> as the value for the <code>row</code>.
  87. * When the <code>JTable</code> receives this event and its
  88. * <code>autoCreateColumnsFromModel</code>
  89. * flag is set it discards any TableColumns that it had and reallocates
  90. * default ones in the order they appear in the model. This is the
  91. * same as calling <code>setModel(TableModel)</code> on the <code>JTable</code>.
  92. */
  93. public TableModelEvent(TableModel source, int row) {
  94. this(source, row, row, ALL_COLUMNS, UPDATE);
  95. }
  96. /**
  97. * The data in rows [<I>firstRow</I>, <I>lastRow</I>] have been updated.
  98. */
  99. public TableModelEvent(TableModel source, int firstRow, int lastRow) {
  100. this(source, firstRow, lastRow, ALL_COLUMNS, UPDATE);
  101. }
  102. /**
  103. * The cells in column <I>column</I> in the range
  104. * [<I>firstRow</I>, <I>lastRow</I>] have been updated.
  105. */
  106. public TableModelEvent(TableModel source, int firstRow, int lastRow, int column) {
  107. this(source, firstRow, lastRow, column, UPDATE);
  108. }
  109. /**
  110. * The cells from (firstRow, column) to (lastRow, column) have been changed.
  111. * The <I>column</I> refers to the column index of the cell in the model's
  112. * co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
  113. * specified range of rows are considered changed.
  114. * <p>
  115. * The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
  116. */
  117. public TableModelEvent(TableModel source, int firstRow, int lastRow, int column, int type) {
  118. super(source);
  119. this.firstRow = firstRow;
  120. this.lastRow = lastRow;
  121. this.column = column;
  122. this.type = type;
  123. }
  124. //
  125. // Querying Methods
  126. //
  127. /** Returns the first row that changed. HEADER_ROW means the meta data,
  128. * ie. names, types and order of the columns.
  129. */
  130. public int getFirstRow() { return firstRow; };
  131. /** Returns the last row that changed. */
  132. public int getLastRow() { return lastRow; };
  133. /**
  134. * Returns the column for the event. If the return
  135. * value is ALL_COLUMNS; it means every column in the specified
  136. * rows changed.
  137. */
  138. public int getColumn() { return column; };
  139. /**
  140. * Returns the type of event - one of: INSERT, UPDATE and DELETE.
  141. */
  142. public int getType() { return type; }
  143. }