1. /*
  2. * @(#)TableModelEvent.java 1.16 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.event;
  11. import java.util.EventObject;
  12. import javax.swing.table.*;
  13. /**
  14. * TableModelEvent is used to notify listeners that a table model
  15. * has changed. The model event describes changes to a TableModel
  16. * and all references to rows and columns are in the co-ordinate
  17. * system of the model.
  18. * Depending on the parameters used in the constructors, the TableModelevent
  19. * can be used to specify the following types of changes: <p>
  20. *
  21. * <pre>
  22. * TableModelEvent(source); // The data, ie. all rows changed
  23. * TableModelEvent(source, HEADER_ROW); // Structure change, reallcoate TableColumns
  24. * TableModelEvent(source, 1); // Row 1 changed
  25. * TableModelEvent(source, 3, 6); // Rows 3 to 6 inclusive changed
  26. * TableModelEvent(source, 2, 2, 6); // Cell at (2, 6) changed
  27. * TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
  28. * TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted
  29. * </pre>
  30. *
  31. * It is possible to use other combinations of the parameters, not all of them
  32. * are meaningful. By subclassing, you can add other information, for example:
  33. * whether the event WILL happen or DID happen. This makes the specification
  34. * of rows in DELETE events more useful but has not been included in
  35. * the swing package as the JTable only needs post-event notification.
  36. * <p>
  37. * <strong>Warning:</strong>
  38. * Serialized objects of this class will not be compatible with
  39. * future Swing releases. The current serialization support is appropriate
  40. * for short term storage or RMI between applications running the same
  41. * version of Swing. A future release of Swing will provide support for
  42. * long term persistence.
  43. *
  44. * @version 1.16 02/02/00
  45. * @author Alan Chung
  46. * @author Philip Milne
  47. * @see TableModel
  48. */
  49. public class TableModelEvent extends java.util.EventObject
  50. {
  51. /** Identifies the addtion of new rows or columns. */
  52. public static final int INSERT = 1;
  53. /** Identifies a change to existing data. */
  54. public static final int UPDATE = 0;
  55. /** Identifies the removal of rows or columns. */
  56. public static final int DELETE = -1;
  57. /** Identifies the header row. */
  58. public static final int HEADER_ROW = -1;
  59. /** Specifies all columns in a row or rows. */
  60. public static final int ALL_COLUMNS = -1;
  61. //
  62. // Instance Variables
  63. //
  64. protected int type;
  65. protected int firstRow;
  66. protected int lastRow;
  67. protected int column;
  68. //
  69. // Constructors
  70. //
  71. /**
  72. * All row data in the table has changed, listeners should discard any state
  73. * that was based on the rows and requery the TableModel to get the new
  74. * row count and all the appropriate values.
  75. * The JTable will repaint the entire visible region on recieving
  76. * this event, querying the model for the cell values that are visble.
  77. * The structure of the table ie, the column names, types and order
  78. * have not changed.
  79. */
  80. public TableModelEvent(TableModel source) {
  81. // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
  82. this(source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
  83. }
  84. /**
  85. * This row of data has been updated.
  86. * To denote the arrival of a completely new table with a different structure
  87. * use <code>HEADER_ROW</code> as the value for the <I>row</I>.
  88. * When the JTable recieves this event and its <I>autoCreateColumnsFromModel</I>
  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 JTable.
  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. }