1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.event;
  27 
  28 import java.util.EventObject;
  29 import javax.swing.table.*;
  30 
  31 /**
  32  * TableModelEvent is used to notify listeners that a table model
  33  * has changed. The model event describes changes to a TableModel
  34  * and all references to rows and columns are in the co-ordinate
  35  * system of the model.
  36  * Depending on the parameters used in the constructors, the TableModelevent
  37  * can be used to specify the following types of changes:
  38  *
  39  * <pre>
  40  * TableModelEvent(source);              //  The data, ie. all rows changed
  41  * TableModelEvent(source, HEADER_ROW);  //  Structure change, reallocate TableColumns
  42  * TableModelEvent(source, 1);           //  Row 1 changed
  43  * TableModelEvent(source, 3, 6);        //  Rows 3 to 6 inclusive changed
  44  * TableModelEvent(source, 2, 2, 6);     //  Cell at (2, 6) changed
  45  * TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
  46  * TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted
  47  * </pre>
  48  *
  49  * It is possible to use other combinations of the parameters, not all of them
  50  * are meaningful. By subclassing, you can add other information, for example:
  51  * whether the event WILL happen or DID happen. This makes the specification
  52  * of rows in DELETE events more useful but has not been included in
  53  * the swing package as the JTable only needs post-event notification.
  54  * <p>
  55  * <strong>Warning:</strong>
  56  * Serialized objects of this class will not be compatible with
  57  * future Swing releases. The current serialization support is
  58  * appropriate for short term storage or RMI between applications running
  59  * the same version of Swing.  As of 1.4, support for long term storage
  60  * of all JavaBeans&trade;
  61  * has been added to the <code>java.beans</code> package.
  62  * Please see {@link java.beans.XMLEncoder}.
  63  *
  64  * @author Alan Chung
  65  * @author Philip Milne
  66  * @see TableModel
  67  */
  68 @SuppressWarnings("serial") // Same-version serialization only
  69 public class TableModelEvent extends java.util.EventObject
  70 {
  71     /** Identifies the addition of new rows or columns. */
  72     public static final int INSERT =  1;
  73     /** Identifies a change to existing data. */
  74     public static final int UPDATE =  0;
  75     /** Identifies the removal of rows or columns. */
  76     public static final int DELETE = -1;
  77 
  78     /** Identifies the header row. */
  79     public static final int HEADER_ROW = -1;
  80 
  81     /** Specifies all columns in a row or rows. */
  82     public static final int ALL_COLUMNS = -1;
  83 
  84 //
  85 //  Instance Variables
  86 //
  87 
  88     /**
  89      * The type of the event.
  90      */
  91     protected int       type;
  92     /**
  93      * The first row that has changed.
  94      */
  95     protected int       firstRow;
  96     /**
  97      * The last row that has changed.
  98      */
  99     protected int       lastRow;
 100     /**
 101      * The column for the event.
 102      */
 103     protected int       column;
 104 
 105 //
 106 // Constructors
 107 //
 108 
 109     /**
 110      * All row data in the table has changed, listeners should discard any state
 111      * that was based on the rows and requery the <code>TableModel</code>
 112      * to get the new row count and all the appropriate values.
 113      * The <code>JTable</code> will repaint the entire visible region on
 114      * receiving this event, querying the model for the cell values that are visible.
 115      * The structure of the table ie, the column names, types and order
 116      * have not changed.
 117      *
 118      * @param source the {@code TableModel} affected by this event
 119      */
 120     public TableModelEvent(TableModel source) {
 121         // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
 122         this(source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
 123     }
 124 
 125     /**
 126      * This row of data has been updated.
 127      * To denote the arrival of a completely new table with a different structure
 128      * use <code>HEADER_ROW</code> as the value for the <code>row</code>.
 129      * When the <code>JTable</code> receives this event and its
 130      * <code>autoCreateColumnsFromModel</code>
 131      * flag is set it discards any TableColumns that it had and reallocates
 132      * default ones in the order they appear in the model. This is the
 133      * same as calling <code>setModel(TableModel)</code> on the <code>JTable</code>.
 134      *
 135      * @param source the {@code TableModel} affected by this event
 136      * @param row the row which has been updated
 137      */
 138     public TableModelEvent(TableModel source, int row) {
 139         this(source, row, row, ALL_COLUMNS, UPDATE);
 140     }
 141 
 142     /**
 143      * The data in rows [<I>firstRow</I>, <I>lastRow</I>] have been updated.
 144      *
 145      * @param source the {@code TableModel} affected by this event
 146      * @param firstRow the first row affected by this event
 147      * @param lastRow  the last row affected by this event
 148      */
 149     public TableModelEvent(TableModel source, int firstRow, int lastRow) {
 150         this(source, firstRow, lastRow, ALL_COLUMNS, UPDATE);
 151     }
 152 
 153     /**
 154      *  The cells in column <I>column</I> in the range
 155      *  [<I>firstRow</I>, <I>lastRow</I>] have been updated.
 156      *
 157      * @param source the {@code TableModel} affected by this event
 158      * @param firstRow the first row affected by this event
 159      * @param lastRow  the last row affected by this event
 160      * @param column the column index of cells changed; {@code ALL_COLUMNS}
 161      *        signifies all cells in the specified range of rows are changed.
 162      */
 163     public TableModelEvent(TableModel source, int firstRow, int lastRow, int column) {
 164         this(source, firstRow, lastRow, column, UPDATE);
 165     }
 166 
 167     /**
 168      * The cells from (firstRow, column) to (lastRow, column) have been changed.
 169      * The <I>column</I> refers to the column index of the cell in the model's
 170      * co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
 171      * specified range of rows are considered changed.
 172      * <p>
 173      * The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
 174      *
 175      * @param source the {@code TableModel} affected by this event
 176      * @param firstRow the first row affected by this event
 177      * @param lastRow  the last row affected by this event
 178      * @param column the column index of cells changed; {@code ALL_COLUMNS}
 179      *        signifies all cells in the specified range of rows are changed.
 180      * @param type the type of change signified by this even, {@code INSERT},
 181      *        {@code DELETE } or {@code UPDATE}
 182      */
 183     public TableModelEvent(TableModel source, int firstRow, int lastRow, int column, int type) {
 184         super(source);
 185         this.firstRow = firstRow;
 186         this.lastRow = lastRow;
 187         this.column = column;
 188         this.type = type;
 189     }
 190 
 191 //
 192 // Querying Methods
 193 //
 194 
 195     /**
 196      * Returns the first row that changed.  HEADER_ROW means the meta data,
 197      * ie. names, types and order of the columns.
 198      *
 199      * @return an integer signifying the first row changed
 200      */
 201     public int getFirstRow() { return firstRow; };
 202 
 203     /**
 204      * Returns the last row that changed.
 205      *
 206      * @return an integer signifying the last row changed
 207      */
 208     public int getLastRow() { return lastRow; };
 209 
 210     /**
 211      *  Returns the column for the event.  If the return
 212      *  value is ALL_COLUMNS; it means every column in the specified
 213      *  rows changed.
 214      *
 215      * @return an integer signifying which column is affected by this event
 216      */
 217     public int getColumn() { return column; };
 218 
 219     /**
 220      *  Returns the type of event - one of: INSERT, UPDATE and DELETE.
 221      *
 222      * @return the type of change to a table model, an {@code INSERT} or
 223      *         {@code DELETE } of row(s) or column(s) or {@code UPDATE}
 224      *         to data
 225      */
 226     public int getType() { return type; }
 227 }