KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > OldJTable


1 /*
2  * @(#)OldJTable.java 1.11 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)OldJTable.java 1.11 05/11/17
39  */

40
41 import java.lang.Thread JavaDoc;
42 import java.util.*;
43 import java.awt.*;
44 import java.awt.event.*;
45 import javax.swing.*;
46 import javax.swing.event.*;
47 import javax.swing.plaf.*;
48 import javax.swing.table.*;
49
50
51 /**
52  * The OldJTable is an unsupported class containing some methods that were
53  * deleted from the JTable between releases 0.6 and 0.7
54  */

55 public class OldJTable extends JTable
56 {
57    /*
58     * A new convenience method returning the index of the column in the co-ordinate
59     * space of the view.
60     */

61     public int getColumnIndex(Object JavaDoc identifier) {
62         return getColumnModel().getColumnIndex(identifier);
63     }
64     
65 //
66
// Methods deleted from the JTable because they only work with the
67
// DefaultTableModel.
68
//
69

70     public TableColumn addColumn(Object JavaDoc columnIdentifier, int width) {
71     return addColumn(columnIdentifier, width, null, null, null);
72     }
73
74     public TableColumn addColumn(Object JavaDoc columnIdentifier, Vector columnData) {
75     return addColumn(columnIdentifier, -1, null, null, columnData);
76     }
77
78     // Override the new JTable implementation - it will not add a column to the
79
// DefaultTableModel.
80
public TableColumn addColumn(Object JavaDoc columnIdentifier, int width,
81                  TableCellRenderer renderer,
82                  TableCellEditor editor) {
83         return addColumn(columnIdentifier, width, renderer, editor, null);
84     }
85
86     public TableColumn addColumn(Object JavaDoc columnIdentifier, int width,
87                  TableCellRenderer renderer,
88                  TableCellEditor editor, Vector columnData) {
89     checkDefaultTableModel();
90
91     // Set up the model side first
92
DefaultTableModel m = (DefaultTableModel)getModel();
93     m.addColumn(columnIdentifier, columnData);
94     
95     // The column will have been added to the end, so the index of the
96
// column in the model is the last element.
97
TableColumn newColumn = new TableColumn(m.getColumnCount()-1, width, renderer, editor);
98         super.addColumn(newColumn);
99         return newColumn;
100     }
101
102     // Not possilble to make this work the same way ... change it so that
103
// it does not delete columns from the model.
104
public void removeColumn(Object JavaDoc columnIdentifier) {
105     super.removeColumn(getColumn(columnIdentifier));
106     }
107
108     public void addRow(Object JavaDoc[] rowData) {
109     checkDefaultTableModel();
110     ((DefaultTableModel)getModel()).addRow(rowData);
111     }
112
113     public void addRow(Vector rowData) {
114     checkDefaultTableModel();
115     ((DefaultTableModel)getModel()).addRow(rowData);
116     }
117     
118     public void removeRow(int rowIndex) {
119     checkDefaultTableModel();
120     ((DefaultTableModel)getModel()).removeRow(rowIndex);
121     }
122
123     public void moveRow(int startIndex, int endIndex, int toIndex) {
124     checkDefaultTableModel();
125     ((DefaultTableModel)getModel()).moveRow(startIndex, endIndex, toIndex);
126     }
127
128     public void insertRow(int rowIndex, Object JavaDoc[] rowData) {
129     checkDefaultTableModel();
130     ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);
131     }
132
133     public void insertRow(int rowIndex, Vector rowData) {
134     checkDefaultTableModel();
135     ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);
136     }
137
138     public void setNumRows(int newSize) {
139     checkDefaultTableModel();
140     ((DefaultTableModel)getModel()).setNumRows(newSize);
141     }
142
143     public void setDataVector(Vector newData, Vector columnIds) {
144     checkDefaultTableModel();
145     ((DefaultTableModel)getModel()).setDataVector(newData, columnIds);
146     }
147
148     public void setDataVector(Object JavaDoc[][] newData, Object JavaDoc[] columnIds) {
149     checkDefaultTableModel();
150     ((DefaultTableModel)getModel()).setDataVector(newData, columnIds);
151     }
152         
153     protected void checkDefaultTableModel() {
154         if(!(dataModel instanceof DefaultTableModel))
155             throw new InternalError JavaDoc("In order to use this method, the data model must be an instance of DefaultTableModel.");
156     }
157
158 //
159
// Methods removed from JTable in the move from identifiers to ints.
160
//
161

