}
public void keyTyped(KeyEvent e) {
char ch = e.getKeyChar();
if (Character.isLetter(ch)) {
JTextComponent comp = (JTextComponent) e.getSource();
if ((toSetIn != null) && (toSetIn.length() > 1) &&
(ch == toSetIn.charAt(1))) {
// User continues on the word that was suggested.
toSetIn = toSetIn.substring(1);
if (toSetIn.length() > 0) {
int cp = comp.getCaretPosition();
//comp.setCaretPosition(cp+1-toSetIn.);
//System.out.println(cp-toSetIn.length()+" - "+cp);
comp.select(cp + 1 - toSetIn.length(), cp);
lastBeginning = lastBeginning + ch;
e.consume();
lastCaretPosition = comp.getCaretPosition();
//System.out.println("Added char: '"+toSetIn+"'");
//System.out.println("LastBeginning: '"+lastBeginning+"'");
lastCompletions = findCompletions(lastBeginning, comp);
lastShownCompletion = 0;
for (int i = 0; i < lastCompletions.length; i++) {
Object lastCompletion = lastCompletions[i];
//System.out.println("Completion["+i+"] = "+lastCompletion);
if (((String)lastCompletion).endsWith(toSetIn)) {
lastShownCompletion = i;
break;
}
}
//System.out.println("Index now: "+lastShownCompletion);
if (toSetIn.length() < 2)
toSetIn = null;
return;
}
}
if ((toSetIn != null) && ((toSetIn.length() <= 1) ||
(ch != toSetIn.charAt(1)))) {
// User discontinues the word that was suggested.
lastBeginning = lastBeginning + ch;
Object[] completed = findCompletions(lastBeginning, comp);
if ((completed != null) && (completed.length > 0)) {
lastShownCompletion = 0;
lastCompletions = completed;
String sno = (String) (completed[0]);
int lastLen = toSetIn.length() - 1;
toSetIn = sno.substring(lastBeginning.length() - 1);
String text = comp.getText();
//Util.pr(""+lastLen);
comp.setText(text.substring(0, lastCaretPosition - lastLen)
+ toSetIn
+ text.substring(lastCaretPosition));
comp.select(lastCaretPosition + 1 - lastLen,
lastCaretPosition + toSetIn.length() - lastLen);
lastCaretPosition = comp.getCaretPosition();
e.consume();
return;
} else {
toSetIn = null;
return;
}
}
StringBuffer currentword = getCurrentWord(comp);
if (currentword == null)
return;
currentword.append(ch);
Object[] completed = findCompletions(currentword.toString(), comp);
int no = 0; // We use the first word in the array of completions.
if ((completed != null) && (completed.length > 0)) {
lastShownCompletion = 0;
lastCompletions = completed;
String sno = (String) (completed[no]);
toSetIn = sno.substring(currentword.length() - 1);
//Util.pr("AutoCompListener: Found "+completed[0]);
StringBuffer alltext = new StringBuffer(comp.getText());
int cp = comp.getCaretPosition();
alltext.insert(cp, toSetIn);
//Util.pr(alltext.toString());
comp.setText(alltext.toString());
comp.setCaretPosition(cp);
comp.select(cp + 1, cp + 1 + sno.length() - currentword.length());
e.consume();
lastCaretPosition = comp.getCaretPosition();
lastBeginning = currentword.toString();
return;
}
}
//Util.pr("#hm");