1 19 20 package org.netbeans.modules.db.sql.execute.ui; 21 22 import java.awt.datatransfer.StringSelection ; 23 import java.awt.event.MouseEvent ; 24 import java.sql.Time ; 25 import java.sql.Timestamp ; 26 import java.text.DateFormat ; 27 import javax.swing.JTable ; 28 import javax.swing.UIManager ; 29 import javax.swing.table.DefaultTableCellRenderer ; 30 import javax.swing.table.DefaultTableModel ; 31 import javax.swing.table.TableCellRenderer ; 32 import javax.swing.table.TableModel ; 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 42 public class SQLResultPanel extends javax.swing.JPanel { 43 44 private SQLExecutionResults executionResults; 45 46 public SQLResultPanel() { 47 initComponents(); 48 } 49 50 public void setModel(SQLResultPanelModel model) { 51 TableModel resultSetModel = null; 52 53 if (model != null) { 54 if (model.getResultSetModel() != null) { 55 resultSetModel = model.getResultSetModel(); 56 } else { 57 resultSetModel = new DefaultTableModel (0, 0); 58 } 59 } else { 60 resultSetModel = new DefaultTableModel (0, 0); 61 } 62 63 resultTable.setModel(resultSetModel); 64 } 65 66 private void setClipboard(String contents) { 67 ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup (ExClipboard.class); 68 StringSelection strSel = new StringSelection (contents); 69 clipboard.setContents(strSel, strSel); 70 } 71 72 77 private void initComponents() { 79 tablePopupMenu = new javax.swing.JPopupMenu (); 80 copyCellValueMenuItem = new javax.swing.JMenuItem (); 81 copyRowValuesMenuItem = new javax.swing.JMenuItem (); 82 resultScrollPane = new javax.swing.JScrollPane (); 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 () { 87 public void actionPerformed(java.awt.event.ActionEvent 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 () { 97 public void actionPerformed(java.awt.event.ActionEvent 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 ()); 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 ( 110 new Object [][] { 111 112 }, 113 new String [] { 114 115 } 116 )); 117 resultTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); 118 resultTable.setOpaque(false); 119 resultTable.addMouseListener(new java.awt.event.MouseAdapter () { 120 public void mouseReleased(java.awt.event.MouseEvent evt) { 121 resultTableMouseReleased(evt); 122 } 123 }); 124 125 resultScrollPane.setViewportView(resultTable); 126 127 add(resultScrollPane, java.awt.BorderLayout.CENTER); 128 129 } 131 private void copyRowValuesMenuItemActionPerformed(java.awt.event.ActionEvent evt) { int[] rows = resultTable.getSelectedRows(); 133 StringBuffer output = new StringBuffer (); 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 o = resultTable.getValueAt(rows[i], col); 140 output.append(o.toString()); 141 } 142 output.append('\n'); 143 } 144 setClipboard(output.toString()); 145 } 147 private void copyCellValueMenuItemActionPerformed(java.awt.event.ActionEvent evt) { Object o = resultTable.getValueAt(resultTable.getSelectedRow(), resultTable.getSelectedColumn()); 149 setClipboard(o.toString()); 150 } 152 private void resultTableMouseReleased(java.awt.event.MouseEvent evt) { 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 } 172 private javax.swing.JMenuItem copyCellValueMenuItem; 174 private javax.swing.JMenuItem copyRowValuesMenuItem; 175 private javax.swing.JScrollPane resultScrollPane; 176 private javax.swing.JTable resultTable; 177 private javax.swing.JPopupMenu tablePopupMenu; 178 180 static final class SQLResultTable extends JTable { 181 182 public SQLResultTable() { 183 setDefaultRenderer(Timestamp .class, new DateTimeRenderer()); 185 setDefaultRenderer(Time .class, new TimeRenderer()); 187 } 188 189 194 public TableCellRenderer getCellRenderer(int row, int column) { 195 Object value = getValueAt(row, column); 196 if (value instanceof NullValue) { 197 return getDefaultRenderer(Object .class); 198 } else { 199 return super.getCellRenderer(row, column); 200 } 201 } 202 203 206 private static final class DateTimeRenderer extends DefaultTableCellRenderer.UIResource { 207 208 DateFormat formatter; 209 210 public DateTimeRenderer() { 211 super(); 212 } 213 214 public void setValue(Object value) { 215 if (formatter == null) { 216 formatter = DateFormat.getDateTimeInstance(); 217 } 218 setText((value == null) ? "" : formatter.format(value)); } 220 } 221 222 private static final class TimeRenderer extends DefaultTableCellRenderer.UIResource { 223 224 DateFormat formatter; 225 226 public TimeRenderer() { 227 super(); 228 } 229 230 public void setValue(Object value) { 231 if (formatter == null) { 232 formatter = DateFormat.getTimeInstance(); 233 } 234 setText((value == null) ? "" : formatter.format(value)); } 236 } 237 } 238 } | Popular Tags |