1 19 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 20 21 import java.util.Vector ; 22 import java.awt.Dimension ; 23 import java.awt.Color ; 24 import java.awt.event.*; 25 import javax.swing.JTable ; 26 import javax.swing.table.DefaultTableModel ; 27 28 import java.sql.ResultSet ; 29 import java.sql.ResultSetMetaData ; 30 import java.sql.SQLException ; 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 ; 42 import java.sql.Clob ; 43 import java.sql.Timestamp ; 44 import java.sql.Date ; 45 import java.sql.Time ; 46 47 import java.text.DateFormat ; 48 49 53 public class QueryBuilderResultTable extends JTable 54 implements KeyListener { 55 56 private DefaultTableModel resultTableModel = null; 57 private QueryBuilder _queryBuilder; 58 59 public QueryBuilderResultTable() { 60 this(null); 61 } 62 63 64 public QueryBuilderResultTable(QueryBuilder queryBuilder) { 65 66 super(); 67 68 _queryBuilder = queryBuilder; 69 resultTableModel = new DefaultTableModel () { 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 80 this.setForeground(Color.DARK_GRAY); 82 addKeyListener(this); 83 } 84 85 86 87 public void keyTyped(KeyEvent e) { 88 } 89 90 91 public void keyReleased(KeyEvent e) { 92 } 93 94 96 public void keyPressed(KeyEvent e) { 97 if ( _queryBuilder != null ) _queryBuilder.handleKeyPress(e); 98 } 99 100 103 void clearModel() { 104 ((DefaultTableModel )this.getModel()).setRowCount(0); 105 } 106 107 111 public void displayResultSet(ResultSet rs) { 112 displayResultSet( rs, 40, true ) ; 113 } 114 public boolean displayResultSet(ResultSet rs, int maxEntries, boolean infoMsgIfTruncated ) { 115 ResultSetMetaData rsmd; 117 boolean resultsTruncated = false ; 118 try { 119 if ((rs == null) || 120 ((rsmd=rs.getMetaData())==null)) { 121 Log.err.log(ErrorManager.EXCEPTION, 123 "Exception - unable to get query result ! "); return resultsTruncated; 125 } 126 127 int numberOfColumns = rsmd.getColumnCount(); 129 Log.err.log(ErrorManager.INFORMATIONAL, 130 " rsmd.getColumnCount(): " + numberOfColumns); 132 String [] dbColumnNames = new String [numberOfColumns]; 134 for (int i = 1; i <= numberOfColumns; i++) { 135 dbColumnNames[i - 1] = rsmd.getColumnName(i); 136 } 137 138 resultTableModel.setColumnIdentifiers(dbColumnNames); 140 141 resultTableModel.setRowCount(0); 143 144 147 152 int count = 0; 154 while (rs.next() && count < maxEntries + 1 ) { 155 if ( count >= maxEntries ) { 156 resultsTruncated = true ; 157 break ; 158 } 159 Object [] row = new Object [numberOfColumns]; 160 for (int i = 1; i <= numberOfColumns; i++) { 161 163 if(rsmd.getColumnType(i) == java.sql.Types.BLOB){ 165 Blob blobData = rs.getBlob(i); 166 if ( blobData != null ) 170 row[i - 1] = "[BLOB of size " + blobData.length() + "]"; }else if(rsmd.getColumnType(i) == java.sql.Types.CLOB){ 172 Clob clobData = rs.getClob(i); 173 if ( clobData != null ) 177 row[i - 1] = "[CLOB of size " + clobData.length() + "]"; } 179 else if ( rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP){ 181 Timestamp timeStampData = rs.getTimestamp(i); 182 183 if ( timeStampData != null ) { 186 row[i - 1] = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM).format( timeStampData ) ; 187 } 188 } 189 else if ( rsmd.getColumnType(i) == java.sql.Types.DATE){ 191 Date dateData = rs.getDate(i); 192 if ( dateData != null ) { 195 row[i - 1] = DateFormat.getDateInstance(DateFormat.SHORT).format( dateData ) ; 196 } 197 } 198 else if ( rsmd.getColumnType(i) == java.sql.Types.TIME){ 200 Time timeData = rs.getTime(i); 201 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 msg = NbBundle.getMessage(QueryBuilderResultTable.class, "MAX_ENTRIES_DISPLAYED", Integer.toString(maxEntries)); NotifyDescriptor d = 217 new NotifyDescriptor.Message(msg + "\n\n", NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(d); 219 } 220 } catch(SQLException sqle) { 221 sqle.printStackTrace(); 222 Log.err.log(ErrorManager.EXCEPTION, "Exception - unable to build table"); }finally{ 224 if (rs != null){ 225 try{ 226 rs.close(); 227 }catch (Exception exc){ 228 229 } 230 } 231 } 232 return resultsTruncated ; 233 234 } 235 } 236 | Popular Tags |