📄 zauruseditor.java
字号:
* structures for instance creating or dropping tables, or * altering tabel. The method will be called if one asks to * refresh the tree or if the connection to the database is * changed. * * @param c a <code>Connection</code> is the actual connection to * the database */ public void refresh(Connection c) { cConn = c; if (vHoldForms == null) { this.initGUI(); // System.out.println("initGUI <<<<<<<<<<<<<<<<<<<<<<"); } else { this.resetTableForms(); // System.out.println("reset >>>>>>>>>>>>>>>>>"); } } private void initGUI() { // without connection there are no tables // vAllTables is a local variable with all table names in the database // vHoldTableNames holds the table names which have a ZaurusTableForm Vector vAllTables = getAllTables(); if (vAllTables == null) { return; } // initialize a new list for the table names which have a form in pForm vHoldTableNames = new Vector(20); vHoldForms = new Vector(20); // this holds the card panel pForm for the forms in the top // a card panel pButton below // the both card panels form a panel which is centered in this // and a status line in the south this.setLayout(new BorderLayout(3, 3)); // >>> the top of this: the entry forms in pForm // pFormButs holds in the center the forms card panel pForm and // in the south the button card panel pButton Panel pFormButs = new Panel(); pFormButs.setLayout(new BorderLayout(3, 3)); pForm = new Panel(); lForm = new CardLayout(2, 2); pForm.setLayout(lForm); // the search panel containing the list of all tables and // the entry fields for search words in the Center Panel pEntry = new Panel(); pEntry.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(3, 3, 3, 3); c.gridwidth = 1; c.gridheight = 1; c.weightx = c.weighty = 1; c.anchor = GridBagConstraints.WEST; c.gridy = 0; c.gridx = 0; pEntry.add(new Label("Search table"), c); c.gridx = 1; // get all table names and show a drop down list of them in cTables cTables = new Choice(); for (Enumeration e = vAllTables.elements(); e.hasMoreElements(); ) { cTables.addItem((String) e.nextElement()); } c.gridwidth = 2; pEntry.add(cTables, c); c.gridy = 1; c.gridx = 0; c.gridwidth = 1; pEntry.add(new Label("Search words"), c); c.gridx = 1; c.gridwidth = 2; fSearchWords = new TextField(8); pEntry.add(fSearchWords, c); // use search words c.gridwidth = 1; c.gridy = 2; c.gridx = 0; pEntry.add(new Label("Use search words"), c); gAllWords = new CheckboxGroup(); Checkbox[] checkboxes = new Checkbox[2]; checkboxes[0] = new Checkbox("all", gAllWords, true); c.gridx = 1; pEntry.add(checkboxes[0], c); checkboxes[1] = new Checkbox("any ", gAllWords, false); c.gridx = 2; pEntry.add(checkboxes[1], c); // ignore case c.gridy = 3; c.gridx = 0; pEntry.add(new Label("Ignore case"), c); gIgnoreCase = new CheckboxGroup(); Checkbox[] checkboxes1 = new Checkbox[2]; checkboxes1[0] = new Checkbox("yes", gIgnoreCase, true); c.gridx = 1; pEntry.add(checkboxes1[0], c); checkboxes1[1] = new Checkbox("no", gIgnoreCase, false); c.gridx = 2; pEntry.add(checkboxes1[1], c); // Match column exactly c.gridy = 4; c.gridx = 0; pEntry.add(new Label("Match whole col"), c); gNoMatchWhole = new CheckboxGroup(); Checkbox[] checkboxes2 = new Checkbox[2]; checkboxes2[0] = new Checkbox("no", gNoMatchWhole, true); c.gridx = 1; pEntry.add(checkboxes2[0], c); checkboxes2[1] = new Checkbox("yes ", gNoMatchWhole, false); c.gridx = 2; pEntry.add(checkboxes2[1], c); pForm.add("search", pEntry); pFormButs.add("Center", pForm); // the buttons this.initButtons(); pButton = new Panel(); lButton = new CardLayout(2, 2); pButton.setLayout(lButton); pButton.add("search", pSearchButs); pButton.add("edit", pEditButs); pButton.add("insert", pInsertButs); pFormButs.add("South", pButton); this.add("Center", pFormButs); // >>> the South: status line at the bottom Font fFont = new Font("Dialog", Font.PLAIN, 10); ZaurusEditor.tStatus = new TextField(""); ZaurusEditor.tStatus.setEditable(false); this.add("South", ZaurusEditor.tStatus); } // process the buttons events // ******************************************************* // private methods // ******************************************************* // read all table names over the current database connection // exclude tables without primary key private Vector getAllTables() { Vector result = new Vector(20); try { if (cConn == null) { return null; } dbmeta = cConn.getMetaData(); String[] tableTypes = { "TABLE" }; ResultSet allTables = dbmeta.getTables(null, null, null, tableTypes); while (allTables.next()) { String aktTable = allTables.getString("TABLE_NAME"); ResultSet primKeys = dbmeta.getPrimaryKeys(null, null, aktTable); // take only table with a primary key if (primKeys.next()) { result.addElement(aktTable); } primKeys.close(); } allTables.close(); } catch (SQLException e) { // System.out.println("SQL Exception: " + e.getMessage()); } return result; } // determine the index of the choosen table in Vector vHoldTableNames // if the table name is not in vHoldTableNames, create a ZaurusTableForm for it private int getChoosenTableIndex() { String tableName = cTables.getSelectedItem(); // System.out.println("in getChoosenTableIndex, selected Item is "+tableName); int index = getTableIndex(tableName); if (index >= 0) { // System.out.println("table found, index: " + index); return index; } // end of if (index >= 0) ZaurusTableForm tableForm = new ZaurusTableForm(tableName, cConn); pForm.add(tableName, tableForm); vHoldTableNames.addElement(tableName); vHoldForms.addElement(tableForm); // System.out.println("new tableform for table "+tableName+", index: " + index); return vHoldTableNames.size() - 1; } // determine the index of the given tableName in Vector vHoldTableNames // if the name is not in vHoldTableNames, answer -1 private int getTableIndex(String tableName) { int index; // System.out.println("begin searching for "+tableName); for (index = 0; index < vHoldTableNames.size(); index++) { // System.out.println("in getTableIndex searching for "+tableName+", index: "+index); if (tableName.equals((String) vHoldTableNames.elementAt(index))) { return index; } // end of if (tableName.equals(vHoldTableNames.elementAt(index))) } // end of for (index = 0; index < vHoldTableNames.size(); index ++) return -1; } // convert the search words in the textfield to an array of words private String[] getWords() { StringTokenizer tokenizer = new StringTokenizer(fSearchWords.getText()); String[] result = new String[tokenizer.countTokens()]; int i = 0; while (tokenizer.hasMoreTokens()) { result[i++] = tokenizer.nextToken(); } // end of while ((tokenizer.hasMoreTokens())) return result; } // init the three boxes for buttons private void initButtons() { // the buttons for the search form bSearchRow = new Button("Search Rows"); bNewRow = new Button("Insert New Row"); bSearchRow.addActionListener(this); bNewRow.addActionListener(this); pSearchButs = new Panel(); pSearchButs.setLayout(new GridLayout(1, 0, 4, 4)); pSearchButs.add(bSearchRow); pSearchButs.add(bNewRow); // the buttons for editing a row bCancel1 = new Button("Cancel"); bPrev = new Button("Prev"); bNext = new Button("Next"); bDelete = new Button("Delete"); lastButtonDelete = false; bNewSearch = new Button("Search"); bCancel1.addActionListener(this); bPrev.addActionListener(this); bNext.addActionListener(this); bDelete.addActionListener(this); bNewSearch.addActionListener(this); pEditButs = new Panel(); pEditButs.setLayout(new GridLayout(1, 0, 4, 4)); pEditButs.add(bCancel1); pEditButs.add(bPrev); pEditButs.add(bNext); pEditButs.add(bDelete); pEditButs.add(bNewSearch); // the buttons for inserting a new row pInsertButs = new Panel(); pInsertButs.setLayout(new GridLayout(1, 0, 4, 4)); bCancel2 = new Button("Cancel Insert"); bNewInsert = new Button("New Insert"); bNewSearch1 = new Button("Search"); bCancel2.addActionListener(this); bNewInsert.addActionListener(this); bNewSearch1.addActionListener(this); pInsertButs.add(bCancel2); pInsertButs.add(bNewInsert); pInsertButs.add(bNewSearch1); } // check whether the last button pressed was delete // if so, clear status line and reset the flag private void resetLastButtonDelete() { if (lastButtonDelete) { ZaurusEditor.printStatus(""); lastButtonDelete = false; } // end of if (lastButtonDelete) } // reset everything after changes in the database private void resetTableForms() { lForm.show(pForm, "search"); lButton.show(pButton, "search"); Vector vAllTables = getAllTables(); // fill the drop down list again // get all table names and show a drop down list of them in cTables cTables.removeAll(); for (Enumeration e = vAllTables.elements(); e.hasMoreElements(); ) { cTables.addItem((String) e.nextElement()); } // remove all form panels from pForm for (Enumeration e = vHoldForms.elements(); e.hasMoreElements(); ) { pForm.remove((ZaurusTableForm) e.nextElement()); } // end of while (Enumeration e = vHoldForms.elements(); e.hasMoreElements();) // initialize a new list for the table names which have a form in pForm vHoldTableNames = new Vector(20); vHoldForms = new Vector(20); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -