KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.datatransfer.StringSelection JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.sql.Time JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import javax.swing.JTable JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
30 import javax.swing.table.DefaultTableModel JavaDoc;
31 import javax.swing.table.TableCellRenderer JavaDoc;
32 import javax.swing.table.TableModel JavaDoc;
33 import org.netbeans.modules.db.sql.execute.NullValue;
34 import org.openide.util.Lookup;
35 import org.netbeans.modules.db.sql.execute.SQLExecutionResults;
36 import org.openide.util.datatransfer.ExClipboard;
37
38 /**
39  *
40  * @author Andrei Badea
41  */

42 public class SQLResultPanel extends javax.swing.JPanel JavaDoc {
43     
44     private SQLExecutionResults executionResults;
45     
46     public SQLResultPanel() {
47         initComponents();
48     }
49     
50     public void setModel(SQLResultPanelModel model) {
51         TableModel JavaDoc resultSetModel = null;
52         
53         if (model != null) {
54             if (model.getResultSetModel() != null) {
55                 resultSetModel = model.getResultSetModel();
56             } else {
57                 resultSetModel = new DefaultTableModel JavaDoc(0, 0);
58             }
59         } else {
60             resultSetModel = new DefaultTableModel JavaDoc(0, 0);
61         }
62         
63         resultTable.setModel(resultSetModel);
64     }
65     
66     private void setClipboard(String JavaDoc contents) {
67         ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup (ExClipboard.class);
68         StringSelection JavaDoc strSel = new StringSelection JavaDoc(contents);
69         clipboard.setContents(strSel, strSel);
70     }
71     
72     /** This method is called from within the constructor to
73      * initialize the form.
74      * WARNING: Do NOT modify this code. The content of this method is
75      * always regenerated by the Form Editor.
76      */

77     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
78
private void initComponents() {
79         tablePopupMenu = new javax.swing.JPopupMenu JavaDoc();
80         copyCellValueMenuItem = new javax.swing.JMenuItem JavaDoc();
81         copyRowValuesMenuItem = new javax.swing.JMenuItem JavaDoc();
82         resultScrollPane = new javax.swing.JScrollPane JavaDoc();
83         resultTable = new SQLResultTable();
84
85         copyCellValueMenuItem.setText(org.openide.util.NbBundle.getMessage(SQLResultPanel.class, "LBL_CopyCellValue"));
86         copyCellValueMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
87             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
88                 copyCellValueMenuItemActionPerformed(evt);
89             }
90         });
91
92         tablePopupMenu.add(copyCellValueMenuItem);
93         copyCellValueMenuItem.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLResultPanel.class, "ACSD_CopyCellValue"));
94
95         copyRowValuesMenuItem.setText(org.openide.util.NbBundle.getMessage(SQLResultPanel.class, "LBL_CopyRowValues"));
96         copyRowValuesMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
97             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
98                 copyRowValuesMenuItemActionPerformed(evt);
99             }
100         });
101
102         tablePopupMenu.add(copyRowValuesMenuItem);
103         copyRowValuesMenuItem.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLResultPanel.class, "ACSD_CopyRowValues"));
104
105         setLayout(new java.awt.BorderLayout JavaDoc());
106
107         resultScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
108         resultScrollPane.getViewport().setBackground(UIManager.getDefaults().getColor("Table.background"));
109         resultTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
110             new Object JavaDoc [][] {
111
112             },
113             new String JavaDoc [] {
114
115             }
116         ));
117         resultTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
118         resultTable.setOpaque(false);
119         resultTable.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
120             public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
121                 resultTableMouseReleased(evt);
122             }
123         });
124
125         resultScrollPane.setViewportView(resultTable);
126
127         add(resultScrollPane, java.awt.BorderLayout.CENTER);
128
129     }// </editor-fold>//GEN-END:initComponents
130

