001: package org.mandarax.examples.jdbc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.awt.*;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.MouseAdapter;
024: import java.awt.event.MouseEvent;
025: import java.awt.event.MouseListener;
026: import java.util.*;
027: import java.sql.*;
028:
029: import javax.swing.event.ListSelectionEvent;
030: import javax.swing.event.ListSelectionListener;
031: import javax.swing.table.*;
032: import javax.swing.*;
033:
034: import org.mandarax.jdbc.server.PseudoColumns;
035: import org.mandarax.kernel.Derivation;
036:
037: import java.text.DateFormat;
038:
039: /**
040: * Component to visualize SQL Result sets.
041: * Rather simplem e.g. all results are feched and displayed immidiately.
042: * @author <a href="mailto:jens@jbdietrich.com">Jens Dietrich</a>
043: * @version 3.3.2 <29 December 2004>
044: */
045: class SimpleSQLResultSetView extends javax.swing.JPanel {
046: private ResultSet rs = null;
047: private String remark = null;
048: private JTable table = null;
049: Action actShowDerivation = null;
050:
051: /**
052: * Constructor.
053: */
054: SimpleSQLResultSetView() {
055: super ();
056: initialize();
057: }
058:
059: /**
060: * Get the table model for the respective result set.
061: * @return javax.swing.table.TableModel
062: */
063: private TableModel getTableModel() {
064:
065: if (rs == null) {
066: TableModel model = new AbstractTableModel() {
067: public int getRowCount() {
068: return 0;
069: }
070:
071: public int getColumnCount() {
072: return 0;
073: }
074:
075: public Object getValueAt(int rowIndex, int columnIndex) {
076: return "";
077: }
078: };
079: return model;
080: }
081:
082: final Vector rows = new Vector();
083: final Vector colNames = new Vector();
084: int cc = 0;
085:
086: try {
087: ResultSetMetaData metaData = rs.getMetaData();
088: cc = metaData.getColumnCount();
089: for (int i = 1; i <= cc; i++) {
090: colNames.add(metaData.getColumnName(i));
091: }
092: ;
093:
094: while (rs.next()) {
095: Vector row = new Vector(cc);
096: for (int i = 1; i <= cc; i++)
097: row.add(toString(rs.getObject(i)));
098: rows.add(row);
099: }
100: } catch (Exception x) {
101: remark = rows.size() == 0 ? "Error, cannot fetch rows"
102: : "Error - cannot fetch all rows";
103: }
104:
105: final int colCount = cc;
106:
107: TableModel model = new AbstractTableModel() {
108: public int getColumnCount() {
109: return colCount;
110: }
111:
112: public int getRowCount() {
113: return rows.size();
114: }
115:
116: public Object getValueAt(int row, int col) {
117: Vector r = (Vector) rows.get(row);
118: if (r == null)
119: return null;
120: else
121: return r.get(col);
122: }
123:
124: public String getColumnName(int index) {
125: return index < colNames.size() ? colNames.get(index)
126: .toString() : "column " + index;
127: };
128: };
129: return model;
130: }
131:
132: /**
133: * Initialize the object.
134: */
135: private void initialize() {
136: TableModel tableModel = getTableModel();
137: setLayout(new BorderLayout(5, 5));
138: table = new JTable(tableModel);
139: add(new JScrollPane(table), BorderLayout.CENTER);
140:
141: // add event handler to display derivation on double click
142: table.addMouseListener(new MouseAdapter() {
143: public void mouseClicked(MouseEvent e) {
144: if (e.getClickCount() == 2)
145: showDerivation();
146: }
147: });
148: table.getSelectionModel().addListSelectionListener(
149: new ListSelectionListener() {
150: public void valueChanged(ListSelectionEvent e) {
151: actShowDerivation.setEnabled(table
152: .getSelectedRow() > -1);
153: }
154: });
155: // init actions
156: actShowDerivation = new AbstractAction("Show Derivation") {
157: public void actionPerformed(ActionEvent e) {
158: showDerivation();
159: }
160: };
161:
162: }
163:
164: /**
165: * Handle a double click event.
166: */
167: private void showDerivation() {
168: int row = table.getSelectedRow();
169: if (row > -1 && rs != null) {
170: try {
171: rs.absolute(row + 1);
172: Derivation derivation = (Derivation) rs
173: .getObject(PseudoColumns.PSEUDO_COLUMNS[0]);
174: if (derivation != null) {
175: try {
176: SimpleDerivationView
177: .show(this , derivation, row);
178: } catch (Exception x) {
179: x.printStackTrace();
180: }
181: }
182: } catch (Exception x) {
183: x.printStackTrace();
184: }
185: }
186: }
187:
188: /**
189: * Execute an sql statement and display the results.
190: * @param sql an sql statement
191: * @param connection a connection
192: */
193: void execute(Connection conn, String sql) {
194: // clean up old result set
195: try {
196: if (rs != null)
197: rs.close();
198: rs = null;
199: } catch (SQLException x) {
200: }
201: // execute query, display result
202: try {
203: display(conn.createStatement().executeQuery(sql));
204: } catch (Exception x) {
205: JOptionPane.showMessageDialog(this , "Cannot perform query "
206: + sql + " see console for details", "Error",
207: JOptionPane.ERROR_MESSAGE);
208: x.printStackTrace();
209: }
210: }
211:
212: /**
213: * Display an SQL result set.
214: * @param rs a result set
215: */
216: void display(ResultSet rs) {
217: // clean up old result set
218: try {
219: if (this .rs != null)
220: this .rs.close();
221: this .rs = rs;
222: } catch (SQLException x) {
223: JOptionPane
224: .showMessageDialog(
225: this ,
226: "Cannot display result set, see console for details",
227: "Error", JOptionPane.ERROR_MESSAGE);
228: x.printStackTrace();
229: } finally {
230: actShowDerivation.setEnabled(false);
231: table.setModel(getTableModel());
232: }
233: }
234:
235: /**
236: * Convert an object read from the database to a string.
237: * @return java.lang.String
238: * @param obj java.lang.Object
239: */
240: private String toString(Object obj) {
241: if (obj == null)
242: return "null";
243: else {
244: // special handling for data and time
245: if (obj instanceof java.util.Date) {
246: if (obj instanceof java.sql.Date)
247: return DateFormat.getDateInstance().format(
248: (java.util.Date) obj);
249: if (obj instanceof java.sql.Time)
250: return DateFormat.getTimeInstance().format(
251: (java.util.Date) obj);
252: if (obj instanceof Timestamp)
253: return DateFormat.getDateTimeInstance().format(
254: (java.util.Date) obj);
255: } else if (obj instanceof Derivation)
256: return "<a mandarax derivation>";
257:
258: return obj.toString();
259: }
260: }
261:
262: /**
263: * Clear the view.
264: */
265: void clear() {
266: display(null);
267: }
268: }
|