1 19 20 package org.netbeans.modules.db.explorer.dataview; 21 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.datatransfer.DataFlavor ; 26 import java.awt.datatransfer.StringSelection ; 27 import java.awt.datatransfer.Transferable ; 28 import java.awt.dnd.DropTarget ; 29 import java.awt.dnd.DropTargetDragEvent ; 30 import java.awt.dnd.DropTargetDropEvent ; 31 import java.awt.dnd.DropTargetEvent ; 32 import java.awt.dnd.DropTargetListener ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.MouseAdapter ; 36 import java.awt.event.MouseEvent ; 37 import java.io.ObjectStreamException ; 38 import java.sql.Connection ; 39 import java.sql.ResultSet ; 40 import java.sql.ResultSetMetaData ; 41 import java.sql.SQLException ; 42 import java.sql.Statement ; 43 import java.sql.Types ; 44 import java.text.MessageFormat ; 45 import java.util.Enumeration ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.ResourceBundle ; 49 import java.util.StringTokenizer ; 50 import java.util.Vector ; 51 import javax.swing.AbstractListModel ; 52 import javax.swing.JButton ; 53 import javax.swing.JComboBox ; 54 import javax.swing.JLabel ; 55 import javax.swing.JMenuItem ; 56 import javax.swing.JPanel ; 57 import javax.swing.JPopupMenu ; 58 import javax.swing.JScrollPane ; 59 import javax.swing.JSplitPane ; 60 import javax.swing.JTable ; 61 import javax.swing.JTextArea ; 62 import javax.swing.MutableComboBoxModel ; 63 import javax.swing.SwingUtilities ; 64 import javax.swing.table.AbstractTableModel ; 65 import org.netbeans.api.db.explorer.DatabaseException; 66 import org.netbeans.modules.db.explorer.infos.ColumnNodeInfo; 67 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; 68 import org.netbeans.modules.db.explorer.nodes.ConnectionNode; 69 import org.openide.DialogDisplayer; 70 import org.openide.NotifyDescriptor; 71 import org.openide.awt.Mnemonics; 72 import org.openide.nodes.Node; 73 import org.openide.nodes.NodeTransfer; 74 import org.openide.util.Lookup; 75 import org.openide.util.NbBundle; 76 import org.openide.util.RequestProcessor; 77 import org.openide.util.Task; 78 import org.openide.util.TaskListener; 79 import org.openide.util.datatransfer.ExClipboard; 80 import org.openide.util.datatransfer.ExTransferable; 81 import org.openide.util.datatransfer.MultiTransferObject; 82 import org.openide.windows.TopComponent; 83 84 public class DataViewWindow extends TopComponent { 85 86 88 private JTextArea queryarea; 89 private JTable jtable; 90 private DataModel dbadaptor; 91 private JComboBox rcmdscombo; 92 private JLabel status; 93 private ResourceBundle bundle; 94 private Node node; 95 private JPopupMenu tablePopupMenu; 96 static final long serialVersionUID = 6855188441469780252L; 97 98 public DataViewWindow(DatabaseNodeInfo info, String query) throws SQLException { 99 node = info.getNode(); 100 101 bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); 103 this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewWindowA11yDesc")); 105 Node tempNode = node; 106 while(!(tempNode instanceof ConnectionNode)) 107 tempNode = tempNode.getParentNode(); 108 109 String title = tempNode.getDisplayName(); 110 int idx = title.indexOf(" ["); title = title.substring(0, idx); 112 setName(title); 113 setToolTipText(bundle.getString("CommandEditorTitle") + " " + tempNode.getDisplayName()); 115 GridBagLayout layout = new GridBagLayout (); 116 GridBagConstraints con = new GridBagConstraints (); 117 setLayout (layout); 118 119 dbadaptor = new DataModel(info); 121 122 JPanel subpane = new JPanel (); 124 GridBagLayout sublayout = new GridBagLayout (); 125 GridBagConstraints subcon = new GridBagConstraints (); 126 subpane.setLayout(sublayout); 127 128 subcon.fill = GridBagConstraints.HORIZONTAL; 130 subcon.weightx = 0.0; 131 subcon.weighty = 0.0; 132 subcon.gridx = 0; 133 subcon.gridy = 0; 134 subcon.gridwidth = 3; 135 subcon.insets = new Insets (0, 0, 5, 0); 136 subcon.anchor = GridBagConstraints.SOUTH; 137 JLabel queryLabel = new JLabel (); 138 Mnemonics.setLocalizedText(queryLabel, bundle.getString("QueryLabel")); 139 queryLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewQueryLabelA11yDesc")); 140 sublayout.setConstraints(queryLabel, subcon); 141 subpane.add(queryLabel); 142 143 subcon.fill = GridBagConstraints.BOTH; 145 subcon.weightx = 1.0; 146 subcon.weighty = 1.0; 147 subcon.gridx = 0; 148 subcon.gridwidth = 3; 149 subcon.gridy = 1; 150 queryarea = new JTextArea (query, 3, 70); 151 queryarea.setLineWrap(true); 152 queryarea.setWrapStyleWord(true); 153 queryarea.setDropTarget(new DropTarget (queryarea, new ViewDropTarget())); 154 queryarea.getAccessibleContext().setAccessibleName(bundle.getString("ACS_DataViewTextAreaA11yName")); queryarea.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewTextAreaA11yDesc")); queryarea.setToolTipText(bundle.getString("ACS_DataViewTextAreaA11yDesc")); queryLabel.setLabelFor(queryarea); 158 159 JScrollPane scrollpane = new JScrollPane (queryarea); 160 subcon.insets = new Insets (0, 0, 5, 0); 161 sublayout.setConstraints(scrollpane, subcon); 162 subpane.add(scrollpane); 163 164 subcon.fill = GridBagConstraints.HORIZONTAL; 166 subcon.weightx = 0.0; 167 subcon.weighty = 0.0; 168 subcon.gridx = 0; 169 subcon.gridy = 2; 170 subcon.gridwidth = 1; 171 subcon.insets = new Insets (0, 0, 5, 5); 172 subcon.anchor = GridBagConstraints.CENTER; 173 JLabel comboLabel = new JLabel (); 174 Mnemonics.setLocalizedText(comboLabel, bundle.getString("HistoryLabel")); comboLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewHistoryLabelA11yDesc")); sublayout.setConstraints(comboLabel, subcon); 177 subpane.add(comboLabel); 178 179 subcon.fill = GridBagConstraints.HORIZONTAL; 181 subcon.weightx = 1.0; 182 subcon.weighty = 0.0; 183 subcon.gridx = 1; 184 subcon.gridy = 2; 185 subcon.gridwidth = 1; 186 subcon.insets = new Insets (0, 0, 5, 5); 187 subcon.anchor = GridBagConstraints.SOUTH; 188 rcmdscombo = new JComboBox (new ComboModel()); 189 rcmdscombo.getAccessibleContext().setAccessibleName(bundle.getString("ACS_DataViewComboBoxA11yName")); rcmdscombo.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewComboBoxA11yDesc")); rcmdscombo.setToolTipText(bundle.getString("ACS_DataViewComboBoxA11yDesc")); comboLabel.setLabelFor(rcmdscombo); 193 sublayout.setConstraints(rcmdscombo, subcon); 194 subpane.add(rcmdscombo); 195 rcmdscombo.addActionListener(new ActionListener () { 196 public void actionPerformed(ActionEvent e) { 197 JComboBox source = (JComboBox )e.getSource(); 198 RecentCommand cmd = (RecentCommand)source.getSelectedItem(); 199 if (cmd != null) 200 setCommand(cmd.getCommand()); 201 } 202 }); 203 204 subcon.gridx = 2; 206 subcon.gridy = 2; 207 subcon.weightx = 0.0; 208 subcon.weighty = 0.0; 209 subcon.insets = new Insets (0, 0, 5, 0); 210 subcon.fill = GridBagConstraints.HORIZONTAL; 211 subcon.anchor = GridBagConstraints.SOUTH; 212 final JButton fetchbtn = new JButton (); 213 Mnemonics.setLocalizedText(fetchbtn, bundle.getString("ExecuteButton")); 214 fetchbtn.setToolTipText(bundle.getString("ACS_ExecuteButtonA11yDesc")); sublayout.setConstraints(fetchbtn, subcon); 216 subpane.add(fetchbtn); 217 fetchbtn.addActionListener(new ActionListener () { 218 public void actionPerformed(ActionEvent e) { 219 fetchbtn.setEnabled(false); 220 Task t = RequestProcessor.getDefault().create(new Runnable () { 221 public void run () { 222 executeCommand(); 223 } 224 }); 225 t.addTaskListener(new TaskListener() { 226 public void taskFinished(Task task) { 227 fetchbtn.setEnabled(true); 228 } 229 }); 230 RequestProcessor.getDefault().post(t, 0); 231 } 232 }); 233 subcon.fill = GridBagConstraints.HORIZONTAL; 235 subcon.weightx = 1.0; 236 subcon.weighty = 0.0; 237 subcon.gridx = 0; 238 subcon.gridy = 3; 239 subcon.gridwidth = 3; 240 subcon.insets = new Insets (0, 0, 5, 0); 241 subcon.anchor = GridBagConstraints.SOUTH; 242 status = new JLabel (" "); status.setBorder(new javax.swing.border.LineBorder (java.awt.Color.gray)); 244 status.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewStatusLabelA11yDesc")); sublayout.setConstraints(status, subcon); 246 subpane.add(status); 247 248 JPanel subpane2 = new JPanel (); 249 GridBagLayout sublayout2 = new GridBagLayout (); 250 GridBagConstraints subcon2 = new GridBagConstraints (); 251 subpane2.setLayout(sublayout2); 252 253 subcon2.fill = GridBagConstraints.HORIZONTAL; 255 subcon2.weightx = 0.0; 256 subcon2.weighty = 0.0; 257 subcon2.gridx = 0; 258 subcon2.gridy = 0; 259 subcon2.gridwidth = 1; 260 subcon2.insets = new Insets (5, 0, 0, 0); 261 subcon2.anchor = GridBagConstraints.SOUTH; 262 JLabel tableLabel = new JLabel (); 263 Mnemonics.setLocalizedText(tableLabel, bundle.getString("ResultsLabel")); 264 tableLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewResultsLabelA11yDesc")); sublayout2.setConstraints(tableLabel, subcon2); 266 subpane2.add(tableLabel); 267 268 tablePopupMenu = new JPopupMenu (); 270 JMenuItem miCopyValue = new JMenuItem (bundle.getString ("CopyCellValue")); miCopyValue.addActionListener(new ActionListener () { 272 public void actionPerformed (ActionEvent e) { 273 try { 274 Object o = jtable.getValueAt(jtable.getSelectedRow(), jtable.getSelectedColumn()); 275 String output = (o != null) ? o.toString () : ""; ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup (ExClipboard.class); 277 StringSelection strSel = new StringSelection (output); 278 clipboard.setContents (strSel, strSel); 279 } catch (ArrayIndexOutOfBoundsException exc) { 280 } 281 } 282 }); 283 tablePopupMenu.add (miCopyValue); 284 285 JMenuItem miCopyRowValues = new JMenuItem (bundle.getString ("CopyRowValues")); miCopyRowValues.addActionListener(new ActionListener () { 287 public void actionPerformed (ActionEvent e) { 288 try { 289 int[] rows = jtable.getSelectedRows (); 290 int[] columns; 291 if (jtable.getRowSelectionAllowed ()) { 292 columns = new int[jtable.getColumnCount ()]; 293 for (int a = 0; a < columns.length; a ++) 294 columns[a] = a; 295 } else { 296 columns = jtable.getSelectedColumns (); 297 } 298 if (rows != null && columns != null) { 299 StringBuffer output = new StringBuffer (); 300 for (int row = 0; row < rows.length; row ++) { 301 for (int column = 0; column < columns.length; column ++) { 302 if (column > 0) 303 output.append ('\t'); Object o = jtable.getValueAt(rows[row], columns[column]); 305 output.append (o != null ? o.toString () : ""); } 307 output.append ('\n'); } 309 ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup (ExClipboard.class); 310 StringSelection strSel = new StringSelection (output.toString ()); 311 clipboard.setContents (strSel, strSel); 312 } 313 } catch (ArrayIndexOutOfBoundsException exc) { 314 } 315 } 316 }); 317 tablePopupMenu.add (miCopyRowValues); 318 319 jtable = new JTable (dbadaptor); 322 jtable.getAccessibleContext().setAccessibleName(bundle.getString("ACS_DataViewTableA11yName")); jtable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewTableA11yDesc")); jtable.setToolTipText(bundle.getString("ACS_DataViewTableA11yDesc")); jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 326 jtable.addMouseListener (new MouseAdapter () { 328 public void mouseReleased (MouseEvent e) { 329 if (e.getButton() == MouseEvent.BUTTON3) { 330 int row = jtable.rowAtPoint (e.getPoint ()); 331 int column = jtable.columnAtPoint (e.getPoint ()); 332 boolean inSelection = false; 333 int[] rows = jtable.getSelectedRows (); 334 for (int a = 0; a < rows.length; a ++) 335 if (rows[a] == row) { 336 inSelection = true; 337 break; 338 } 339 if (!jtable.getRowSelectionAllowed ()) { 340 inSelection = false; 341 int[] columns = jtable.getSelectedColumns (); 342 for (int a = 0; a < columns.length; a ++) 343 if (columns[a] == column) { 344 inSelection = true; 345 break; 346 } 347 } 348 if (!inSelection) 349 jtable.changeSelection (row, column, false, false); 350 tablePopupMenu.show(jtable, e.getX (), e.getY ()); 351 } 352 } 353 }); 354 tableLabel.setLabelFor(jtable); 355 356 scrollpane = new JScrollPane (jtable); 357 subcon2.fill = GridBagConstraints.BOTH; 358 subcon2.weightx = 1.0; 359 subcon2.weighty = 1.0; 360 subcon2.gridx = 0; 361 subcon2.gridy = 1; 362 subcon2.gridwidth = 1; 363 sublayout2.setConstraints(scrollpane, subcon2); 364 subpane2.add(scrollpane); 365 366 con.weightx = 1.0; 368 con.weighty = 1.0; 369 con.fill = GridBagConstraints.BOTH; 370 con.gridx = 0; 371 con.gridwidth = 1; 372 con.gridy = 1; 373 con.insets = new Insets (12, 12, 11, 11); 374 375 JSplitPane split = new JSplitPane (JSplitPane.VERTICAL_SPLIT, subpane, subpane2); 376 layout.setConstraints(split, con); 377 add(split); 378 } 379 380 386 protected String preferredID() { 387 return getName(); 388 } 389 390 392 public int getPersistenceType() { 393 return TopComponent.PERSISTENCE_NEVER; 394 } 395 396 398 public String getCommand() { 399 return queryarea.getText(); 400 } 401 402 404 public void setCommand(String command) { 405 queryarea.setText(command); 406 } 407 408 public boolean executeCommand() { 409 String command = queryarea.getText().trim(); 410 boolean ret; 411 412 try { 413 dbadaptor.execute(command); 414 415 RecentCommand rcmd = new RecentCommand(command); 416 ((ComboModel)rcmdscombo.getModel()).addElement(rcmd); 417 ret = true; 418 } catch (Exception exc) { 419 ret = false; 420 status.setText(bundle.getString("CommandFailed")); org.openide.DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(bundle.getString("DataViewFetchErrorPrefix") + exc.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); } 423 424 return ret; 425 } 426 427 class ColDef { 428 private String name; 429 private boolean writable; 430 private boolean bric; 431 int datatype; 432 433 public ColDef(String name, boolean flag) { 434 this.name = name; 435 writable = flag; 436 } 437 438 public String getName() { 439 return name; 440 } 441 442 public void setName(String name) { 443 this.name = name; 444 } 445 446 public int getDataType() { 447 return datatype; 448 } 449 450 public void setDataType(int type) { 451 datatype = type; 452 } 453 454 public boolean isWritable() { 455 return writable; 456 } 457 458 public void setWritable(boolean flag) { 459 writable = flag; 460 } 461 462 public boolean isBestRowIdentifierColumn() { 463 return bric; 464 } 465 466 public void setBestRowIdentifierColumn(boolean flag) { 467 bric = flag; 468 } 469 } 470 471 static int tstrg = 0; 472 static int gtcmd = 0; 473 474 class RecentCommand { 475 private String command; 476 477 478 private String shortCommand; 479 480 public RecentCommand(String cmd) { 481 command = cmd; 482 shortCommand = getShortCommand(); 483 } 484 485 public String toString() { 486 return shortCommand; 487 } 488 489 public String getCommand() { 490 return command; 491 } 492 493 public boolean equals(Object obj) { 494 if (obj instanceof RecentCommand) 495 return ((RecentCommand)obj).getShortCommand().equals(shortCommand); 496 497 return super.equals(obj); 498 } 499 500 506 private String getShortCommand() { 507 StringTokenizer tokenizer = new StringTokenizer (command); 508 StringBuffer buffer = new StringBuffer (); 509 while (tokenizer.hasMoreElements()) { 510 buffer.append(tokenizer.nextElement()); 511 buffer.append(" "); 512 } 513 return buffer.toString(); 514 } 515 } 516 517 class ComboModel extends AbstractListModel implements MutableComboBoxModel { 518 Vector commands; 519 Object selected; 520 521 static final long serialVersionUID =-5831993904798984334L; 522 public ComboModel() { 523 this(new Vector (1)); 524 } 525 526 public ComboModel(Vector elems) { 527 commands = elems; 528 } 529 530 public Object getSelectedItem() { 531 return selected; 532 } 533 534 public void setSelectedItem(Object anItem) { 535 selected = anItem; 536 fireContentsChanged(this,-1,-1); 537 } 538 539 public void addElement(Object obj) { 540 if (!commands.contains(obj)) { 541 commands.add(obj); 542 fireContentsChanged(this,-1,-1); 543 } 544 } 545 546 public void removeElement(Object obj) { 547 commands.removeElement(obj); 548 fireContentsChanged(this,-1,-1); 549 } 550 551 public void insertElementAt(Object obj, int index) { 552 if (!commands.contains(obj)) { 553 commands.insertElementAt(obj, index); 554 fireContentsChanged(this,-1,-1); 555 } 556 } 557 558 public void removeElementAt(int index) { 559 commands.removeElementAt(index); 560 fireContentsChanged(this,-1,-1); 561 } 562 563 public int getSize() { 564 return commands.size(); 565 } 566 567 public Object getElementAt(int index) { 568 return commands.get(index); 569 } 570 } 571 572 class ViewDropTarget implements DropTargetListener { 573 574 public void dragEnter (DropTargetDragEvent dtde) { 575 dtde.acceptDrag(dtde.getDropAction()); 576 } 577 578 579 public void dragOver (DropTargetDragEvent dtde) { 580 } 581 582 public void dropActionChanged (DropTargetDragEvent dtde) { 583 } 584 585 586 public void dragExit (DropTargetEvent dte) { 587 } 588 589 private ColumnNodeInfo getNodeInfo(Transferable t) { 590 Node n = NodeTransfer.node(t, NodeTransfer.MOVE); 591 if (n != null) 592 return (ColumnNodeInfo)n.getCookie(ColumnNodeInfo.class); 593 594 n = NodeTransfer.node(t, NodeTransfer.COPY); 595 if (n != null) 596 return (ColumnNodeInfo)n.getCookie(ColumnNodeInfo.class); 597 598 return null; 599 } 600 601 602 public void drop (DropTargetDropEvent dtde) { 603 String query = null; 604 Transferable t = dtde.getTransferable(); 605 StringBuffer buff = new StringBuffer (); 606 607 try { 608 DataFlavor multiFlavor = new DataFlavor ( 609 "application/x-java-openide-multinode;class=org.openide.util.datatransfer.MultiTransferObject", NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("transferFlavorsMultiFlavorName"), 611 MultiTransferObject.class.getClassLoader()); 612 613 if (t.isDataFlavorSupported(multiFlavor)) { 614 MultiTransferObject mobj = (MultiTransferObject)t.getTransferData(ExTransferable.multiFlavor); 615 int count = mobj.getCount(); 616 int tabidx = 0; 617 HashMap tabidxmap = new HashMap (); 618 for (int i = 0; i < count; i++) { 619 ColumnNodeInfo nfo = getNodeInfo(mobj.getTransferableAt(i)); 620 if (nfo != null) { 621 String tablename = nfo.getTable(); 622 Integer tableidx = (Integer )tabidxmap.get(tablename); 623 if (tableidx == null) tabidxmap.put(tablename, tableidx = new Integer (tabidx++)); 624 if (buff.length()>0) buff.append(", "); buff.append("t"+tableidx+"."+nfo.getName()); } 627 } 628 629 StringBuffer frombuff = new StringBuffer (); 630 Iterator iter = tabidxmap.keySet().iterator(); 631 while (iter.hasNext()) { 632 String tab = (String )iter.next(); 633 if (frombuff.length()>0) frombuff.append(", "); frombuff.append(tab + " t"+tabidxmap.get(tab)); } 636 637 query = "select "+buff.toString()+" from "+frombuff.toString(); 639 } else { 640 ColumnNodeInfo nfo = getNodeInfo(t); 641 if (nfo != null) query = "select "+nfo.getName()+" from "+nfo.getTable(); } 643 644 if (query != null) 645 setCommand(query); 646 } catch (Exception e) { 647 e.printStackTrace(); 648 } 649 } 650 } 651 652 class DataModel extends AbstractTableModel { 653 DatabaseNodeInfo node_info; 654 Vector coldef = new Vector (); 655 Vector data = new Vector (); 656 boolean editable = false; 657 658 static final long serialVersionUID =7729426847826999963L; 659 660 661 public DataModel(DatabaseNodeInfo node_info) throws SQLException { 662 this.node_info = node_info; 663 } 664 665 668 synchronized public void execute(String command) throws Exception { 669 if (command.length() == 0) { 670 status.setText(" "); return; 672 } 673 674 status.setText(bundle.getString("CommandRunning")); 676 Connection con; 677 Statement stat; 678 try { 679 con = node_info.getConnection(); 680 stat = con.createStatement(); 681 } catch ( Exception exc ) { 682 String message = MessageFormat.format(bundle.getString("EXC_ConnectionError"), new String [] {exc.getMessage()}); throw new DatabaseException(message); 684 } 685 686 ResultSet rs; 687 688 if (command.toLowerCase().startsWith("select")) { rs = stat.executeQuery(command); 690 691 ResultSetMetaData mdata = rs.getMetaData(); 692 693 int cols = mdata.getColumnCount(); 694 Vector coldefWork = new Vector (); 701 Vector dataWork = new Vector (); 702 for(int column = 1; column <= cols; column++) { 703 boolean writable; 704 try { 705 writable = mdata.isWritable(column); 706 } catch (SQLException exc) { 707 writable = false; 709 } 710 ColDef cd = new ColDef(mdata.getColumnLabel(column), writable); 711 cd.setDataType(mdata.getColumnType(column)); 712 coldefWork.add(cd); 713 } 714 715 int rcounter = 0; 718 int limit = 100; 721 int step = 200; 722 723 String cancel = bundle.getString("DataViewCancelButton"); String nextset = bundle.getString("DataViewNextFetchButton"); String allset = bundle.getString("DataViewAllFetchButton"); 727 JButton fetchNext = new JButton (); 728 Mnemonics.setLocalizedText(fetchNext, nextset); 729 fetchNext.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewNextFetchButtonA11yDesc")); 731 JButton fetchAll = new JButton (); 732 Mnemonics.setLocalizedText(fetchAll, allset); 733 fetchAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewAllFetchButtonA11yDesc")); 735 JButton no = new JButton (); 736 Mnemonics.setLocalizedText(no, cancel); 737 no.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_DataViewCancelButtonA11yDesc")); 739 String message; 740 NotifyDescriptor ndesc; 741 while (rs.next()) { 742 Vector row = new Vector (cols); 743 for (int column = 1; column <= cols; column++) 744 row.add(rs.getObject(column)); 745 dataWork.addElement(row); 746 747 if (++rcounter >= limit) { 749 750 message = MessageFormat.format(bundle.getString("DataViewMessage"), new Object [] { 751 new Integer (rcounter), 752 new Integer (step) 753 }); ndesc = new NotifyDescriptor(message, bundle.getString("FetchDataTitle"), NotifyDescriptor.YES_NO_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE, new Object [] {fetchNext, fetchAll, no}, NotifyDescriptor.CANCEL_OPTION); 756 Object ret = DialogDisplayer.getDefault().notify(ndesc); 757 if (fetchAll.equals(ret)) { 758 limit = Integer.MAX_VALUE; 759 } else { 760 if (fetchNext.equals(ret)) { 761 limit = limit + step; 762 } else { 763 break; 766 } 767 } 768 } 769 } 770 771 final Vector assignData = dataWork; 774 final Vector assignColdef = coldefWork; 775 SwingUtilities.invokeAndWait(new Runnable (){ 776 public void run(){ 777 data = assignData; 778 coldef = assignColdef; 779 fireTableChanged(null); 780 } 781 }); 782 790 rs.close(); 791 } else { 793 if (command.toLowerCase().startsWith("delete") || command.toLowerCase().startsWith("insert") || command.toLowerCase().startsWith("update")) stat.executeUpdate(command); 795 else { 796 stat.execute(command); 797 798 while (!(node instanceof ConnectionNode)) 800 node = node.getParentNode(); 801 Enumeration nodes = node.getChildren().nodes(); 802 while (nodes.hasMoreElements()) 803 ((DatabaseNodeInfo)((Node)nodes.nextElement()).getCookie(DatabaseNodeInfo.class)).refreshChildren(); 804 } 805 } 806 status.setText(bundle.getString("CommandExecuted")); stat.close(); 808 } 809 810 813 public String getColumnName(int column) { 814 synchronized (coldef) { 815 if (column < coldef.size()) { 816 String cname = ((ColDef)coldef.elementAt(column)).getName(); 817 return cname; 818 } 819 820 return ""; } 822 } 823 824 827 public Class getColumnClass(int column) { 828 synchronized (coldef) { 829 if (column < coldef.size()) { 830 int coltype = ((ColDef)coldef.elementAt(column)).getDataType(); 831 switch (coltype) { 832 case Types.CHAR: 833 case Types.VARCHAR: 834 case Types.LONGVARCHAR: return String .class; 835 case Types.BIT: return Boolean .class; 836 case Types.TINYINT: 837 case Types.SMALLINT: 838 case Types.INTEGER: return Integer .class; 839 case Types.BIGINT: return Long .class; 840 case Types.FLOAT: 841 case Types.DOUBLE: return Double .class; 842 case Types.DATE: return java.sql.Date .class; 843 } 844 } 845 846 return Object .class; 847 } 848 } 849 850 852 public boolean isCellEditable(int row, int column) { 853 synchronized (coldef) { 854 if (!editable) 855 return false; 856 857 if (column < coldef.size()) 858 return ((ColDef)coldef.elementAt(column)).isWritable(); 859 860 return false; 861 } 862 } 863 864 866 public int getColumnCount() { 867 synchronized (coldef) { 868 return coldef.size(); 869 } 870 } 871 872 874 public int getRowCount() { 875 synchronized (data) { 876 return data.size(); 877 } 878 } 879 880 882 public Object getValueAt(int aRow, int aColumn) { 883 synchronized (data) { 884 Vector row = new Vector (); 885 if (aRow < data.size()) 886 row = (Vector ) data.elementAt(aRow); 887 if (row != null && aColumn < row.size()) 888 return row.elementAt(aColumn); 889 890 return null; 891 } 892 } 893 894 private String format(Object value, int type) { 895 if (value == null) 896 return "null"; 898 switch(type) { 899 case Types.INTEGER: 900 case Types.DOUBLE: 901 case Types.FLOAT: return value.toString(); 902 case Types.BIT: return ((Boolean )value).booleanValue() ? "1" : "0"; case Types.DATE: return value.toString(); 904 default: return "\""+value.toString()+"\""; } 906 } 907 908 public void setValueAt(Object value, int row, int column) { 909 synchronized (coldef) { 910 int enucol = 0; 911 StringBuffer where = new StringBuffer (); 912 Enumeration enu = coldef.elements(); 913 while (enu.hasMoreElements()) { 914 ColDef cd = (ColDef)enu.nextElement(); 915 if (cd.isBestRowIdentifierColumn()) { 916 String key = cd.getName(); 917 String val = format(getValueAt(row,enucol), cd.getDataType()); 918 if (where.length()>0) 919 where.append(" and "); where.append(key+" = "+val); } 922 enucol++; 923 } 924 } 925 } 926 } 927 928 protected Object writeReplace() throws ObjectStreamException { 929 return null; 930 } 931 } 932 | Popular Tags |