KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > execute > ui > ResultSetTableModel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.execute.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.ResultSetMetaData JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.table.AbstractTableModel JavaDoc;
29 import org.netbeans.modules.db.core.SQLOptions;
30 import org.openide.DialogDisplayer;
31 import org.openide.NotifyDescriptor;
32 import org.openide.util.NbBundle;
33 import org.netbeans.modules.db.sql.execute.ColumnDef;
34 import org.netbeans.modules.db.sql.execute.FetchLimitHandler;
35 import org.netbeans.modules.db.sql.execute.ResultSetTableModelSupport;
36 import org.openide.awt.Mnemonics;
37
38 /**
39  * TableModel based on a ResultSet.
40  *
41  * @author Andrei Badea
42  */

43 public class ResultSetTableModel extends AbstractTableModel JavaDoc {
44         
45     // TODO: be conservative for editing: no editing for sensitive result sets
46
// TODO: maybe the fetch handler should be received as a param
47

48     private final List JavaDoc/*<ColumnDef>*/ columnDefs;
49     private final List JavaDoc/*<List>*/ rows;
50     
51     /**
52      * @throws SQLException if a database error occurred
53      * @throws IOException if an error occurred while reading data from a column
54      *
55      * @return a TableModel for the ResultSet or null if the calling thread was
56      * interrupted
57      */

58     public static ResultSetTableModel create(ResultSet JavaDoc rs) throws SQLException JavaDoc, IOException JavaDoc {
59         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
60         List JavaDoc columnDefs = ResultSetTableModelSupport.createColumnDefs(rsmd);
61         if (columnDefs == null) { // thread interrupted
62
return null;
63         }
64         List JavaDoc rows = ResultSetTableModelSupport.retrieveRows(rs, rsmd, new FetchLimitHandlerImpl());
65         if (rows == null) { // thread interrupted
66
return null;
67         }
68         return new ResultSetTableModel(columnDefs, rows);
69     }
70     
71     private ResultSetTableModel(List JavaDoc/*<ColumnDef>*/ columnDefs, List JavaDoc/*<List>*/ rows) {
72         this.columnDefs = columnDefs;
73         this.rows = rows;
74     }
75
76     public int getRowCount() {
77         return rows.size();
78     }
79
80     public int getColumnCount() {
81         return columnDefs.size();
82     }
83
84     public Object JavaDoc getValueAt(int row, int column) {
85         List JavaDoc rowData = (List JavaDoc)rows.get(row);
86         return rowData.get(column);
87     }
88
89     public boolean isCellEditable(int row, int column) {
90         return false;
91     }
92
93     public String JavaDoc getColumnName(int column) {
94         return getColumnDef(column).getName();
95     }
96
97     public Class JavaDoc getColumnClass(int column) {
98         return getColumnDef(column).getDisplayClass();
99     }
100
101     private ColumnDef getColumnDef(int column) {
102         return (ColumnDef)columnDefs.get(column);
103     }
104     
105     private static final class FetchLimitHandlerImpl implements FetchLimitHandler {
106         
107         public int fetchLimitReached(int fetchCount) {
108             JButton JavaDoc fetchYes = new JButton JavaDoc();
109             Mnemonics.setLocalizedText(fetchYes, NbBundle.getMessage(ResultSetTableModel.class, "LBL_FetchYes"));
110             fetchYes.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ResultSetTableModel.class, "ACSD_FetchYes"));
111             
112             JButton JavaDoc fetchAll = new JButton JavaDoc();
113             Mnemonics.setLocalizedText(fetchAll, NbBundle.getMessage(ResultSetTableModel.class, "LBL_FetchAll"));
114             fetchAll.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ResultSetTableModel.class, "ACSD_FetchAll"));
115             
116             JButton JavaDoc fetchNo = new JButton JavaDoc();
117             Mnemonics.setLocalizedText(fetchNo, NbBundle.getMessage(ResultSetTableModel.class, "LBL_FetchNo"));
118             fetchNo.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ResultSetTableModel.class, "ACSD_FetchNo"));
119             
120             String JavaDoc title = NbBundle.getMessage(ResultSetTableModel.class, "LBL_FetchNextTitle");
121             String JavaDoc message = NbBundle.getMessage(ResultSetTableModel.class, "LBL_FetchNextMessage",
122                     new Object JavaDoc[] { new Integer JavaDoc(fetchCount), new Integer JavaDoc(SQLOptions.getDefault().getFetchStep()) });
123             
124             NotifyDescriptor desc = new NotifyDescriptor(message, title, NotifyDescriptor.YES_NO_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE, new Object JavaDoc[] { fetchYes, fetchAll , fetchNo }, NotifyDescriptor.CANCEL_OPTION);
125             Object JavaDoc ret = DialogDisplayer.getDefault().notify(desc);
126             
127             if (ret instanceof JButton JavaDoc) {
128                 if (ret == fetchYes) {
129                     return fetchCount + SQLOptions.getDefault().getFetchStep();
130                 } else if (ret == fetchAll) {
131                     return 0;
132                 } else {
133                     return fetchCount;
134                 }
135             } else {
136                 // dialog closed using the close button or the Esc key
137
return fetchCount;
138             }
139         }
140
141         public int getFetchLimit() {
142             return SQLOptions.getDefault().getFetchStep();
143         }
144     }
145 }
146
Popular Tags