1 /*
   2  * Copyright (c) 1997, 2001, 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 public class TableModelEvent extends java.util.EventObject
  69 {
  70     /** Identifies the addition of new rows or columns. */
  71     public static final int INSERT =  1;
  72     /** Identifies a change to existing data. */
  73     public static final int UPDATE =  0;
  74     /** Identifies the removal of rows or columns. */
  75     public static final int DELETE = -1;
  76 
  77     /** Identifies the header row. */
  78     public static final int HEADER_ROW = -1;
  79 
  80     /** Specifies all columns in a row or rows. */
  81     public static final int ALL_COLUMNS = -1;
  82 
  83 //
  84 //  Instance Variables
  85 //
  86 
  87     protected int       type;
  88     protected int       firstRow;
  89     protected int       lastRow;
  90     protected int       column;
  91 
  92 //
  93 // Constructors
  94 //
  95 
  96     /**
  97      *  All row data in the table has changed, listeners should discard any state
  98      *  that was based on the rows and requery the <code>TableModel</code>
  99      *  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, int lastRow, int column) {
 136         this(source, firstRow, lastRow, column, UPDATE);
 137     }
 138 
 139     /**
 140      *  The cells from (firstRow, column) to (lastRow, column) have been changed.
 141      *  The <I>column</I> refers to the column index of the cell in the model's
 142      *  co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
 143      *  specified range of rows are considered changed.
 144      *  <p>
 145      *  The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
 146      */
 147     public TableModelEvent(TableModel source, int firstRow, int lastRow, int column, int type) {
 148         super(source);
 149         this.firstRow = firstRow;
 150         this.lastRow = lastRow;
 151         this.column = column;
 152         this.type = type;
 153     }
 154 
 155 //
 156 // Querying Methods
 157 //
 158 
 159    /** Returns the first row that changed.  HEADER_ROW means the meta data,
 160      * ie. names, types and order of the columns.
 161      */
 162     public int getFirstRow() { return firstRow; };
 163 
 164     /** Returns the last row that changed. */
 165     public int getLastRow() { return lastRow; };
 166 
 167     /**
 168      *  Returns the column for the event.  If the return
 169      *  value is ALL_COLUMNS; it means every column in the specified
 170      *  rows changed.
 171      */
 172     public int getColumn() { return column; };
 173 
 174     /**
 175      *  Returns the type of event - one of: INSERT, UPDATE and DELETE.
 176      */
 177     public int getType() { return type; }
 178 }