KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > jfccontrols > ResultSetTableModel


1 /**
2  * com.mckoi.jfccontrols.ResultSetTableModel 02 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.jfccontrols;
26
27 import javax.swing.table.*;
28 import java.sql.*;
29
30 /**
31  * An implementation of a javax.swing.table.TableModel that updates itself from
32  * a scrollable java.sql.ResultSet source. This directly maps columns from a
33  * query to columns in the table model. If you wish to filter information
34  * from the result set before it is output as a table use
35  * FilteredResultSetTableModel.
36  *
37  * @author Tobias Downer
38  */

39
40 public class ResultSetTableModel extends AbstractTableModel {
41
42   /**
43    * The scrollable ResultSet source.
44    */

45   private ResultSet result_set;
46
47   /**
48    * The ResultSetMetaData object for this result set.
49    */

50   private ResultSetMetaData meta_data;
51
52   /**
53    * The number of rows in the result set.
54    */

55   private int row_count;
56
57   /**
58    * If true, a table structure change event is NOT thrown if the result set
59    * looks similar to an updated result set.
60    */

61   private boolean preserve_table_structure;
62   
63   
64   /**
65    * Constructs the model.
66    */

67   public ResultSetTableModel(ResultSet result_set) {
68     super();
69     preserve_table_structure = false;
70     if (result_set != null) {
71       updateResultSet(result_set);
72     }
73     else {
74       clear();
75     }
76   }
77
78   public ResultSetTableModel() {
79     this(null);
80   }
81
82   /**
83    * A property that checks for changes when a result set is updated and
84    * preserves the layout if the updated result set looks similar. This means
85    * that the width of columns in the UI will not change to their default
86    * values.
87    */

88   public void setPreserveTableStructure(boolean status) {
89     preserve_table_structure = status;
90   }
91
92   /**
93    * Updates the result set in this model with the given ResultSet object.
94    */

95   public void updateResultSet(ResultSet result_set) {
96     try {
97       boolean fire_structure_change = true;
98       if (this.result_set != null) {
99         // If the 'preserve_table_structure' flag is set, we want to check if
100
// there are any changes between the old meta data structure and the
101
// new one.
102
if (preserve_table_structure) {
103           // Did the result set change?
104
ResultSetMetaData old_meta_data = this.meta_data;
105           ResultSetMetaData new_meta_data = result_set.getMetaData();
106           int col_count = new_meta_data.getColumnCount();
107           if (old_meta_data.getColumnCount() == col_count) {
108             boolean different = false;
109             for (int i = 1; i < col_count + 1 && !different; ++i) {
110               different =
111                  (!old_meta_data.getColumnName(i).equals(
112                                          new_meta_data.getColumnName(i)) ||
113                   !old_meta_data.getTableName(i).equals(
114                                          new_meta_data.getTableName(i)) ||
115                   !old_meta_data.getSchemaName(i).equals(
116                                          new_meta_data.getSchemaName(i)));
117             }
118             fire_structure_change = different;
119           }
120         }
121         this.result_set.close();
122       }
123
124       // Set up the new result set info.
125
this.result_set = result_set;
126       this.meta_data = result_set.getMetaData();
127
128       // Move to the end of the result set and get the row index. This is a
129
// fast way to work out the row count, however it won't work efficiently
130
// in many database systems.
131
if (result_set.last()) {
132         row_count = result_set.getRow();
133       }
134       else {
135         row_count = 0;
136       }
137
138       if (fire_structure_change) {
139         fireTableStructureChanged();
140       }
141       else {
142         fireTableDataChanged();
143       }
144
145     }
146     catch (SQLException e) {
147       throw new Error JavaDoc("SQL Exception: " + e.getMessage());
148     }
149   }
150
151   /**
152    * Clears the model of the current result set.
153    */

154   public void clear() {
155     // Close the old result set if needed.
156
if (result_set != null) {
157       try {
158         result_set.close();
159       }
160       catch (SQLException e) {
161         // Just incase the JDBC driver can't close a result set twice.
162
e.printStackTrace();
163       }
164     }
165     result_set = null;
166     meta_data = null;
167     row_count = 0;
168     fireTableStructureChanged();
169   }
170
171   // ---------- Implemented from AbstractTableModel ----------
172

173   public int getRowCount() {
174     return row_count;
175   }
176
177   public int getColumnCount() {
178     if (meta_data != null) {
179       try {
180         return meta_data.getColumnCount();
181       }
182       catch (SQLException e) {
183         throw new Error JavaDoc("SQL Exception: " + e.getMessage());
184       }
185     }
186     return 0;
187   }
188
189   public String JavaDoc getColumnName(int column) {
190     if (meta_data != null) {
191       try {
192         return meta_data.getColumnLabel(column + 1);
193       }
194       catch (SQLException e) {
195         throw new Error JavaDoc("SQL Exception: " + e.getMessage());
196       }
197     }
198     throw new Error JavaDoc("No columns!");
199   }
200
201   public Object JavaDoc getValueAt(int row, int column) {
202     if (result_set != null) {
203       try {
204         result_set.absolute(row + 1);
205         return result_set.getObject(column + 1);
206       }
207       catch (SQLException e) {
208         throw new Error JavaDoc("SQL Exception: " + e.getMessage());
209       }
210     }
211     throw new Error JavaDoc("No contents!");
212   }
213
214 }
215
Popular Tags