📄 nlbean.java
字号:
// mwa.products.NLBean.java
//
// Copyright 1997, Mark Watson.
//
package nlbean;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.text.*;
import java.util.*;
import java.beans.*;
import java.sql.*;
import java.util.Vector;
public class NLBean extends Panel implements Serializable {
protected NLEngine engine = null;
public NLBean() {
super();
reset();
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
System.out.println("Beginning writeObject");
s.defaultWriteObject();
System.out.println("Ending writeObject");
}
private void resetExamples() {
choiceChanged = false;
choice.removeAll();
choice.addItem("Examples ");
for (int i=(examples.length - 1); i>= 0; i--) {
choice.insert(examples[i], 1);
}
}
private void resetSynonyms() {
engine.clearSynonyms();
for (int i=0; i<synonyms.length; i++) {
addSynonym(synonyms[i]);
}
}
// Set up USER INTERFACE:
private void reset() {
engine = new NLEngine();
AWTsetup();
}
private void AWTsetup() {
Frame help_frame = new Frame();
help = new Help(help_frame);
setFont(new Font("Dialog", Font.PLAIN, 12));
setLayout(null);
Label l1 = new Label("Natural Language Database Access");
l1.setFont(new Font("Dialog", Font.BOLD, 28));
add(l1);
l1.setBounds(2, 1, 600, 34);
list1 = new java.awt.List(3, false);
for (int i=0; i<databaseNames.length; i++) list1.add(databaseNames[i]);
list2 = new java.awt.List(3, false);
list3 = new java.awt.List(3, false);
add(list1); add(list2); add(list3);
list1.setBounds(2, 40, 220, 90);
list2.setBounds(232, 40, 170, 90);
list3.setBounds(412, 40, 170, 90);
list1.addMouseListener(new MouseSelect1());
list2.addMouseListener(new MouseSelect2());
list3.addMouseListener(new MouseSelect3());
Button q_button = new Button("Do query");
q_button.addMouseListener(new MouseQuery());
add(q_button); q_button.setBounds(2, 140, 160, 30);
Button help_button = new Button("Help");
help_button.addMouseListener(new MouseHelp());
add(help_button); help_button.setBounds(172, 140, 40, 30);
Label label22 = new Label("Query:");
label22.setFont(new Font("Dialog", Font.BOLD, 14));
add(label22); label22.setBounds(2, 180, 60, 22); label22.setVisible(true);
// inputText = new SmartTextField("list Salary where EmpName equals Mark", 64);
inputText = new TextField("list Salary where EmpName equals Mark", 64);
add(inputText); inputText.setBounds(80, 180, 500, 22);
choice = new Choice();
choiceChanged = false;
choice.addItem("Examples ");
for (int i=(examples.length - 1); i>=0; i--) choice.insert(examples[i], 1);
choice.addItemListener(new ChoiceListener());
add(choice); choice.setBounds(2, 210, 582, 25);
Label label23 = new Label("Generated SQL:");
label23.setFont(new Font("Dialog", Font.BOLD, 12));
add(label23); label23.setBounds(2, 240, 120, 30);
sqlText = new TextArea("",1,80,TextArea.SCROLLBARS_NONE);
sqlText.setEditable(false);
add(sqlText); sqlText.setBounds(130, 240, 455, 40);
outputText = new TextArea("NLBean(tm) natural language interface\nCopyright 1997, Mark Watson. All rights reserved.\n", 8, 74);
add(outputText); outputText.setBounds(2, 285, 582, 150);
Label l1x = new Label("NLBean Copyright 1997-1999 by Mark Watson. www.markwatson.com");
l1x.setFont(new Font("Dialog", Font.ITALIC, 14));
add(l1x);
l1x.setBounds(5, 442, 540, 16);
// * * *
list1_last_selection=-1; list2_last_selection=-1; list3_last_selection=-1;
setBounds(20, 20, 590, 464);
setupHelper();
}
private void setupHelper() {
if (loadSynonyms) {
for (int i=0; i<synonyms.length; i++) addSynonym(synonyms[i]);
}
if (loadDB) {
for (int i=0; i<databaseNames.length; i++) {
engine.addDB(databaseNames[i], userNames[i], passwords[i], tableLists[i]);
}
}
engine.initDB();
}
// Suggest spelling corrections, or other words:
// synchronized private String[] suggestedWords(String a_word) {
// return engine.suggestedWords(a_word);
// }
synchronized private void putText(String s, boolean replace_flag) {
if (replace_flag==false) {
outputText.append(s);
} else {
outputText.setText(s);
}
}
synchronized private String inText(String new_val, boolean set_flag) {
if (set_flag) {
inputText.setText(new_val);
return "";
}
return inputText.getText();
}
synchronized private void query() {
System.out.println("Entering query()");
sqlText.setText("");
String a_query = inText("", false);
System.out.println("query(): a_query=" + a_query);
String sql_query=null;
if (a_query.startsWith("SELECT") || a_query.startsWith("select")) {
sql_query = a_query;
} else {
engine.parse(a_query);
sql_query = engine.getSQL();
}
if (sql_query==null) {
System.out.println("No SQL for " + a_query);
return;
}
System.out.println("SQL query: " + sql_query);
sqlText.setText(sql_query);
try {
engine.createResultSet(sql_query, databaseNames[0], userNames[0], passwords[0]);
putText("Query results:\n", false);
String data = engine.getRows(sql_query, databaseNames[0], userNames[0], passwords[0]);
putText(engine.toEnglish(data) + "\n", false);
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean usingAWT = false;
private boolean loadSynonyms = true;
private boolean loadDB = true;
private void addSynonym(String def_string) {
int pos = def_string.indexOf("=");
String description = def_string.substring(0,pos);
String column = def_string.substring(pos+1);
if (engine!=null) {
engine.addSynonym(column, description);
}
}
// Use an inner classes for mouse event handling:
class MouseQuery extends MouseAdapter implements Serializable {
// The magic: access the public query method in the
// containing class:
synchronized public void mouseReleased(MouseEvent mevt) {
query();
}
}
// Use an inner classes for mouse event handling:
class MouseSelect1 extends MouseAdapter implements Serializable {
synchronized public void mouseReleased(MouseEvent mevt) {
if (list1_last_selection != list1.getSelectedIndex()) {
list1_last_selection = list1.getSelectedIndex();
String s="";
if (list1_last_selection >= 0 && list1_last_selection < tableLists.length) {
s = tableLists[list1_last_selection];
}
System.out.println("s=" + s);
String temp [] = Util.parseStrings(s);
list2.removeAll(); list3.removeAll();
for (int i=0; i<temp.length; i++) list2.addItem(temp[i]);
}
}
}
// Use an inner classes for mouse event handling:
class MouseSelect2 extends MouseAdapter implements Serializable {
synchronized public void mouseReleased(MouseEvent mevt) {
if (list2_last_selection != list2.getSelectedIndex()) {
list2_last_selection = list2.getSelectedIndex();
list3.removeAll();
String sel1 [] = list1.getSelectedItems();
if (sel1!=null) {
if (sel1.length>0) {
String sel2 [] = list2.getSelectedItems();
if (sel2!=null) {
if (sel2.length>0) {
String user="";
String pass="";
if (list1_last_selection >= 0 && list1_last_selection < userNames.length) {
user = userNames[list1_last_selection];
}
try {
String cols[] = engine.getColumnNames(sel2[0],
sel1[0], user, pass);
if (cols!=null) {
for (int j=0; j<cols.length; j++) {
list3.addItem(cols[j]);
}
}
} catch (Exception e) { }
}
}
}
}
}
}
}
// Use an inner classes for mouse event handling:
class MouseSelect3 extends MouseAdapter implements Serializable {
synchronized public void mouseReleased(MouseEvent mevt) {
if (list3_last_selection != list3.getSelectedIndex()) {
System.out.println("TESTING 3rd list selection");
list3_last_selection = list3.getSelectedIndex();
String [] sel1x = list1.getSelectedItems();
if (sel1x!=null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -