1 30 31 32 package org.hsqldb.util; 33 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 import java.util.Enumeration ; 39 import java.util.StringTokenizer ; 40 import java.util.Vector ; 41 import java.awt.BorderLayout ; 42 import java.awt.Button ; 43 import java.awt.CardLayout ; 44 import java.awt.Checkbox ; 45 import java.awt.CheckboxGroup ; 46 import java.awt.Choice ; 47 import java.awt.Font ; 48 import java.awt.GridBagConstraints ; 49 import java.awt.GridBagLayout ; 50 import java.awt.GridLayout ; 51 import java.awt.Insets ; 52 import java.awt.Label ; 53 import java.awt.Panel ; 54 import java.awt.TextField ; 55 import java.awt.event.ActionEvent ; 56 import java.awt.event.ActionListener ; 57 58 101 public class ZaurusEditor extends Panel implements ActionListener { 102 103 static TextField tStatus; 106 107 Connection cConn; 110 DatabaseMetaData dbmeta; 111 112 Button bSearchRow, bNewRow; 114 Button bCancel1, bPrev, bNext, bDelete, bNewSearch; 115 Button bCancel2, bNewInsert, bNewSearch1; 116 117 Panel pSearchButs, pEditButs, pInsertButs; 119 120 Vector vHoldTableNames; 122 123 Vector vHoldForms; 125 126 int aktHoldNr; 128 129 Panel pForm, pButton; 131 CardLayout lForm, lButton; 132 147 148 TextField fSearchWords; 150 151 Choice cTables; 153 154 CheckboxGroup gAllWords; 156 157 CheckboxGroup gIgnoreCase; 159 160 CheckboxGroup gNoMatchWhole; 162 163 boolean lastButtonDelete; 165 166 171 public static void printStatus(String text) { 172 tStatus.setText(text); 173 } 174 175 179 public static void clearStatus() { 180 tStatus.setText(""); 181 } 182 183 187 public ZaurusEditor() { 188 189 super(); 190 191 initGUI(); 192 } 193 194 226 public void actionPerformed(ActionEvent e) { 227 228 Button button = (Button ) e.getSource(); 229 230 if (button == bSearchRow) { 231 this.resetLastButtonDelete(); 232 233 aktHoldNr = getChoosenTableIndex(); 235 236 int numberOfRows = 238 ((ZaurusTableForm) vHoldForms.elementAt(aktHoldNr)) 239 .searchRows(this 240 .getWords(), (gAllWords.getSelectedCheckbox() 241 .getLabel().equals("all")), (gIgnoreCase 242 .getSelectedCheckbox().getLabel() 243 .equals("yes")), (gNoMatchWhole 244 .getSelectedCheckbox().getLabel().equals("no"))); 245 String tableName = (String ) vHoldTableNames.elementAt(aktHoldNr); 246 247 if (numberOfRows > 0) { 248 lForm.show(pForm, tableName); 249 lButton.show(pButton, "edit"); 250 bPrev.setEnabled(false); 251 252 bNext.setEnabled(numberOfRows != 1); 254 ZaurusEditor.printStatus("found " + numberOfRows 255 + " rows in table " + tableName); 256 } else if (numberOfRows == 0) { 257 ZaurusEditor.printStatus("no rows found in table " 258 + tableName); 259 260 } } else if ((button == bNewRow)) { 263 264 aktHoldNr = getChoosenTableIndex(); 266 267 lForm.show(pForm, (String ) vHoldTableNames.elementAt(aktHoldNr)); 268 lButton.show(pButton, "insert"); 269 ((ZaurusTableForm) vHoldForms.elementAt( 270 aktHoldNr)).insertNewRow(); 271 } else if (button == bNewInsert) { 272 this.resetLastButtonDelete(); 273 274 if (((ZaurusTableForm) vHoldForms.elementAt( 276 aktHoldNr)).saveNewRow()) { 277 278 ((ZaurusTableForm) vHoldForms.elementAt( 280 aktHoldNr)).insertNewRow(); 281 } 282 } else if (button == bNewSearch) { 283 this.resetLastButtonDelete(); 284 285 if (((ZaurusTableForm) vHoldForms.elementAt( 287 aktHoldNr)).saveChanges()) { 288 289 lForm.show(pForm, "search"); 291 lButton.show(pButton, "search"); 292 } 293 } else if (button == bNewSearch1) { 294 this.resetLastButtonDelete(); 295 296 if (((ZaurusTableForm) vHoldForms.elementAt( 298 aktHoldNr)).saveNewRow()) { 299 300 lForm.show(pForm, "search"); 302 lButton.show(pButton, "search"); 303 } 304 } else if ((button == bNext)) { 305 this.resetLastButtonDelete(); 306 ZaurusEditor.clearStatus(); 307 308 if (((ZaurusTableForm) vHoldForms.elementAt( 309 aktHoldNr)).saveChanges()) { 310 bPrev.setEnabled(true); 311 312 if (!((ZaurusTableForm) vHoldForms.elementAt( 313 aktHoldNr)).nextRow()) { 314 bNext.setEnabled(false); 315 } 316 } 317 } else if ((button == bPrev)) { 318 this.resetLastButtonDelete(); 319 ZaurusEditor.clearStatus(); 320 321 if (((ZaurusTableForm) vHoldForms.elementAt( 322 aktHoldNr)).saveChanges()) { 323 bNext.setEnabled(true); 324 325 if (!((ZaurusTableForm) vHoldForms.elementAt( 326 aktHoldNr)).prevRow()) { 327 bPrev.setEnabled(false); 328 } 329 } 330 } else if ((button == bCancel1)) { 331 332 this.resetLastButtonDelete(); 334 ((ZaurusTableForm) vHoldForms.elementAt( 335 aktHoldNr)).cancelChanges(); 336 } else if ((button == bCancel2)) { 337 this.resetLastButtonDelete(); 338 339 lForm.show(pForm, "search"); 341 lButton.show(pButton, "search"); 342 } else if (button == bDelete) { 343 if (lastButtonDelete) { 344 345 switch (((ZaurusTableForm) vHoldForms.elementAt( 347 aktHoldNr)).deleteRow()) { 348 349 case 1 : 350 lForm.show(pForm, "search"); 351 lButton.show(pButton, "search"); 352 break; 353 354 case 2 : 355 bPrev.setEnabled(false); 356 break; 357 358 case 3 : 359 bNext.setEnabled(false); 360 break; 361 362 default : 363 break; 364 } 366 lastButtonDelete = false; 367 } else { 368 ZaurusEditor.printStatus( 369 "Press 'Delete' a second time to delete row."); 370 371 lastButtonDelete = true; 372 } } } 375 376 378 389 public void refresh(Connection c) { 390 391 cConn = c; 392 393 if (vHoldForms == null) { 394 this.initGUI(); 395 396 } else { 398 this.resetTableForms(); 399 400 } 402 } 403 404 private void initGUI() { 405 406 Vector vAllTables = getAllTables(); 410 411 if (vAllTables == null) { 412 return; 413 } 414 415 vHoldTableNames = new Vector (20); 417 vHoldForms = new Vector (20); 418 419 this.setLayout(new BorderLayout (3, 3)); 424 425 Panel pFormButs = new Panel (); 429 430 pFormButs.setLayout(new BorderLayout (3, 3)); 431 432 pForm = new Panel (); 433 lForm = new CardLayout (2, 2); 434 435 pForm.setLayout(lForm); 436 437 Panel pEntry = new Panel (); 440 441 pEntry.setLayout(new GridBagLayout ()); 442 443 GridBagConstraints c = new GridBagConstraints (); 444 445 c.fill = GridBagConstraints.HORIZONTAL; 446 c.insets = new Insets (3, 3, 3, 3); 447 c.gridwidth = 1; 448 c.gridheight = 1; 449 c.weightx = c.weighty = 1; 450 c.anchor = GridBagConstraints.WEST; 451 c.gridy = 0; 452 c.gridx = 0; 453 454 pEntry.add(new Label ("Search table"), c); 455 456 c.gridx = 1; 457 458 cTables = new Choice (); 460 461 for (Enumeration e = vAllTables.elements(); e.hasMoreElements(); ) { 462 cTables.addItem((String ) e.nextElement()); 463 } 464 465 c.gridwidth = 2; 466 467 pEntry.add(cTables, c); 468 469 c.gridy = 1; 470 c.gridx = 0; 471 c.gridwidth = 1; 472 473 pEntry.add(new Label ("Search words"), c); 474 475 c.gridx = 1; 476 c.gridwidth = 2; 477 fSearchWords = new TextField (8); 478 479 pEntry.add(fSearchWords, c); 480 481 c.gridwidth = 1; 483 c.gridy = 2; 484 c.gridx = 0; 485 486 pEntry.add(new Label ("Use search words"), c); 487 488 gAllWords = new CheckboxGroup (); 489 490 Checkbox [] checkboxes = new Checkbox [2]; 491 492 checkboxes[0] = new Checkbox ("all", gAllWords, true); 493 c.gridx = 1; 494 495 pEntry.add(checkboxes[0], c); 496 497 checkboxes[1] = new Checkbox ("any ", gAllWords, false); 498 c.gridx = 2; 499 500 pEntry.add(checkboxes[1], c); 501 502 c.gridy = 3; 504 c.gridx = 0; 505 506 pEntry.add(new Label ("Ignore case"), c); 507 508 gIgnoreCase = new CheckboxGroup (); 509 510 Checkbox [] checkboxes1 = new Checkbox [2]; 511 512 checkboxes1[0] = new Checkbox ("yes", gIgnoreCase, true); 513 c.gridx = 1; 514 515 pEntry.add(checkboxes1[0], c); 516 517 checkboxes1[1] = new Checkbox ("no", gIgnoreCase, false); 518 c.gridx = 2; 519 520 pEntry.add(checkboxes1[1], c); 521 522 c.gridy = 4; 524 c.gridx = 0; 525 526 pEntry.add(new Label ("Match whole col"), c); 527 528 gNoMatchWhole = new CheckboxGroup (); 529 530 Checkbox [] checkboxes2 = new Checkbox [2]; 531 532 checkboxes2[0] = new Checkbox ("no", gNoMatchWhole, true); 533 c.gridx = 1; 534 535 pEntry.add(checkboxes2[0], c); 536 537 checkboxes2[1] = new Checkbox ("yes ", gNoMatchWhole, false); 538 c.gridx = 2; 539 540 pEntry.add(checkboxes2[1], c); 541 pForm.add("search", pEntry); 542 pFormButs.add("Center", pForm); 543 544 this.initButtons(); 546 547 pButton = new Panel (); 548 lButton = new CardLayout (2, 2); 549 550 pButton.setLayout(lButton); 551 pButton.add("search", pSearchButs); 552 pButton.add("edit", pEditButs); 553 pButton.add("insert", pInsertButs); 554 pFormButs.add("South", pButton); 555 this.add("Center", pFormButs); 556 557 Font fFont = new Font ("Dialog", Font.PLAIN, 10); 559 560 ZaurusEditor.tStatus = new TextField (""); 561 562 ZaurusEditor.tStatus.setEditable(false); 563 this.add("South", ZaurusEditor.tStatus); 564 } 565 566 private Vector getAllTables() { 573 574 Vector result = new Vector (20); 575 576 try { 577 if (cConn == null) { 578 return null; 579 } 580 581 dbmeta = cConn.getMetaData(); 582 583 String [] tableTypes = { "TABLE" }; 584 ResultSet allTables = dbmeta.getTables(null, null, null, 585 tableTypes); 586 587 while (allTables.next()) { 588 String aktTable = allTables.getString("TABLE_NAME"); 589 ResultSet primKeys = dbmeta.getPrimaryKeys(null, null, 590 aktTable); 591 592 if (primKeys.next()) { 594 result.addElement(aktTable); 595 } 596 597 primKeys.close(); 598 } 599 600 allTables.close(); 601 } catch (SQLException e) { 602 603 } 605 606 return result; 607 } 608 609 private int getChoosenTableIndex() { 612 613 String tableName = cTables.getSelectedItem(); 614 615 int index = getTableIndex(tableName); 617 618 if (index >= 0) { 619 620 return index; 622 } 624 ZaurusTableForm tableForm = new ZaurusTableForm(tableName, cConn); 625 626 pForm.add(tableName, tableForm); 627 vHoldTableNames.addElement(tableName); 628 vHoldForms.addElement(tableForm); 629 630 return vHoldTableNames.size() - 1; 632 } 633 634 private int getTableIndex(String tableName) { 637 638 int index; 639 640 for (index = 0; index < vHoldTableNames.size(); index++) { 642 643 if (tableName.equals((String ) vHoldTableNames.elementAt(index))) { 645 return index; 646 } } 649 return -1; 650 } 651 652 private String [] getWords() { 654 655 StringTokenizer tokenizer = 656 new StringTokenizer (fSearchWords.getText()); 657 String [] result = new String [tokenizer.countTokens()]; 658 int i = 0; 659 660 while (tokenizer.hasMoreTokens()) { 661 result[i++] = tokenizer.nextToken(); 662 } 664 return result; 665 } 666 667 private void initButtons() { 669 670 bSearchRow = new Button ("Search Rows"); 672 bNewRow = new Button ("Insert New Row"); 673 674 bSearchRow.addActionListener(this); 675 bNewRow.addActionListener(this); 676 677 pSearchButs = new Panel (); 678 679 pSearchButs.setLayout(new GridLayout (1, 0, 4, 4)); 680 pSearchButs.add(bSearchRow); 681 pSearchButs.add(bNewRow); 682 683 bCancel1 = new Button ("Cancel"); 685 bPrev = new Button ("Prev"); 686 bNext = new Button ("Next"); 687 bDelete = new Button ("Delete"); 688 lastButtonDelete = false; 689 bNewSearch = new Button ("Search"); 690 691 bCancel1.addActionListener(this); 692 bPrev.addActionListener(this); 693 bNext.addActionListener(this); 694 bDelete.addActionListener(this); 695 bNewSearch.addActionListener(this); 696 697 pEditButs = new Panel (); 698 699 pEditButs.setLayout(new GridLayout (1, 0, 4, 4)); 700 pEditButs.add(bCancel1); 701 pEditButs.add(bPrev); 702 pEditButs.add(bNext); 703 pEditButs.add(bDelete); 704 pEditButs.add(bNewSearch); 705 706 pInsertButs = new Panel (); 708 709 pInsertButs.setLayout(new GridLayout (1, 0, 4, 4)); 710 711 bCancel2 = new Button ("Cancel Insert"); 712 bNewInsert = new Button ("New Insert"); 713 bNewSearch1 = new Button ("Search"); 714 715 bCancel2.addActionListener(this); 716 bNewInsert.addActionListener(this); 717 bNewSearch1.addActionListener(this); 718 pInsertButs.add(bCancel2); 719 pInsertButs.add(bNewInsert); 720 pInsertButs.add(bNewSearch1); 721 } 722 723 private void resetLastButtonDelete() { 726 727 if (lastButtonDelete) { 728 ZaurusEditor.printStatus(""); 729 730 lastButtonDelete = false; 731 } } 733 734 private void resetTableForms() { 736 737 lForm.show(pForm, "search"); 738 lButton.show(pButton, "search"); 739 740 Vector vAllTables = getAllTables(); 741 742 cTables.removeAll(); 745 746 for (Enumeration e = vAllTables.elements(); e.hasMoreElements(); ) { 747 cTables.addItem((String ) e.nextElement()); 748 } 749 750 for (Enumeration e = vHoldForms.elements(); e.hasMoreElements(); ) { 752 pForm.remove((ZaurusTableForm) e.nextElement()); 753 } 755 vHoldTableNames = new Vector (20); 757 vHoldForms = new Vector (20); 758 } 759 } 760 | Popular Tags |