1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * ResultSetTableModel.java |
---|
19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.sql; |
---|
24 | |
---|
25 | import java.sql.ResultSet; |
---|
26 | import java.util.HashSet; |
---|
27 | |
---|
28 | import javax.swing.event.TableModelListener; |
---|
29 | import javax.swing.table.TableModel; |
---|
30 | |
---|
31 | /** |
---|
32 | * The model for an SQL ResultSet. |
---|
33 | * |
---|
34 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
35 | * @version $Revision: 1.3 $ |
---|
36 | */ |
---|
37 | public class ResultSetTableModel implements TableModel { |
---|
38 | |
---|
39 | /** the listeners. */ |
---|
40 | protected HashSet m_Listeners; |
---|
41 | |
---|
42 | /** the data. */ |
---|
43 | protected Object[][] m_Data; |
---|
44 | |
---|
45 | /** for retrieving the data etc. */ |
---|
46 | protected ResultSetHelper m_Helper; |
---|
47 | |
---|
48 | /** |
---|
49 | * initializes the model, retrieves all rows. |
---|
50 | * |
---|
51 | * @param rs the ResultSet to get the data from |
---|
52 | */ |
---|
53 | public ResultSetTableModel(ResultSet rs) { |
---|
54 | this(rs, 0); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * initializes the model, retrieves only the given amount of rows (0 means |
---|
59 | * all). |
---|
60 | * |
---|
61 | * @param rs the ResultSet to get the data from |
---|
62 | * @param rows the maximum number of rows to retrieve, 0 retrieves all |
---|
63 | */ |
---|
64 | public ResultSetTableModel(ResultSet rs, int rows) { |
---|
65 | super(); |
---|
66 | |
---|
67 | m_Listeners = new HashSet(); |
---|
68 | m_Helper = new ResultSetHelper(rs, rows); |
---|
69 | m_Data = m_Helper.getCells(); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * adds a listener to the list that is notified each time a change to data |
---|
74 | * model occurs. |
---|
75 | * |
---|
76 | * @param l the listener to add |
---|
77 | */ |
---|
78 | public void addTableModelListener(TableModelListener l) { |
---|
79 | m_Listeners.add(l); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * returns the most specific superclass for all the cell values in the |
---|
84 | * column (always String). |
---|
85 | * |
---|
86 | * @param columnIndex the index of the column |
---|
87 | * @return the class |
---|
88 | */ |
---|
89 | public Class getColumnClass(int columnIndex) { |
---|
90 | Class result; |
---|
91 | |
---|
92 | result = null; |
---|
93 | |
---|
94 | if ( (m_Helper.getColumnClasses() != null) |
---|
95 | && (columnIndex >= 0) |
---|
96 | && (columnIndex < getColumnCount()) ) { |
---|
97 | if (columnIndex == 0) |
---|
98 | result = Integer.class; |
---|
99 | else |
---|
100 | result = m_Helper.getColumnClasses()[columnIndex - 1]; |
---|
101 | } |
---|
102 | |
---|
103 | return result; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * returns the number of columns in the model. |
---|
108 | * |
---|
109 | * @return the number of columns |
---|
110 | */ |
---|
111 | public int getColumnCount() { |
---|
112 | return m_Helper.getColumnCount() + 1; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * returns the name of the column at columnIndex. |
---|
117 | * |
---|
118 | * @param columnIndex the index of the column |
---|
119 | * @return the name |
---|
120 | */ |
---|
121 | public String getColumnName(int columnIndex) { |
---|
122 | String result; |
---|
123 | |
---|
124 | result = ""; |
---|
125 | |
---|
126 | if ( (m_Helper.getColumnNames() != null) |
---|
127 | && (columnIndex >= 0) |
---|
128 | && (columnIndex < getColumnCount()) ) { |
---|
129 | if (columnIndex == 0) |
---|
130 | result = "Row"; |
---|
131 | else |
---|
132 | result = m_Helper.getColumnNames()[columnIndex - 1]; |
---|
133 | } |
---|
134 | |
---|
135 | return result; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * returns the number of rows in the model. |
---|
140 | * |
---|
141 | * @return the number of data rows |
---|
142 | */ |
---|
143 | public int getRowCount() { |
---|
144 | return m_Data.length; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * returns the value for the cell at columnindex and rowIndex. |
---|
149 | * |
---|
150 | * @param rowIndex the row of the cell |
---|
151 | * @param columnIndex the column of the cell |
---|
152 | * @return the data value |
---|
153 | */ |
---|
154 | public Object getValueAt(int rowIndex, int columnIndex) { |
---|
155 | Object result; |
---|
156 | |
---|
157 | result = null; |
---|
158 | |
---|
159 | if ( (rowIndex >= 0) && (rowIndex < getRowCount()) |
---|
160 | && (columnIndex >= 0) && (columnIndex < getColumnCount()) ) { |
---|
161 | if (columnIndex == 0) |
---|
162 | result = new Integer(rowIndex + 1); |
---|
163 | else |
---|
164 | result = m_Data[rowIndex][columnIndex - 1]; |
---|
165 | } |
---|
166 | |
---|
167 | return result; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * checks whether the value of the cell is NULL. |
---|
172 | * |
---|
173 | * @param rowIndex the row of the cell |
---|
174 | * @param columnIndex the column of the cell |
---|
175 | * @return true if the cell value is NULL |
---|
176 | */ |
---|
177 | public boolean isNullAt(int rowIndex, int columnIndex) { |
---|
178 | return (getValueAt(rowIndex, columnIndex) == null); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * returns whether the column at the given index is numeric. |
---|
183 | * |
---|
184 | * @param columnIndex the column to check |
---|
185 | * @return whether the column is numeric |
---|
186 | */ |
---|
187 | public boolean isNumericAt(int columnIndex) { |
---|
188 | boolean result; |
---|
189 | |
---|
190 | result = false; |
---|
191 | |
---|
192 | if ( (columnIndex >= 0) && (columnIndex < getColumnCount()) ) { |
---|
193 | if (columnIndex == 0) { |
---|
194 | result = true; |
---|
195 | } |
---|
196 | else { |
---|
197 | if (m_Helper.getNumericColumns() == null) |
---|
198 | result = false; |
---|
199 | else |
---|
200 | result = m_Helper.getNumericColumns()[columnIndex - 1]; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | return result; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * returns true if the cell at rowindex and columnindexis editable. |
---|
209 | * |
---|
210 | * @param rowIndex the row of the cell |
---|
211 | * @param columnIndex the column of the cell |
---|
212 | * @return always false |
---|
213 | */ |
---|
214 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
---|
215 | return false; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * removes a listener from the list that is notified each time a change to |
---|
220 | * the data model occurs. |
---|
221 | * |
---|
222 | * @param l the listener to remove |
---|
223 | */ |
---|
224 | public void removeTableModelListener(TableModelListener l) { |
---|
225 | m_Listeners.remove(l); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * sets the value in the cell at columnIndex and rowIndex to aValue. |
---|
230 | * Ignored. |
---|
231 | * |
---|
232 | * @param aValue the value to set - ignored |
---|
233 | * @param rowIndex the row of the cell |
---|
234 | * @param columnIndex the column of the cell |
---|
235 | */ |
---|
236 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
---|
237 | // ignore |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * frees up the memory. |
---|
242 | * |
---|
243 | * @throws Throwable if something goes wrong |
---|
244 | */ |
---|
245 | public void finalize() throws Throwable { |
---|
246 | try { |
---|
247 | m_Helper.getResultSet().close(); |
---|
248 | m_Helper.getResultSet().getStatement().close(); |
---|
249 | m_Helper = null; |
---|
250 | } |
---|
251 | catch (Exception e) { |
---|
252 | // ignored |
---|
253 | } |
---|
254 | |
---|
255 | m_Data = null; |
---|
256 | |
---|
257 | super.finalize(); |
---|
258 | } |
---|
259 | } |
---|