home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / AbstractTableModel.java < prev    next >
Text File  |  1998-02-26  |  7KB  |  222 lines

  1. /*
  2.  * @(#)AbstractTableModel.java    1.18 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.table;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.event.*;
  25. import java.io.Serializable;
  26.  
  27.  
  28. /**
  29.  *  This abstract class provides default implementations for most of 
  30.  *  the methods in the <B>TableModel</B> interface. It takes care of 
  31.  *  the management of listners and provides some conveniences for generating 
  32.  *  TableModelEvents and dispatching them to the listeners. 
  33.  *  To create a concrete TableModel as a sublcass of 
  34.  *  AbstractTableModel you need only provide implementations for the 
  35.  *  following three methods: 
  36.  *
  37.  *  <pre>
  38.  *  public int getRowCount();
  39.  *  public int getColumnCount();
  40.  *  public Object getValueAt(int row, int column);
  41.  *  </pre>
  42.  * <p>
  43.  * Warning: serialized objects of this class will not be compatible with
  44.  * future swing releases.  The current serialization support is appropriate 
  45.  * for short term storage or RMI between Swing1.0 applications.  It will
  46.  * not be possible to load serialized Swing1.0 objects with future releases
  47.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  48.  * baseline for the serialized form of Swing objects.
  49.  *
  50.  * @version 1.18 02/02/98
  51.  * @author Alan Chung
  52.  * @author Philip Milne
  53.  */
  54. public abstract class AbstractTableModel implements TableModel, Serializable
  55. {
  56. //
  57. // Instance Variables
  58. //
  59.  
  60.     /** List of listeners */
  61.     protected EventListenerList listenerList = new EventListenerList();
  62.  
  63. //
  64. // Default Implementation of the Interface
  65. //
  66.  
  67.     /**
  68.      *  Return a default name for the column using spreadsheet conventions:
  69.      *  A, B, C, ... Z, AA, AB, etc.
  70.      */
  71.     public String getColumnName(int column) {
  72.     String result = "";
  73.     for (; column >= 0; column = column / 26 - 1) {
  74.         result = (char)((char)(column%26)+'A') + result;
  75.     }
  76.         return result;
  77.     }
  78.  
  79.     /**
  80.      * Convenience method for locating columns by name.
  81.      * Implementation is naive so this should be overridden if
  82.      * this method is to be called often. This method is not 
  83.      * in the TableModel interface and is not used by the JTable. 
  84.      */
  85.     public int findColumn(String columnName) {
  86.         for (int i = 0; i < getColumnCount(); i++) {
  87.             if (columnName.equals(getColumnName(i))) {
  88.                 return i;
  89.             }
  90.         }
  91.         return -1;
  92.     }
  93.  
  94.     /**
  95.      *  Returns Object.class by default
  96.      */
  97.     public Class getColumnClass(int columnIndex) {
  98.     return Object.class;
  99.     }
  100.  
  101.     /**
  102.      *  This default implementation returns false for all cells
  103.      */
  104.     public boolean isCellEditable(int rowIndex, int columnIndex) {
  105.     return false;
  106.     }
  107.  
  108.     /**
  109.      *  This empty implementation is provided so users don't have to implement
  110.      *  this method if their data model is not editable.
  111.      */
  112.     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
  113.     }
  114.  
  115.  
  116. //
  117. //  Managing Listeners
  118. //
  119.  
  120.     public void addTableModelListener(TableModelListener l) {
  121.     listenerList.add(TableModelListener.class, l);
  122.     }
  123.  
  124.     public void removeTableModelListener(TableModelListener l) {
  125.     listenerList.remove(TableModelListener.class, l);
  126.     }
  127.  
  128. //
  129. //  Fire methods
  130. //
  131.  
  132.     /**
  133.      * Notify all listeners that all cell values in the table's rows may have changed. 
  134.      * The number of rows may also have changed and the JTable should redraw the 
  135.      * table from scratch. The structure of the table, ie. the order of the 
  136.      * columns is assumed to be the same. 
  137.      * @see TableModelEvent
  138.      * @see EventListenerList
  139.      */
  140.     protected void fireTableDataChanged() {
  141.         fireTableChanged(new TableModelEvent(this)); 
  142.     }
  143.  
  144.     /**
  145.      * Notify all listeners that the table's structure has changed. 
  146.      * The number of columns in the table, and the names and types of 
  147.      * the new columns may be different from the previous state. 
  148.      * If the JTable recieves this event and its <I>autoCreateColumnsFromModel</I> 
  149.      * flag is set it discards any TableColumns that it had and reallocates 
  150.      * default ones in the order they appear in the model. This is the 
  151.      * same as calling <code>setModel(TableModel)</code> on the JTable. 
  152.      * @see TableModelEvent
  153.      * @see EventListenerList
  154.      */
  155.     protected void fireTableStructureChanged() {
  156.         fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW)); 
  157.     }
  158.  
  159.     /**
  160.      * Notify all listeners that rows in the (inclusive) range 
  161.      * [<I>firstRow</I>, <I>lastRow</I>] have been inserted.  
  162.      * @see TableModelEvent
  163.      * @see EventListenerList
  164.      */
  165.     protected void fireTableRowsInserted(int firstRow, int lastRow) {
  166.         fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 
  167.                              TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT)); 
  168.     }
  169.  
  170.     /**
  171.      * Notify all listeners that rows in the (inclusive) range 
  172.      * [<I>firstRow</I>, <I>lastRow</I>] have been updated.  
  173.      * @see TableModelEvent
  174.      * @see EventListenerList
  175.      */
  176.     protected void fireTableRowsUpdated(int firstRow, int lastRow) {
  177.         fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 
  178.                              TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE)); 
  179.     }
  180.  
  181.     /**
  182.      * Notify all listeners that rows in the (inclusive) range 
  183.      * [<I>firstRow</I>, <I>lastRow</I>] have been deleted.  
  184.      * @see TableModelEvent
  185.      * @see EventListenerList
  186.      */
  187.     protected void fireTableRowsDeleted(int firstRow, int lastRow) {
  188.         fireTableChanged(new TableModelEvent(this, firstRow, lastRow, 
  189.                              TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE)); 
  190.     }
  191.  
  192.     /**
  193.      * Notify all listeners that the value of the cell at (row, column) 
  194.      * has been updated.  
  195.      * @see TableModelEvent
  196.      * @see EventListenerList
  197.      */
  198.     protected void fireTableCellUpdated(int row, int column) {
  199.         fireTableChanged(new TableModelEvent(this, row, row, column)); 
  200.     }
  201.  
  202.     /**
  203.      * Forward the given notification event to all TableModelListeners that registered 
  204.      * themselves as listeners for this table model. 
  205.      * @see #addTableModelListener
  206.      * @see TableModelEvent
  207.      * @see EventListenerList
  208.      */
  209.     protected void fireTableChanged(TableModelEvent e) {
  210.     // Guaranteed to return a non-null array
  211.     Object[] listeners = listenerList.getListenerList();
  212.     // Process the listeners last to first, notifying
  213.     // those that are interested in this event
  214.     for (int i = listeners.length-2; i>=0; i-=2) {
  215.         if (listeners[i]==TableModelListener.class) {
  216.         ((TableModelListener)listeners[i+1]).tableChanged(e);
  217.         }
  218.     }
  219.     }
  220.  
  221. } // End of class AbstractTableModel
  222.