001 /*
002 * Copyright 1997-2001 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.event;
027
028 import java.util.EventObject;
029 import javax.swing.table.*;
030
031 /**
032 * TableModelEvent is used to notify listeners that a table model
033 * has changed. The model event describes changes to a TableModel
034 * and all references to rows and columns are in the co-ordinate
035 * system of the model.
036 * Depending on the parameters used in the constructors, the TableModelevent
037 * can be used to specify the following types of changes: <p>
038 *
039 * <pre>
040 * TableModelEvent(source); // The data, ie. all rows changed
041 * TableModelEvent(source, HEADER_ROW); // Structure change, reallocate TableColumns
042 * TableModelEvent(source, 1); // Row 1 changed
043 * TableModelEvent(source, 3, 6); // Rows 3 to 6 inclusive changed
044 * TableModelEvent(source, 2, 2, 6); // Cell at (2, 6) changed
045 * TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
046 * TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted
047 * </pre>
048 *
049 * It is possible to use other combinations of the parameters, not all of them
050 * are meaningful. By subclassing, you can add other information, for example:
051 * whether the event WILL happen or DID happen. This makes the specification
052 * of rows in DELETE events more useful but has not been included in
053 * the swing package as the JTable only needs post-event notification.
054 * <p>
055 * <strong>Warning:</strong>
056 * Serialized objects of this class will not be compatible with
057 * future Swing releases. The current serialization support is
058 * appropriate for short term storage or RMI between applications running
059 * the same version of Swing. As of 1.4, support for long term storage
060 * of all JavaBeans<sup><font size="-2">TM</font></sup>
061 * has been added to the <code>java.beans</code> package.
062 * Please see {@link java.beans.XMLEncoder}.
063 *
064 * @version 1.28 05/05/07
065 * @author Alan Chung
066 * @author Philip Milne
067 * @see TableModel
068 */
069 public class TableModelEvent extends java.util.EventObject {
070 /** Identifies the addtion of new rows or columns. */
071 public static final int INSERT = 1;
072 /** Identifies a change to existing data. */
073 public static final int UPDATE = 0;
074 /** Identifies the removal of rows or columns. */
075 public static final int DELETE = -1;
076
077 /** Identifies the header row. */
078 public static final int HEADER_ROW = -1;
079
080 /** Specifies all columns in a row or rows. */
081 public static final int ALL_COLUMNS = -1;
082
083 //
084 // Instance Variables
085 //
086
087 protected int type;
088 protected int firstRow;
089 protected int lastRow;
090 protected int column;
091
092 //
093 // Constructors
094 //
095
096 /**
097 * All row data in the table has changed, listeners should discard any state
098 * that was based on the rows and requery the <code>TableModel</code>
099 * to get the new row count and all the appropriate values.
100 * The <code>JTable</code> will repaint the entire visible region on
101 * receiving this event, querying the model for the cell values that are visible.
102 * The structure of the table ie, the column names, types and order
103 * have not changed.
104 */
105 public TableModelEvent(TableModel source) {
106 // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
107 this (source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
108 }
109
110 /**
111 * This row of data has been updated.
112 * To denote the arrival of a completely new table with a different structure
113 * use <code>HEADER_ROW</code> as the value for the <code>row</code>.
114 * When the <code>JTable</code> receives this event and its
115 * <code>autoCreateColumnsFromModel</code>
116 * flag is set it discards any TableColumns that it had and reallocates
117 * default ones in the order they appear in the model. This is the
118 * same as calling <code>setModel(TableModel)</code> on the <code>JTable</code>.
119 */
120 public TableModelEvent(TableModel source, int row) {
121 this (source, row, row, ALL_COLUMNS, UPDATE);
122 }
123
124 /**
125 * The data in rows [<I>firstRow</I>, <I>lastRow</I>] have been updated.
126 */
127 public TableModelEvent(TableModel source, int firstRow, int lastRow) {
128 this (source, firstRow, lastRow, ALL_COLUMNS, UPDATE);
129 }
130
131 /**
132 * The cells in column <I>column</I> in the range
133 * [<I>firstRow</I>, <I>lastRow</I>] have been updated.
134 */
135 public TableModelEvent(TableModel source, int firstRow,
136 int lastRow, int column) {
137 this (source, firstRow, lastRow, column, UPDATE);
138 }
139
140 /**
141 * The cells from (firstRow, column) to (lastRow, column) have been changed.
142 * The <I>column</I> refers to the column index of the cell in the model's
143 * co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
144 * specified range of rows are considered changed.
145 * <p>
146 * The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
147 */
148 public TableModelEvent(TableModel source, int firstRow,
149 int lastRow, int column, int type) {
150 super (source);
151 this .firstRow = firstRow;
152 this .lastRow = lastRow;
153 this .column = column;
154 this .type = type;
155 }
156
157 //
158 // Querying Methods
159 //
160
161 /** Returns the first row that changed. HEADER_ROW means the meta data,
162 * ie. names, types and order of the columns.
163 */
164 public int getFirstRow() {
165 return firstRow;
166 };
167
168 /** Returns the last row that changed. */
169 public int getLastRow() {
170 return lastRow;
171 };
172
173 /**
174 * Returns the column for the event. If the return
175 * value is ALL_COLUMNS; it means every column in the specified
176 * rows changed.
177 */
178 public int getColumn() {
179 return column;
180 };
181
182 /**
183 * Returns the type of event - one of: INSERT, UPDATE and DELETE.
184 */
185 public int getType() {
186 return type;
187 }
188 }
|