⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zauruseditor.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb.util;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Enumeration;import java.util.StringTokenizer;import java.util.Vector;import java.awt.BorderLayout;import java.awt.Button;import java.awt.CardLayout;import java.awt.Checkbox;import java.awt.CheckboxGroup;import java.awt.Choice;import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Label;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/** * <code>ZaurusEditor</code> implements an search/input/edit form to * search/view/update/insert table records. * * @author ulrivo@users * @version 1.0 * * <p> * Starting on a search panel, one can choose a table from the actual * database. The list contains only tables which have a primary key * defined. * * One may provide one or more words which are to be contained in the * rows of the table. * <p> * There are three options in searching these words: * <ul> * <li>Use search words: all or any * <br> * Only rows which contain <b>all</b> words in any column will be * found. Alternatively, it is sufficient when <b>any</b> of the * words is contained in one of the columns. * <br> * <li>Ignore case : * <br> * While searching, there is no difference between lower or upper case * letters, if this optioned is set to 'yes', * <br> * <li>Match whole column: * <br> * It is sufficient that the given word is just a part of any * column. For instance, the word 'ring' will be found as part of * 'Stringer'. Alternatively, the column content must match the search * word completely. * </ul><p> * After choosing a table, typing one or more search words and * choosing some search options, the button <b>Search Rows</b> initiates * the search through the table. * <br>Alternatively, the button <b>New Row</b> will open an empty input * panel for inserting a new row for the  choosen table.<br>In both cases, * a table specific panel with an entry field per column is shown.<br> * */public class ZaurusEditor extends Panel implements ActionListener {    // class variables    // status line    static TextField tStatus;    // instance variables    // connection to database - brought via the refresh method    Connection       cConn;    DatabaseMetaData dbmeta;    // buttons in Editor    Button bSearchRow, bNewRow;    Button bCancel1, bPrev, bNext, bDelete, bNewSearch;    Button bCancel2, bNewInsert, bNewSearch1;    // button boxes    Panel pSearchButs, pEditButs, pInsertButs;    // the list of used table names    Vector vHoldTableNames;    // the list of corresponding table forms    Vector vHoldForms;    // the pointer into the vHold.... for the actual table    int aktHoldNr;    //  pForm and pButton with CardLayout    Panel      pForm, pButton;    CardLayout lForm, lButton;    /*      ZaurusEditor holds two card panels:      1. pForm holds the different forms      pForm shows initially a search form to select a table and to      input some search words.      For every table which should be used in the editor, there is an own      ZaurusTableForm added to the card panel pForm      2. pButton holds the different button boxes      For the search form, there are buttons search row, new row  - all in the panel pSearchButs      For a table in the editor, there are alternatively:      a) cancel, prev, next, delete row, new search - for editing a row - in panel pEditButs      b) cancel, new insert, new search - for inserting a new row - pInsertButs     */    // text field with search words    TextField fSearchWords;    // the choice of all tables    Choice cTables;    // the checkbox group for using any/all search words    CheckboxGroup gAllWords;    // the checkbox group for ignoring the case of search words    CheckboxGroup gIgnoreCase;    // the checkbox group for  matching the whole column i. e. not using LIKE    CheckboxGroup gNoMatchWhole;    // one needs a double press of the delete button to delete a row    boolean lastButtonDelete;    /**     * <code>printStatus</code> prints a text into the status line below the panel.     *     * @param text a <code>String</code> value will be shown     */    public static void printStatus(String text) {        tStatus.setText(text);    }    /**     * The class method <code>clearStatus</code> deletes the status line.     *     */    public static void clearStatus() {        tStatus.setText("");    }    /**     * Constructor declaration     *     */    public ZaurusEditor() {        super();        initGUI();    }    /**     *  <code>actionPerformed</code> method is the main entry point     * which interprets the buttons and initiates the actions.     *     * @param e an <code>ActionEvent</code> value is been sent to ZaurusEditor as ActionListener     *     * <p>The possible events are:<ul> <li>Buttons on the <b>search     * panel:</b> <dl><dt>Search Row<dd> Starts the search of rows in     * the choosen table with the given search words and search     * options. Without any search words, all rows will be found.<br>     * If no row meets the criteria, there will be a message in the     * status line.  <dt>New Row<dd> An empty input panel for the     * choosen table is given. Any search words are     * ignored.</dl><li>Buttons on the <b>edit panel:</b><br>Any     * changes to field values on this panel will be updated to the     * table when the actual row is left, i. e. when pressing one of     * the buttons 'Prev', 'Next' or     * 'Search'. <br><dl><dt>Cancel<dd>Any changes to field contents     * are canceled and reset to the previous values.<dt>Prev<dd>Show     * the previous row which meets the search     * criteria.<dt>Next<dd>Show the next row which meets the search     * criteria.<dt>Delete<dd>This button has to be clicked twice and     * the shown row will be deleted from the table<dt>Search<dd>With     * this button a new search is initiated. Any changes made to     * field contents are saved</dl><li>Buttons on the <b>insert     * panel:</b><dl><dt>Cancel Insert<dd>After beginning to fill a     * new row, the insert may be cancelled. The search panel will be     * shown next.<dt>New Insert<dd>The new row is inserted into the     * table and a new empty insert panel is shown.<dt>New     * Search<dd>The new row is inserted into the table and the search     * panel is shown again.</ul>     */    public void actionPerformed(ActionEvent e) {        Button button = (Button) e.getSource();        if (button == bSearchRow) {            this.resetLastButtonDelete();            // System.out.println("pressed search");            aktHoldNr = getChoosenTableIndex();            // search all rows            int numberOfRows =                ((ZaurusTableForm) vHoldForms.elementAt(aktHoldNr))                    .searchRows(this                        .getWords(), (gAllWords.getSelectedCheckbox()                            .getLabel().equals("all")), (gIgnoreCase                            .getSelectedCheckbox().getLabel()                            .equals("yes")), (gNoMatchWhole                            .getSelectedCheckbox().getLabel().equals("no")));            String tableName = (String) vHoldTableNames.elementAt(aktHoldNr);            if (numberOfRows > 0) {                lForm.show(pForm, tableName);                lButton.show(pButton, "edit");                bPrev.setEnabled(false);                // if there is more than one row, enable the next button                bNext.setEnabled(numberOfRows != 1);                ZaurusEditor.printStatus("found " + numberOfRows                                         + " rows in table " + tableName);            } else if (numberOfRows == 0) {                ZaurusEditor.printStatus("no rows found in table "                                         + tableName);                // numberOfRows could be -1 as well, if an SQL exception was encountered            }        // end of if (numberOfRows > 0)        } else if ((button == bNewRow)) {            // System.out.println("pressed new");            aktHoldNr = getChoosenTableIndex();            lForm.show(pForm, (String) vHoldTableNames.elementAt(aktHoldNr));            lButton.show(pButton, "insert");            ((ZaurusTableForm) vHoldForms.elementAt(                aktHoldNr)).insertNewRow();        } else if (button == bNewInsert) {            this.resetLastButtonDelete();            // new search in edit row            if (((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).saveNewRow()) {                // System.out.println("pressed new insert");                ((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).insertNewRow();            }        } else if (button == bNewSearch) {            this.resetLastButtonDelete();            // new search in edit row            if (((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).saveChanges()) {                // System.out.println("pressed new search");                lForm.show(pForm, "search");                lButton.show(pButton, "search");            }        } else if (button == bNewSearch1) {            this.resetLastButtonDelete();            // new search in insert row, succeeds if the saving is successfull            if (((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).saveNewRow()) {                // System.out.println("pressed new search");                lForm.show(pForm, "search");                lButton.show(pButton, "search");            }        } else if ((button == bNext)) {            this.resetLastButtonDelete();            ZaurusEditor.clearStatus();            if (((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).saveChanges()) {                bPrev.setEnabled(true);                if (!((ZaurusTableForm) vHoldForms.elementAt(                        aktHoldNr)).nextRow()) {                    bNext.setEnabled(false);                }            }        } else if ((button == bPrev)) {            this.resetLastButtonDelete();            ZaurusEditor.clearStatus();            if (((ZaurusTableForm) vHoldForms.elementAt(                    aktHoldNr)).saveChanges()) {                bNext.setEnabled(true);                if (!((ZaurusTableForm) vHoldForms.elementAt(                        aktHoldNr)).prevRow()) {                    bPrev.setEnabled(false);                }            }        } else if ((button == bCancel1)) {            // cancel in edit panel            this.resetLastButtonDelete();            ((ZaurusTableForm) vHoldForms.elementAt(                aktHoldNr)).cancelChanges();        } else if ((button == bCancel2)) {            this.resetLastButtonDelete();            // cancel in insert panel, just show the search panel again            lForm.show(pForm, "search");            lButton.show(pButton, "search");        } else if (button == bDelete) {            if (lastButtonDelete) {                // delete and determine follow up actions, see comment in ZaurusTableForm.deleteRow()                switch (((ZaurusTableForm) vHoldForms.elementAt(                        aktHoldNr)).deleteRow()) {                    case 1 :                        lForm.show(pForm, "search");                        lButton.show(pButton, "search");                        break;                    case 2 :                        bPrev.setEnabled(false);                        break;                    case 3 :                        bNext.setEnabled(false);                        break;                    default :                        break;                }    // end of switch (((ZaurusTableForm) vHoldForms.elementAt(aktHoldNr)).deleteRow())                lastButtonDelete = false;            } else {                ZaurusEditor.printStatus(                    "Press 'Delete' a second time to delete row.");                lastButtonDelete = true;            }        // end of if (lastButtonDelete)        }            // end of if (button == Rest)    }    // when tree is refreshed ZaurusEditor.refresh() is called, too    /**     *  <code>refresh</code> will read again the meta data of the     * actual database. This is useful after changes of the table

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -