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.awt.datatransfer.StringSelection;
045: import java.awt.event.MouseEvent;
046: import java.sql.Time;
047: import java.sql.Timestamp;
048: import java.text.DateFormat;
049: import javax.swing.JTable;
050: import javax.swing.UIManager;
051: import javax.swing.table.DefaultTableCellRenderer;
052: import javax.swing.table.DefaultTableModel;
053: import javax.swing.table.TableCellRenderer;
054: import javax.swing.table.TableModel;
055: import org.netbeans.modules.db.sql.execute.NullValue;
056: import org.openide.util.Lookup;
057: import org.netbeans.modules.db.sql.execute.SQLExecutionResults;
058: import org.openide.util.datatransfer.ExClipboard;
059:
060: /**
061: *
062: * @author Andrei Badea
063: */
064: public class SQLResultPanel extends javax.swing.JPanel {
065:
066: private SQLExecutionResults executionResults;
067:
068: public SQLResultPanel() {
069: initComponents();
070: }
071:
072: public void setModel(SQLResultPanelModel model) {
073: TableModel resultSetModel = null;
074:
075: if (model != null) {
076: if (model.getResultSetModel() != null) {
077: resultSetModel = model.getResultSetModel();
078: } else {
079: resultSetModel = new DefaultTableModel(0, 0);
080: }
081: } else {
082: resultSetModel = new DefaultTableModel(0, 0);
083: }
084:
085: resultTable.setModel(resultSetModel);
086: }
087:
088: private void setClipboard(String contents) {
089: ExClipboard clipboard = (ExClipboard) Lookup.getDefault()
090: .lookup(ExClipboard.class);
091: StringSelection strSel = new StringSelection(contents);
092: clipboard.setContents(strSel, strSel);
093: }
094:
095: /** This method is called from within the constructor to
096: * initialize the form.
097: * WARNING: Do NOT modify this code. The content of this method is
098: * always regenerated by the Form Editor.
099: */
100: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
101: private void initComponents() {
102:
103: tablePopupMenu = new javax.swing.JPopupMenu();
104: copyCellValueMenuItem = new javax.swing.JMenuItem();
105: copyRowValuesMenuItem = new javax.swing.JMenuItem();
106: resultScrollPane = new javax.swing.JScrollPane();
107: resultTable = new SQLResultTable();
108:
109: copyCellValueMenuItem.setText(org.openide.util.NbBundle
110: .getMessage(SQLResultPanel.class, "LBL_CopyCellValue")); // NOI18N
111: copyCellValueMenuItem
112: .addActionListener(new java.awt.event.ActionListener() {
113: public void actionPerformed(
114: java.awt.event.ActionEvent evt) {
115: copyCellValueMenuItemActionPerformed(evt);
116: }
117: });
118: tablePopupMenu.add(copyCellValueMenuItem);
119: copyCellValueMenuItem.getAccessibleContext()
120: .setAccessibleDescription(
121: org.openide.util.NbBundle.getMessage(
122: SQLResultPanel.class,
123: "ACSD_CopyCellValue")); // NOI18N
124:
125: copyRowValuesMenuItem.setText(org.openide.util.NbBundle
126: .getMessage(SQLResultPanel.class, "LBL_CopyRowValues")); // NOI18N
127: copyRowValuesMenuItem
128: .addActionListener(new java.awt.event.ActionListener() {
129: public void actionPerformed(
130: java.awt.event.ActionEvent evt) {
131: copyRowValuesMenuItemActionPerformed(evt);
132: }
133: });
134: tablePopupMenu.add(copyRowValuesMenuItem);
135: copyRowValuesMenuItem.getAccessibleContext()
136: .setAccessibleDescription(
137: org.openide.util.NbBundle.getMessage(
138: SQLResultPanel.class,
139: "ACSD_CopyRowValues")); // NOI18N
140:
141: setLayout(new java.awt.BorderLayout());
142:
143: resultScrollPane.setBorder(javax.swing.BorderFactory
144: .createEmptyBorder(0, 0, 0, 0));
145: resultScrollPane.getViewport().setBackground(
146: UIManager.getDefaults().getColor("Table.background"));
147:
148: resultTable.setModel(new javax.swing.table.DefaultTableModel(
149: new Object[][] {
150:
151: }, new String[] {
152:
153: }));
154: resultTable
155: .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
156: resultTable.setOpaque(false);
157: resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
158: public void mouseReleased(java.awt.event.MouseEvent evt) {
159: resultTableMouseReleased(evt);
160: }
161: });
162: resultScrollPane.setViewportView(resultTable);
163:
164: add(resultScrollPane, java.awt.BorderLayout.CENTER);
165: }// </editor-fold>//GEN-END:initComponents
166:
167: private void copyRowValuesMenuItemActionPerformed(
168: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyRowValuesMenuItemActionPerformed
169: int[] rows = resultTable.getSelectedRows();
170: StringBuffer output = new StringBuffer();
171: for (int i = 0; i < rows.length; i++) {
172: for (int col = 0; col < resultTable.getColumnCount(); col++) {
173: if (col > 0) {
174: output.append('\t');
175: }
176: Object o = resultTable.getValueAt(rows[i], col);
177: output.append(o.toString());
178: }
179: output.append('\n');
180: }
181: setClipboard(output.toString());
182: }//GEN-LAST:event_copyRowValuesMenuItemActionPerformed
183:
184: private void copyCellValueMenuItemActionPerformed(
185: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyCellValueMenuItemActionPerformed
186: Object o = resultTable.getValueAt(resultTable.getSelectedRow(),
187: resultTable.getSelectedColumn());
188: setClipboard(o.toString());
189: }//GEN-LAST:event_copyCellValueMenuItemActionPerformed
190:
191: private void resultTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseReleased
192: if (evt.getButton() != MouseEvent.BUTTON3) {
193: return;
194: }
195: int row = resultTable.rowAtPoint(evt.getPoint());
196: int column = resultTable.columnAtPoint(evt.getPoint());
197: boolean inSelection = false;
198: int[] rows = resultTable.getSelectedRows();
199: for (int i = 0; i < rows.length; i++) {
200: if (rows[i] == row) {
201: inSelection = true;
202: break;
203: }
204: }
205: if (!inSelection) {
206: resultTable.changeSelection(row, column, false, false);
207: }
208: tablePopupMenu.show(resultTable, evt.getX(), evt.getY());
209: }//GEN-LAST:event_resultTableMouseReleased
210:
211: // Variables declaration - do not modify//GEN-BEGIN:variables
212: private javax.swing.JMenuItem copyCellValueMenuItem;
213: private javax.swing.JMenuItem copyRowValuesMenuItem;
214: private javax.swing.JScrollPane resultScrollPane;
215: private javax.swing.JTable resultTable;
216: private javax.swing.JPopupMenu tablePopupMenu;
217:
218: // End of variables declaration//GEN-END:variables
219:
220: static final class SQLResultTable extends JTable {
221:
222: private final NullRenderer nullRenderer = new NullRenderer();
223:
224: public SQLResultTable() {
225: setDefaultRenderer(String.class, new StringRenderer());
226: // issue 70521: rendering java.sql.Timestamp as date and time
227: setDefaultRenderer(Timestamp.class, new DateTimeRenderer());
228: // issue 72607: rendering java.sql.Time as time
229: setDefaultRenderer(Time.class, new TimeRenderer());
230: }
231:
232: /**
233: * Overrinding in order to provide a valid renderer for NullValue.
234: * NullValue can appear in any column and causes formatting exceptions.
235: * See issue 62622.
236: */
237: public TableCellRenderer getCellRenderer(int row, int column) {
238: Object value = getValueAt(row, column);
239: if (value instanceof NullValue) {
240: return nullRenderer;
241: } else {
242: return super .getCellRenderer(row, column);
243: }
244: }
245:
246: /**
247: * Renderer which renders both the date and time part of a Date.
248: */
249: private static final class DateTimeRenderer extends
250: DefaultTableCellRenderer.UIResource {
251:
252: DateFormat formatter;
253:
254: public DateTimeRenderer() {
255: super ();
256: }
257:
258: public void setValue(Object value) {
259: if (formatter == null) {
260: formatter = DateFormat.getDateTimeInstance();
261: }
262: setText((value == null) ? "" : formatter.format(value)); // NOI18N
263: }
264: }
265:
266: private static final class TimeRenderer extends
267: DefaultTableCellRenderer.UIResource {
268:
269: DateFormat formatter;
270:
271: public TimeRenderer() {
272: super ();
273: }
274:
275: public void setValue(Object value) {
276: if (formatter == null) {
277: formatter = DateFormat.getTimeInstance();
278: }
279: setText((value == null) ? "" : formatter.format(value)); // NOI18N
280: }
281: }
282:
283: private static final class NullRenderer extends
284: DefaultTableCellRenderer.UIResource {
285:
286: public void setValue(Object value) {
287: setText(((NullValue) value).toString());
288: }
289: }
290:
291: private static final class StringRenderer extends
292: DefaultTableCellRenderer.UIResource {
293: public StringRenderer() {
294: super ();
295: super .putClientProperty("html.disable", Boolean.TRUE);
296: }
297: }
298: }
299: }
|