KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > QueryBuilderResultTable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
20
21 import java.util.Vector JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.event.*;
25 import javax.swing.JTable JavaDoc;
26 import javax.swing.table.DefaultTableModel JavaDoc;
27
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.ResultSetMetaData JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import org.openide.util.NbBundle;
33 import org.openide.windows.TopComponent;
34 import org.openide.windows.WindowManager;
35 import org.openide.windows.Mode;
36 import org.openide.ErrorManager;
37 import org.openide.NotifyDescriptor;
38 import org.openide.DialogDisplayer;
39
40 import org.netbeans.modules.db.sql.visualeditor.Log;
41 import java.sql.Blob JavaDoc;
42 import java.sql.Clob JavaDoc;
43 import java.sql.Timestamp JavaDoc;
44 import java.sql.Date JavaDoc;
45 import java.sql.Time JavaDoc;
46
47 import java.text.DateFormat JavaDoc ;
48
49 /**
50  * A table for displaying query results in the query editor
51  * @author Sanjay Dhamankar, Jim Davidson
52  */

53 public class QueryBuilderResultTable extends JTable JavaDoc
54                         implements KeyListener {
55
56     private DefaultTableModel JavaDoc resultTableModel = null;
57     private QueryBuilder _queryBuilder;
58
59     public QueryBuilderResultTable() {
60         this(null);
61     }
62
63     /** Constructor which takes the parent as parameter */
64     public QueryBuilderResultTable(QueryBuilder queryBuilder) {
65
66         super();
67
68         _queryBuilder = queryBuilder;
69         resultTableModel = new DefaultTableModel JavaDoc() {
70             public boolean isCellEditable ( int row, int column ) {
71                 return false;
72             }
73         };
74         this.setModel(resultTableModel);
75
76         this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
77 // this.setMinimumSize(new Dimension(200, 30) );
78
// this.setPreferredSize(new Dimension(200, 30) );
79

80         // change foreground color to Dark Gray ...
81
this.setForeground(Color.DARK_GRAY);
82         addKeyListener(this);
83     }
84
85
86     /** ignore */
87     public void keyTyped(KeyEvent e) {
88     }
89
90     /** ignore */
91     public void keyReleased(KeyEvent e) {
92     }
93
94     /** Handle the key pressed event and change the focus if a particular
95      * key combination is pressed. */

96     public void keyPressed(KeyEvent e) {
97         if ( _queryBuilder != null ) _queryBuilder.handleKeyPress(e);
98     }
99
100     /**
101      * Clear the model
102      */

103     void clearModel() {
104         ((DefaultTableModel JavaDoc)this.getModel()).setRowCount(0);
105     }
106
107     /**
108      * Update the table model from the ResultSet
109      * @param the result set associated with the table
110      */

111     public void displayResultSet(ResultSet JavaDoc rs) {
112         displayResultSet( rs, 40, true ) ;
113     }
114     public boolean displayResultSet(ResultSet JavaDoc rs, int maxEntries, boolean infoMsgIfTruncated ) {
115         // Check validity of ResultSet
116
ResultSetMetaData JavaDoc rsmd;
117         boolean resultsTruncated = false ;
118         try {
119             if ((rs == null) ||
120             ((rsmd=rs.getMetaData())==null)) {
121                 //Thread.dumpStack();
122
Log.err.log(ErrorManager.EXCEPTION,
123                 "Exception - unable to get query result ! "); // NOI18N
124
return resultsTruncated;
125             }
126
127             // Get Column Names
128
int numberOfColumns = rsmd.getColumnCount();
129             Log.err.log(ErrorManager.INFORMATIONAL,
130             " rsmd.getColumnCount(): " + numberOfColumns); // NOI18N
131

132             // Create a vector of column names, for headers
133
String JavaDoc[] dbColumnNames = new String JavaDoc[numberOfColumns];
134             for (int i = 1; i <= numberOfColumns; i++) {
135                 dbColumnNames[i - 1] = rsmd.getColumnName(i);
136             }
137
138             // Set Column Headers; this only works with DefaultTableModel
139
resultTableModel.setColumnIdentifiers(dbColumnNames);
140
141             // Discard all rows in the current model
142
resultTableModel.setRowCount(0);
143             
144             // Process the result set, producing a vector
145
// For each row, produce a 1-d array that gets added to the Model
146

147             // Add a check to diaplay ONLY first 40 entries
148
// Add buttons "PREVIOUS", "NEXT" under the result table and
149
// show the proper entires. This is to avoid the
150
// OutOfMemoryException
151

152             // int maxEntries = 40;
153
int count = 0;
154             while (rs.next() && count < maxEntries + 1 ) {
155                 if ( count >= maxEntries ) {
156                     resultsTruncated = true ;
157                     break ;
158                 }
159                 Object JavaDoc[] row = new Object JavaDoc[numberOfColumns];
160                 for (int i = 1; i <= numberOfColumns; i++) {
161                     // since this is an array, we start at 0
162

163                     // Do not show the values of Blob & Clob
164
if(rsmd.getColumnType(i) == java.sql.Types.BLOB){
165                         Blob JavaDoc blobData = rs.getBlob(i);
166                         // Added check to fix
167
// 5064319 : ServerNav> View Data on a Db2 Table with
168
// BLOB column throws NPE
169
if ( blobData != null )
170                             row[i - 1] = "[BLOB of size " + blobData.length() + "]"; //NOI18N
171
}else if(rsmd.getColumnType(i) == java.sql.Types.CLOB){
172                         Clob JavaDoc clobData = rs.getClob(i);
173                         // Added check to fix
174
// 5064319 : ServerNav> View Data on a Db2 Table with
175
// BLOB column throws NPE
176
if ( clobData != null )
177                             row[i - 1] = "[CLOB of size " + clobData.length() + "]"; //NOI18N
178
}
179                     // convert timestamp to the current locale
180
else if ( rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP){
181                         Timestamp JavaDoc timeStampData = rs.getTimestamp(i);
182
183                         // Added check to fix
184
// 5062947 : Null Date Breaks Viewing Table Data
185
if ( timeStampData != null ) {
186                             row[i - 1] = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM).format( timeStampData ) ;
187                         }
188                     }
189                     // convert date to the current locale
190
else if ( rsmd.getColumnType(i) == java.sql.Types.DATE){
191                         Date JavaDoc dateData = rs.getDate(i);
192                         // Added check to fix
193
// 5062947 : Null Date Breaks Viewing Table Data
194
if ( dateData != null ) {
195                             row[i - 1] = DateFormat.getDateInstance(DateFormat.SHORT).format( dateData ) ;
196                         }
197                     }
198                     // convert time to the current locale
199
else if ( rsmd.getColumnType(i) == java.sql.Types.TIME){
200                         Time JavaDoc timeData = rs.getTime(i);
201                         // Added check to fix
202
// 5062947 : Null Date Breaks Viewing Table Data
203
if ( timeData != null ) {
204                             row[i - 1] = java.text.DateFormat.getTimeInstance(DateFormat.MEDIUM).format(timeData );
205                         }
206                     }
207                     else {
208                           row[i - 1] = rs.getObject(i);
209                     }
210                 }
211                 resultTableModel.addRow(row);
212                 count++;
213             }
214             if ( resultsTruncated && infoMsgIfTruncated ) {
215                 String JavaDoc msg = NbBundle.getMessage(QueryBuilderResultTable.class, "MAX_ENTRIES_DISPLAYED", Integer.toString(maxEntries)); // NOI18N
216
NotifyDescriptor d =
217                 new NotifyDescriptor.Message(msg + "\n\n", NotifyDescriptor.INFORMATION_MESSAGE); // NOI18N
218
DialogDisplayer.getDefault().notify(d);
219             }
220         } catch(SQLException JavaDoc sqle) {
221             sqle.printStackTrace();
222             Log.err.log(ErrorManager.EXCEPTION, "Exception - unable to build table"); // NOI18N
223
}finally{
224             if (rs != null){
225                 try{
226                     rs.close();
227                 }catch (Exception JavaDoc exc){
228                     
229                 }
230             }
231         }
232         return resultsTruncated ;
233         
234     }
235 }
236
Popular Tags