001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.sql.execute.ui;
043:
044: import java.io.IOException;
045: import java.sql.DatabaseMetaData;
046: import java.sql.ResultSet;
047: import java.sql.ResultSetMetaData;
048: import java.sql.SQLException;
049: import java.util.List;
050: import javax.swing.JButton;
051: import javax.swing.table.AbstractTableModel;
052: import org.netbeans.modules.db.core.SQLOptions;
053: import org.openide.DialogDisplayer;
054: import org.openide.NotifyDescriptor;
055: import org.openide.util.NbBundle;
056: import org.netbeans.modules.db.sql.execute.ColumnDef;
057: import org.netbeans.modules.db.sql.execute.FetchLimitHandler;
058: import org.netbeans.modules.db.sql.execute.ResultSetTableModelSupport;
059: import org.openide.awt.Mnemonics;
060:
061: /**
062: * TableModel based on a ResultSet.
063: *
064: * @author Andrei Badea
065: */
066: public class ResultSetTableModel extends AbstractTableModel {
067:
068: // TODO: be conservative for editing: no editing for sensitive result sets
069: // TODO: maybe the fetch handler should be received as a param
070:
071: private final List/*<ColumnDef>*/columnDefs;
072: private final List/*<List>*/rows;
073:
074: /**
075: * @throws SQLException if a database error occurred
076: * @throws IOException if an error occurred while reading data from a column
077: *
078: * @return a TableModel for the ResultSet or null if the calling thread was
079: * interrupted
080: */
081: public static ResultSetTableModel create(DatabaseMetaData dbmd,
082: ResultSet rs) throws SQLException, IOException {
083: ResultSetMetaData rsmd = rs.getMetaData();
084: List columnDefs = ResultSetTableModelSupport.createColumnDefs(
085: dbmd, rsmd);
086: if (columnDefs == null) { // thread interrupted
087: return null;
088: }
089: List rows = ResultSetTableModelSupport.retrieveRows(dbmd, rs,
090: rsmd, new FetchLimitHandlerImpl());
091: if (rows == null) { // thread interrupted
092: return null;
093: }
094: return new ResultSetTableModel(columnDefs, rows);
095: }
096:
097: private ResultSetTableModel(List/*<ColumnDef>*/columnDefs,
098: List/*<List>*/rows) {
099: this .columnDefs = columnDefs;
100: this .rows = rows;
101: }
102:
103: public int getRowCount() {
104: return rows.size();
105: }
106:
107: public int getColumnCount() {
108: return columnDefs.size();
109: }
110:
111: public Object getValueAt(int row, int column) {
112: List rowData = (List) rows.get(row);
113: return rowData.get(column);
114: }
115:
116: public boolean isCellEditable(int row, int column) {
117: return false;
118: }
119:
120: public String getColumnName(int column) {
121: return getColumnDef(column).getName();
122: }
123:
124: public Class getColumnClass(int column) {
125: return getColumnDef(column).getDisplayClass();
126: }
127:
128: private ColumnDef getColumnDef(int column) {
129: return (ColumnDef) columnDefs.get(column);
130: }
131:
132: private static final class FetchLimitHandlerImpl implements
133: FetchLimitHandler {
134:
135: public int fetchLimitReached(int fetchCount) {
136: JButton fetchYes = new JButton();
137: Mnemonics.setLocalizedText(fetchYes, NbBundle.getMessage(
138: ResultSetTableModel.class, "LBL_FetchYes"));
139: fetchYes.getAccessibleContext().setAccessibleDescription(
140: NbBundle.getMessage(ResultSetTableModel.class,
141: "ACSD_FetchYes"));
142:
143: JButton fetchAll = new JButton();
144: Mnemonics.setLocalizedText(fetchAll, NbBundle.getMessage(
145: ResultSetTableModel.class, "LBL_FetchAll"));
146: fetchAll.getAccessibleContext().setAccessibleDescription(
147: NbBundle.getMessage(ResultSetTableModel.class,
148: "ACSD_FetchAll"));
149:
150: JButton fetchNo = new JButton();
151: Mnemonics.setLocalizedText(fetchNo, NbBundle.getMessage(
152: ResultSetTableModel.class, "LBL_FetchNo"));
153: fetchNo.getAccessibleContext().setAccessibleDescription(
154: NbBundle.getMessage(ResultSetTableModel.class,
155: "ACSD_FetchNo"));
156:
157: String title = NbBundle.getMessage(
158: ResultSetTableModel.class, "LBL_FetchNextTitle");
159: String message = NbBundle.getMessage(
160: ResultSetTableModel.class, "LBL_FetchNextMessage",
161: new Object[] {
162: new Integer(fetchCount),
163: new Integer(SQLOptions.getDefault()
164: .getFetchStep()) });
165:
166: NotifyDescriptor desc = new NotifyDescriptor(message,
167: title, NotifyDescriptor.YES_NO_CANCEL_OPTION,
168: NotifyDescriptor.QUESTION_MESSAGE, new Object[] {
169: fetchYes, fetchAll, fetchNo },
170: NotifyDescriptor.CANCEL_OPTION);
171: Object ret = DialogDisplayer.getDefault().notify(desc);
172:
173: if (ret instanceof JButton) {
174: if (ret == fetchYes) {
175: return fetchCount
176: + SQLOptions.getDefault().getFetchStep();
177: } else if (ret == fetchAll) {
178: return 0;
179: } else {
180: return fetchCount;
181: }
182: } else {
183: // dialog closed using the close button or the Esc key
184: return fetchCount;
185: }
186: }
187:
188: public int getFetchLimit() {
189: return SQLOptions.getDefault().getFetchStep();
190: }
191: }
192: }
|