162     public Object JavaDoc getValueAt(Object JavaDoc columnIdentifier, int rowIndex) {
163     return super.getValueAt(rowIndex, getColumnIndex(columnIdentifier));
164     }
165     
166     public boolean isCellEditable(Object JavaDoc columnIdentifier, int rowIndex) {
167     return super.isCellEditable(rowIndex, getColumnIndex(columnIdentifier));
168     }
169     
170     public void setValueAt(Object JavaDoc aValue, Object JavaDoc columnIdentifier, int rowIndex) {
171     super.setValueAt(aValue, rowIndex, getColumnIndex(columnIdentifier));
172     }
173
174     public boolean editColumnRow(Object JavaDoc identifier, int row) {
175     return super.editCellAt(row, getColumnIndex(identifier));
176     }
177
178     public void moveColumn(Object JavaDoc columnIdentifier, Object JavaDoc targetColumnIdentifier) {
179     moveColumn(getColumnIndex(columnIdentifier),
180            getColumnIndex(targetColumnIdentifier));
181     }
182
183     public boolean isColumnSelected(Object JavaDoc identifier) {
184     return isColumnSelected(getColumnIndex(identifier));
185     }
186
187     public TableColumn addColumn(int modelColumn, int width) {
188     return addColumn(modelColumn, width, null, null);
189     }
190
191     public TableColumn addColumn(int modelColumn) {
192     return addColumn(modelColumn, 75, null, null);
193     }
194     
195     /**
196      * Creates a new column with <I>modelColumn</I>, <I>width</I>,
197      * <I>renderer</I>, and <I>editor</I> and adds it to the end of
198      * the JTable's array of columns. This method also retrieves the
199      * name of the column using the model's <I>getColumnName(modelColumn)</I>
200      * method, and sets the both the header value and the identifier
201      * for this TableColumn accordingly.
202      * <p>
203      * The <I>modelColumn</I> is the index of the column in the model which
204      * will supply the data for this column in the table. This, like the
205      * <I>columnIdentifier</I> in previous releases, does not change as the
206      * columns are moved in the view.
207      * <p>
208      * For the rest of the JTable API, and all of its associated classes,
209      * columns are referred to in the co-ordinate system of the view, the
210      * index of the column in the model is kept inside the TableColumn
211      * and is used only to retrieve the information from the appropraite
212      * column in the model.
213      * <p>
214      *
215      * @param modelColumn The index of the column in the model
216      * @param width The new column's width. Or -1 to use
217      * the default width
218      * @param renderer The renderer used with the new column.
219      * Or null to use the default renderer.
220      * @param editor The editor used with the new column.
221      * Or null to use the default editor.
222      */

223     public TableColumn addColumn(int modelColumn, int width,
224                  TableCellRenderer renderer,
225                  TableCellEditor editor) {
226     TableColumn newColumn = new TableColumn(modelColumn, width, renderer, editor);
227     addColumn(newColumn);
228     return newColumn;
229     }
230
231 //
232
// Methods that had their arguments switched.
233
//
234

235 // These won't work with the new table package.
236

237 /*
238     public Object getValueAt(int columnIndex, int rowIndex) {
239     return super.getValueAt(rowIndex, columnIndex);
240     }
241
242     public boolean isCellEditable(int columnIndex, int rowIndex) {
243     return super.isCellEditable(rowIndex, columnIndex);
244     }
245     
246     public void setValueAt(Object aValue, int columnIndex, int rowIndex) {
247         super.setValueAt(aValue, rowIndex, columnIndex);
248     }
249 */

250
251     public boolean editColumnRow(int columnIndex, int rowIndex) {
252     return super.editCellAt(rowIndex, columnIndex);
253     }
254
255     public boolean editColumnRow(int columnIndex, int rowIndex, EventObject e){
256         return super.editCellAt(rowIndex, columnIndex, e);
257     }
258
259
260 } // End Of Class OldJTable
261
Popular Tags