📄 nlbean.java~
字号:
if (sel1x.length>0) {
String sel2 [] = list2.getSelectedItems();
String sel3 [] = list3.getSelectedItems();
if (sel2!=null && sel3!=null) {
if (sel3.length>0) {
String user="";
String pass="";
if (list1_last_selection >= 0 && list1_last_selection < userNames.length) {
user = userNames[list1_last_selection];
pass = passwords[list1_last_selection];
}
try {
String r = engine.getRows("SELECT " + sel3[0] + " FROM " + sel2[0],
sel1x[0],
user,
pass);
if (r==null) return;
putText(r + "\n", false);
} catch (Exception e) { }
}
}
}
}
}
}
}
private Choice choice;
transient private boolean choiceChanged = false;
class ChoiceListener implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
System.out.println("choice menu: " + ie.paramString());
System.out.println("choice menu: " + (String)ie.getItem());
String sel = (String)ie.getItem();
if (sel.equals("Examples")) return;
inText(sel, true);
if (choiceChanged==false) {
choice.remove(0);
choiceChanged=true;
}
System.out.println("choice menu: <returning>");
}
}
private Help help;
class MouseHelp extends MouseAdapter implements Serializable {
public void mouseReleased(MouseEvent mevt) {
help.setVisible(true);
}
}
public static void main(String args[]) {
Frame f = new Frame();
NLBean sc = new NLBean();
f.add(sc);
f.setTitle("NLBean version 4.0");
f.pack();
f.show();
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private TextArea outputText;
private /* Smart */ TextField inputText;
private TextArea sqlText;
private java.awt.List list1, list2, list3;
private int list1_last_selection, list2_last_selection, list3_last_selection;
// properties for specifying a few sample natural language
// queries to place at the beginning of the 'Example'
// 'choice' control. NOTE: this program automatically
// adds additional examples that it generates from examining
// the field names of the database tables.
private String [] examples = {
"list email address where name equals Mark",
"list salary where employee name equals Mark",
"list salary where hire date is after 1993/1/5 and employee name equals Mark",
"list name, phone number, and email address where name equals Mark",
"list employee name, salary, and hire date where hire date is after January 10, 1993",
"list salary where hire date is after January 1, 1993 or employee name equals Carol",
"list product name where cost is less than $20"
};
// Set up properties for synonyms:
private String [] synonyms = {
"employee name=EmpName",
"hire date=HireDate",
"phone number=PhoneNumber",
"email address=Email",
"product name=productname",
"products=productname",
"product=productname"
};
// for four properties each for up to three databases:
// Demo database information
private String [] databaseNames = {"jdbc:idb:database/nlbean.prp"};
private String [] userNames = {"Admin"};
private String [] passwords = {"sammy"};
private String [] tableLists= {"NameTable;products;Employees"};
//
// Inner class to handle text input field:
//
transient protected int startWordPos=0, stopWordPos=0;
class SmartTextField extends TextField implements Serializable {
public SmartTextField(String s, int width) {
super(s, width);
helper();
}
public SmartTextField(String s) {
super(s);
helper();
}
public SmartTextField() {
super();
helper();
}
private void helper() {
addMouseListener(new MouseText());
addKeyListener(new MyKeyAdapter());
setEditable(true);
setFont(new Font("default", Font.PLAIN, 12));
words = new String[20];
for (int i=0; i<20; i++) words[i]="";
num_words=0;
}
transient private String words[] = null;
transient int num_words;
public String getWord(int charPos) {
startWordPos=stopWordPos=-1;
String s = getText();
// find start of word:
int start = charPos-1;
try {
while (start >= 0 && s.charAt(start)!=' ') {
start--;
}
start++;
} catch (Exception e) {}
// find the end of word:
int stop=charPos;
try {
while (stop < s.length() && s.charAt(stop)!=' ') {
stop++;
}
} catch (Exception e) {}
if (start<0) start=0;
//if (stop>s.length()-1) stop=s.length()-1;
System.out.println("getWord(): start=" + start + ", stop=" + stop);
//if (stop > s.length() - 2) stop = s.length() - 1;
int stp = stop;
if (stop>my_char_pos) {
stop=my_char_pos;
System.out.println(" stop reset to " + stop);
}
if (start < stop) {
startWordPos=start;
stopWordPos=stp;
System.out.println("getWord() returning: |" + s.substring(start, stop) + "|");
return s.substring(start, stop);
}
else return "";
}
public void setUpperCase(int charPos) {
String s = getText();
// find start of word:
int start = charPos-1;
try {
while (start >= 0 && s.charAt(start)!=' ') {
start--;
}
start++;
} catch (Exception e) {}
// find the end of word:
int stop=charPos;
try {
while (stop < s.length() && s.charAt(stop)!=' ') {
stop++;
}
} catch (Exception e) {}
if (start<0) start=0;
if (stop>s.length()) stop=s.length();
if (stop>my_char_pos) stop=my_char_pos;
if (start < stop) {
StringBuffer sb = new StringBuffer(s);
for (int i=start; i<stop; i++) {
char ch = sb.charAt(i);
if (ch >= 'a' && ch <= 'z') ch += 'A' - 'a';
sb.setCharAt(i, ch);
}
setText(new String(sb));
setCaretPosition(stop);
}
}
transient private int charWidth = -1;
transient private FontMetrics fm = null;
public void paint(Graphics g) {
super.paint(g);
if (fm==null) {
fm = g.getFontMetrics();
}
}
private int getPixelWidth(String s) {
if (fm==null) return 1;
return fm.stringWidth(s);
}
transient private int my_char_pos=0;
public int getCharPos(int pixel) {
String s = getText();
my_char_pos=-1;
if (getPixelWidth(s) < pixel) return -1; // to right of text
int len = s.length();
for (int i=1; i<len; i++) {
if (getPixelWidth(s.substring(0, i)) > pixel - 12) {
my_char_pos=i;
return i;
}
}
my_char_pos = len-1;
return len-1;
}
// Handle mouse events over the input text window:
class MouseText extends MouseAdapter implements Serializable {
public void mousePressed(MouseEvent mevt) {
int x = mevt.getX();
int charPos = getCharPos(x);
if (charPos<0) return;
my_char_pos = 1000; // special for mouse click only!
String word = getWord(charPos);
System.out.println("MouseText.mousePressed(): getWord returned: " + word);
if (word.length() < 1) return;
System.out.println("x=" + x + ", charPos=" + charPos +
", word=" + word);
}
}
class MyKeyAdapter extends KeyAdapter implements Serializable {
public void keyTyped(KeyEvent ke) {
if (ke.getKeyChar()==KeyEvent.VK_SPACE ||
ke.getKeyChar()==KeyEvent.VK_COMMA ||
ke.getKeyChar()==KeyEvent.VK_PERIOD)
{
// Handle new word:
//System.out.println("New word. all text:" + getText());
int charPos = getCaretPosition();
my_char_pos = charPos;
System.out.println("** charPos=" + charPos);
String word = getWord(charPos);
System.out.println("** word =" + word);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -