home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / TableExample / src / OldJTable.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  9.1 KB  |  260 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)OldJTable.java    1.7 02/06/13
  38.  */
  39.  
  40. import java.lang.Thread;
  41. import java.util.*;
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import javax.swing.*;
  45. import javax.swing.event.*;
  46. import javax.swing.plaf.*;
  47. import javax.swing.table.*;
  48.  
  49.  
  50. /**
  51.  *  The OldJTable is an unsupported class containing some methods that were 
  52.  *  deleted from the JTable between releases 0.6 and 0.7
  53.  */
  54. public class OldJTable extends JTable
  55. {
  56.    /*
  57.     *  A new convenience method returning the index of the column in the co-ordinate 
  58.     *  space of the view. 
  59.     */
  60.     public int getColumnIndex(Object identifier) { 
  61.         return getColumnModel().getColumnIndex(identifier); 
  62.     }
  63.     
  64. // 
  65. //  Methods deleted from the JTable because they only work with the 
  66. //  DefaultTableModel. 
  67. //
  68.  
  69.     public TableColumn addColumn(Object columnIdentifier, int width) {
  70.     return addColumn(columnIdentifier, width, null, null, null);
  71.     }
  72.  
  73.     public TableColumn addColumn(Object columnIdentifier, Vector columnData) {
  74.     return addColumn(columnIdentifier, -1, null, null, columnData);
  75.     }
  76.  
  77.     // Override the new JTable implementation - it will not add a column to the 
  78.     // DefaultTableModel. 
  79.     public TableColumn addColumn(Object columnIdentifier, int width,
  80.                  TableCellRenderer renderer,
  81.                  TableCellEditor editor) {
  82.         return addColumn(columnIdentifier, width, renderer, editor, null);
  83.     }
  84.  
  85.     public TableColumn addColumn(Object columnIdentifier, int width,
  86.                  TableCellRenderer renderer,
  87.                  TableCellEditor editor, Vector columnData) {
  88.     checkDefaultTableModel();
  89.  
  90.     // Set up the model side first
  91.     DefaultTableModel m = (DefaultTableModel)getModel(); 
  92.     m.addColumn(columnIdentifier, columnData);
  93.     
  94.     // The column will have been added to the end, so the index of the 
  95.     // column in the model is the last element. 
  96.     TableColumn newColumn = new TableColumn(m.getColumnCount()-1, width, renderer, editor); 
  97.         super.addColumn(newColumn); 
  98.         return newColumn; 
  99.     }
  100.  
  101.     // Not possilble to make this work the same way ... change it so that 
  102.     // it does not delete columns from the model. 
  103.     public void removeColumn(Object columnIdentifier) {
  104.     super.removeColumn(getColumn(columnIdentifier));
  105.     }
  106.  
  107.     public void addRow(Object[] rowData) {
  108.     checkDefaultTableModel();
  109.     ((DefaultTableModel)getModel()).addRow(rowData);
  110.     }
  111.  
  112.     public void addRow(Vector rowData) {
  113.     checkDefaultTableModel();
  114.     ((DefaultTableModel)getModel()).addRow(rowData);
  115.     }
  116.     
  117.     public void removeRow(int rowIndex) {
  118.     checkDefaultTableModel();
  119.     ((DefaultTableModel)getModel()).removeRow(rowIndex);
  120.     }
  121.  
  122.     public void moveRow(int startIndex, int endIndex, int toIndex) {
  123.     checkDefaultTableModel();
  124.     ((DefaultTableModel)getModel()).moveRow(startIndex, endIndex, toIndex);
  125.     }
  126.  
  127.     public void insertRow(int rowIndex, Object[] rowData) {
  128.     checkDefaultTableModel();
  129.     ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);
  130.     }
  131.  
  132.     public void insertRow(int rowIndex, Vector rowData) {
  133.     checkDefaultTableModel();
  134.     ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);
  135.     }
  136.  
  137.     public void setNumRows(int newSize) {
  138.     checkDefaultTableModel();
  139.     ((DefaultTableModel)getModel()).setNumRows(newSize);
  140.     }
  141.  
  142.     public void setDataVector(Vector newData, Vector columnIds) {
  143.     checkDefaultTableModel();
  144.     ((DefaultTableModel)getModel()).setDataVector(newData, columnIds);
  145.     }
  146.  
  147.     public void setDataVector(Object[][] newData, Object[] columnIds) {
  148.     checkDefaultTableModel();
  149.     ((DefaultTableModel)getModel()).setDataVector(newData, columnIds);
  150.     }
  151.         
  152.     protected void checkDefaultTableModel() {
  153.         if(!(dataModel instanceof DefaultTableModel))
  154.             throw new InternalError("In order to use this method, the data model must be an instance of DefaultTableModel.");
  155.     }
  156.  
  157. //
  158. //  Methods removed from JTable in the move from identifiers to ints. 
  159. //
  160.  
  161.     public Object getValueAt(Object columnIdentifier, int rowIndex) {
  162.     return  super.getValueAt(rowIndex, getColumnIndex(columnIdentifier));
  163.     }
  164.     
  165.     public boolean isCellEditable(Object columnIdentifier, int rowIndex) {
  166.     return  super.isCellEditable(rowIndex, getColumnIndex(columnIdentifier));
  167.     }
  168.     
  169.     public void setValueAt(Object aValue, Object columnIdentifier, int rowIndex) {
  170.     super.setValueAt(aValue, rowIndex, getColumnIndex(columnIdentifier));
  171.     }
  172.  
  173.     public boolean editColumnRow(Object identifier, int row) {
  174.     return super.editCellAt(row, getColumnIndex(identifier));
  175.     }
  176.  
  177.     public void moveColumn(Object columnIdentifier, Object targetColumnIdentifier) {
  178.     moveColumn(getColumnIndex(columnIdentifier),
  179.            getColumnIndex(targetColumnIdentifier));
  180.     }
  181.  
  182.     public boolean isColumnSelected(Object identifier) { 
  183.     return isColumnSelected(getColumnIndex(identifier));
  184.     }
  185.  
  186.     public TableColumn addColumn(int modelColumn, int width) {
  187.     return addColumn(modelColumn, width, null, null);
  188.     }
  189.  
  190.     public TableColumn addColumn(int modelColumn) {
  191.     return addColumn(modelColumn, 75, null, null);
  192.     }
  193.     
  194.     /**
  195.      *  Creates a new column with <I>modelColumn</I>, <I>width</I>,
  196.      *  <I>renderer</I>, and <I>editor</I> and adds it to the end of
  197.      *  the JTable's array of columns. This method also retrieves the
  198.      *  name of the column using the model's <I>getColumnName(modelColumn)</I>
  199.      *  method, and sets the both the header value and the identifier
  200.      *  for this TableColumn accordingly.
  201.      *  <p>
  202.      *  The <I>modelColumn</I> is the index of the column in the model which
  203.      *  will supply the data for this column in the table. This, like the
  204.      *  <I>columnIdentifier</I> in previous releases, does not change as the
  205.      *  columns are moved in the view.
  206.      *  <p>
  207.      *  For the rest of the JTable API, and all of its associated classes,
  208.      *  columns are referred to in the co-ordinate system of the view, the
  209.      *  index of the column in the model is kept inside the TableColumn
  210.      *  and is used only to retrieve the information from the appropraite
  211.      *  column in the model.
  212.      *  <p>
  213.      *
  214.      *  @param    modelColumn     The index of the column in the model
  215.      *  @param    width        The new column's width.  Or -1 to use
  216.      *                the default width
  217.      *  @param    renderer    The renderer used with the new column.
  218.      *                Or null to use the default renderer.
  219.      *  @param    editor        The editor used with the new column.
  220.      *                Or null to use the default editor.
  221.      */
  222.     public TableColumn addColumn(int modelColumn, int width,
  223.                  TableCellRenderer renderer,
  224.                  TableCellEditor editor) {
  225.     TableColumn newColumn = new TableColumn(modelColumn, width, renderer, editor);        
  226.     addColumn(newColumn); 
  227.     return newColumn; 
  228.     }
  229.  
  230. //
  231. //  Methods that had their arguments switched. 
  232. //
  233.  
  234. // These won't work with the new table package. 
  235.  
  236. /*  
  237.     public Object getValueAt(int columnIndex, int rowIndex) {
  238.     return super.getValueAt(rowIndex, columnIndex);
  239.     }
  240.  
  241.     public boolean isCellEditable(int columnIndex, int rowIndex) {
  242.     return super.isCellEditable(rowIndex, columnIndex);
  243.     }
  244.     
  245.     public void setValueAt(Object aValue, int columnIndex, int rowIndex) {
  246.         super.setValueAt(aValue, rowIndex, columnIndex); 
  247.     }
  248. */
  249.  
  250.     public boolean editColumnRow(int columnIndex, int rowIndex) {
  251.     return super.editCellAt(rowIndex, columnIndex);
  252.     }
  253.  
  254.     public boolean editColumnRow(int columnIndex, int rowIndex, EventObject e){
  255.         return super.editCellAt(rowIndex, columnIndex, e); 
  256.     }
  257.  
  258.  
  259. }  // End Of Class OldJTable
  260.