131     private void copyRowValuesMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_copyRowValuesMenuItemActionPerformed
132
int[] rows = resultTable.getSelectedRows();
133         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
134         for (int i = 0; i < rows.length; i++) {
135             for (int col = 0; col < resultTable.getColumnCount(); col++) {
136                 if (col > 0) {
137                     output.append('\t');
138                 }
139                 Object JavaDoc o = resultTable.getValueAt(rows[i], col);
140                 output.append(o.toString());
141             }
142             output.append('\n');
143         }
144         setClipboard(output.toString());
145     }//GEN-LAST:event_copyRowValuesMenuItemActionPerformed
146

147     private void copyCellValueMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_copyCellValueMenuItemActionPerformed
148
Object JavaDoc o = resultTable.getValueAt(resultTable.getSelectedRow(), resultTable.getSelectedColumn());
149         setClipboard(o.toString());
150     }//GEN-LAST:event_copyCellValueMenuItemActionPerformed
151

152     private void resultTableMouseReleased(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_resultTableMouseReleased
153
if (evt.getButton() != MouseEvent.BUTTON3) {
154             return;
155         }
156         int row = resultTable.rowAtPoint(evt.getPoint());
157         int column = resultTable.columnAtPoint(evt.getPoint());
158         boolean inSelection = false;
159         int[] rows = resultTable.getSelectedRows();
160         for (int i = 0; i < rows.length; i++) {
161             if (rows[i] == row) {
162                 inSelection = true;
163                 break;
164             }
165         }
166         if (!inSelection) {
167             resultTable.changeSelection (row, column, false, false);
168         }
169         tablePopupMenu.show(resultTable, evt.getX(), evt.getY());
170     }//GEN-LAST:event_resultTableMouseReleased
171

172     // Variables declaration - do not modify//GEN-BEGIN:variables
173
private javax.swing.JMenuItem JavaDoc copyCellValueMenuItem;
174     private javax.swing.JMenuItem JavaDoc copyRowValuesMenuItem;
175     private javax.swing.JScrollPane JavaDoc resultScrollPane;
176     private javax.swing.JTable JavaDoc resultTable;
177     private javax.swing.JPopupMenu JavaDoc tablePopupMenu;
178     // End of variables declaration//GEN-END:variables
179

180     static final class SQLResultTable extends JTable JavaDoc {
181         
182         public SQLResultTable() {
183             // issue 70521: rendering java.sql.Timestamp as date and time
184
setDefaultRenderer(Timestamp JavaDoc.class, new DateTimeRenderer());
185             // issue 72607: rendering java.sql.Time as time
186
setDefaultRenderer(Time JavaDoc.class, new TimeRenderer());
187         }
188         
189         /**
190          * Overrinding in order to provide a valid renderer for NullValue.
191          * NullValue can appear in any column and causes formatting exceptions.
192          * See issue 62622.
193          */

194         public TableCellRenderer JavaDoc getCellRenderer(int row, int column) {
195             Object JavaDoc value = getValueAt(row, column);
196             if (value instanceof NullValue) {
197                 return getDefaultRenderer(Object JavaDoc.class);
198             } else {
199                 return super.getCellRenderer(row, column);
200             }
201         }
202         
203         /**
204          * Renderer which renders both the date and time part of a Date.
205          */

206         private static final class DateTimeRenderer extends DefaultTableCellRenderer.UIResource JavaDoc {
207             
208             DateFormat JavaDoc formatter;
209             
210             public DateTimeRenderer() {
211                 super();
212             }
213
214             public void setValue(Object JavaDoc value) {
215                 if (formatter == null) {
216                     formatter = DateFormat.getDateTimeInstance();
217                 }
218                 setText((value == null) ? "" : formatter.format(value)); // NOI18N
219
}
220         }
221         
222         private static final class TimeRenderer extends DefaultTableCellRenderer.UIResource JavaDoc {
223             
224             DateFormat JavaDoc formatter;
225             
226             public TimeRenderer() {
227                 super();
228             }
229             
230             public void setValue(Object JavaDoc value) {
231                 if (formatter == null) {
232                     formatter = DateFormat.getTimeInstance();
233                 }
234                 setText((value == null) ? "" : formatter.format(value)); // NOI18N
235
}
236         }
237     }
238 }
Popular Tags