1. /*
  2. * @(#)TableModelEvent.java 1.15 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.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, reallcoate 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 appropriate
  37. * for short term storage or RMI between applications running the same
  38. * version of Swing. A future release of Swing will provide support for
  39. * long term persistence.
  40. *
  41. * @version 1.15 11/29/01
  42. * @author Alan Chung
  43. * @author Philip Milne
  44. * @see TableModel
  45. */
  46. public class TableModelEvent extends java.util.EventObject
  47. {
  48. /** Identifies the addtion of new rows or columns. */
  49. public static final int INSERT = 1;
  50. /** Identifies a change to existing data. */
  51. public static final int UPDATE = 0;
  52. /** Identifies the removal of rows or columns. */
  53. public static final int DELETE = -1;
  54. /** Identifies the header row. */
  55. public static final int HEADER_ROW = -1;
  56. /** Specifies all columns in a row or rows. */
  57. public static final int ALL_COLUMNS = -1;
  58. //
  59. // Instance Variables
  60. //
  61. protected int type;
  62. protected int firstRow;
  63. protected int lastRow;
  64. protected int column;
  65. //
  66. // Constructors
  67. //
  68. /**
  69. * All row data in the table has changed, listeners should discard any state
  70. * that was based on the rows and requery the TableModel to get the new
  71. * row count and all the appropriate values.
  72. * The JTable will repaint the entire visible region on recieving
  73. * this event, querying the model for the cell values that are visble.
  74. * The structure of the table ie, the column names, types and order
  75. * have not changed.
  76. */
  77. public TableModelEvent(TableModel source) {
  78. // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
  79. this(source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
  80. }
  81. /**
  82. * This row of data has been updated.
  83. * To denote the arrival of a completely new table with a different structure
  84. * use <code>HEADER_ROW</code> as the value for the <I>row</I>.
  85. * When the JTable recieves this event and its <I>autoCreateColumnsFromModel</I>
  86. * flag is set it discards any TableColumns that it had and reallocates
  87. * default ones in the order they appear in the model. This is the
  88. * same as calling <code>setModel(TableModel)</code> on the JTable.
  89. */
  90. public TableModelEvent(TableModel source, int row) {
  91. this(source, row, row, ALL_COLUMNS, UPDATE);
  92. }
  93. /**
  94. * The data in rows [<I>firstRow</I>, <I>lastRow</I>] have been updated.
  95. */
  96. public TableModelEvent(TableModel source, int firstRow, int lastRow) {
  97. this(source, firstRow, lastRow, ALL_COLUMNS, UPDATE);
  98. }
  99. /**
  100. * The cells in column <I>column</I> in the range
  101. * [<I>firstRow</I>, <I>lastRow</I>] have been updated.
  102. */
  103. public TableModelEvent(TableModel source, int firstRow, int lastRow, int column) {
  104. this(source, firstRow, lastRow, column, UPDATE);
  105. }
  106. /**
  107. * The cells from (firstRow, column) to (lastRow, column) have been changed.
  108. * The <I>column</I> refers to the column index of the cell in the model's
  109. * co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
  110. * specified range of rows are considered changed.
  111. * <p>
  112. * The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
  113. */
  114. public TableModelEvent(TableModel source, int firstRow, int lastRow, int column, int type) {
  115. super(source);
  116. this.firstRow = firstRow;
  117. this.lastRow = lastRow;
  118. this.column = column;
  119. this.type = type;
  120. }
  121. //
  122. // Querying Methods
  123. //
  124. /** Returns the first row that changed. HEADER_ROW means the meta data,
  125. * ie. names, types and order of the columns.
  126. */
  127. public int getFirstRow() { return firstRow; };
  128. /** Returns the last row that changed. */
  129. public int getLastRow() { return lastRow; };
  130. /**
  131. * Returns the column for the event. If the return
  132. * value is ALL_COLUMNS; it means every column in the specified
  133. * rows changed.
  134. */
  135. public int getColumn() { return column; };
  136. /**
  137. * Returns the type of event - one of: INSERT, UPDATE and DELETE.
  138. */
  139. public int getType() { return type; }
  140. }