KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > GridSwing


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * 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 are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb.util;
68
69 import java.util.Vector JavaDoc;
70 import java.awt.Component JavaDoc;
71
72 import javax.swing.JTable JavaDoc;
73 import javax.swing.event.TableModelEvent JavaDoc;
74 import javax.swing.table.AbstractTableModel JavaDoc;
75 import javax.swing.table.TableCellRenderer JavaDoc;
76 import javax.swing.table.TableColumn JavaDoc;
77 import javax.swing.table.TableModel JavaDoc;
78
79 // sqlbob@users 20020401 - patch 1.7.0 by sqlbob (RMP) - enhancements
80
// deccles@users 20040412 - patch 933671 - various bug fixes
81

82 /** Simple table model to represent a grid of tuples.
83  *
84  * New class based on Hypersonic SQL original
85  *
86  * @author dmarshall@users
87  * @version 1.7.2
88  * @since 1.7.0
89  */

90 class GridSwing extends AbstractTableModel JavaDoc {
91
92     JTable JavaDoc jtable = null;
93     Object JavaDoc[] headers;
94     Vector JavaDoc rows;
95
96     /**
97      * Default constructor.
98      */

99     public GridSwing() {
100
101         super();
102
103         headers = new Object JavaDoc[0]; // initially empty
104
rows = new Vector JavaDoc(); // initially empty
105
}
106
107     /**
108      * Get the name for the specified column.
109      */

110     public String JavaDoc getColumnName(int i) {
111         return headers[i].toString();
112     }
113
114     public Class JavaDoc getColumnClass(int i) {
115
116         if (rows.size() > 0) {
117             Object JavaDoc o = getValueAt(0, i);
118
119             if (o != null) {
120                 return o.getClass();
121             }
122         }
123
124         return super.getColumnClass(i);
125     }
126
127     /**
128      * Get the number of columns.
129      */

130     public int getColumnCount() {
131         return headers.length;
132     }
133
134     /**
135      * Get the number of rows currently in the table.
136      */

137     public int getRowCount() {
138         return rows.size();
139     }
140
141     /**
142      * Get the current column headings.
143      */

144     public Object JavaDoc[] getHead() {
145         return headers;
146     }
147
148     /**
149      * Get the current table data.
150      * Each row is represented as a <code>String[]</code>
151      * with a single non-null value in the 0-relative
152      * column position.
153      * <p>The first row is at offset 0, the nth row at offset n etc.
154      */

155     public Vector JavaDoc getData() {
156         return rows;
157     }
158
159     /**
160      * Get the object at the specified cell location.
161      */

162     public Object JavaDoc getValueAt(int row, int col) {
163
164         if (row >= rows.size()) {
165             return null;
166         }
167
168         Object JavaDoc[] colArray = (Object JavaDoc[]) rows.elementAt(row);
169
170         if (col >= colArray.length) {
171             return null;
172         }
173
174         return colArray[col];
175     }
176
177     /**
178      * Set the name of the column headings.
179      */

180     public void setHead(Object JavaDoc[] h) {
181
182         headers = new Object JavaDoc[h.length];
183
184         // System.arraycopy(h, 0, headers, 0, h.length);
185
for (int i = 0; i < h.length; i++) {
186             headers[i] = h[i];
187         }
188     }
189
190     /**
191      * Append a tuple to the end of the table.
192      */

193     public void addRow(Object JavaDoc[] r) {
194
195         Object JavaDoc[] row = new Object JavaDoc[r.length];
196
197         // System.arraycopy(r, 0, row, 0, r.length);
198
for (int i = 0; i < r.length; i++) {
199             row[i] = r[i];
200
201             if (row[i] == null) {
202
203 // row[i] = "(null)";
204
}
205         }
206
207         rows.addElement(row);
208     }
209
210     /**
211      * Remove data from all cells in the table (without
212      * affecting the current headings).
213      */

214     public void clear() {
215         rows.removeAllElements();
216     }
217
218     public void setJTable(JTable JavaDoc table) {
219         jtable = table;
220     }
221
222     public void fireTableChanged(TableModelEvent JavaDoc e) {
223         super.fireTableChanged(e);
224         autoSizeTableColumns(jtable);
225     }
226
227     public static void autoSizeTableColumns(JTable JavaDoc table) {
228
229         TableModel JavaDoc model = table.getModel();
230         TableColumn JavaDoc column = null;
231         Component JavaDoc comp = null;
232         int headerWidth = 0;
233         int maxCellWidth = Integer.MIN_VALUE;
234         int cellWidth = 0;
235         TableCellRenderer JavaDoc headerRenderer =
236             table.getTableHeader().getDefaultRenderer();
237
238         for (int i = 0; i < table.getColumnCount(); i++) {
239             column = table.getColumnModel().getColumn(i);
240             comp = headerRenderer.getTableCellRendererComponent(table,
241                     column.getHeaderValue(), false, false, 0, 0);
242             headerWidth = comp.getPreferredSize().width + 10;
243             maxCellWidth = Integer.MIN_VALUE;
244
245             for (int j = 0; j < Math.min(model.getRowCount(), 30); j++) {
246                 TableCellRenderer JavaDoc r = table.getCellRenderer(j, i);
247
248                 comp = r.getTableCellRendererComponent(table,
249                                                        model.getValueAt(j, i),
250                                                        false, false, j, i);
251                 cellWidth = comp.getPreferredSize().width;
252
253                 if (cellWidth >= maxCellWidth) {
254                     maxCellWidth = cellWidth;
255                 }
256             }
257
258             column.setPreferredWidth(Math.max(headerWidth, maxCellWidth)
259                                      + 10);
260         }
261     }
262 }
263
Popular Tags