// If they type a key we want to match based on the nodes toString
else if (ke.getID() == KeyEvent.KEY_TYPED) {
Array_Of_DisplayNode<DisplayNode> nodes = this.getViewNodes();
long currentTime = System.currentTimeMillis();
int currentIndex = Math.max(this.getSelectedRow()-1, 0);
DisplayNode nodeToSelect = null;
int indexToSelect = 0;
String strToMatch;
// If more then 0.7 seconds has passed since the last key press. Clear out the keys pressed.
if (currentTime - keyScrollerLastKeyPressTime > 700) {
keyScrollerKeysPressed = new StringBuilder(5);
}
// Record the current time and key press
keyScrollerLastKeyPressTime = currentTime;
keyScrollerKeysPressed.append(ke.getKeyChar());
// Check if they keep pressing the same key
boolean isSameKeyPress = true;
for (int i=0; i<keyScrollerKeysPressed.length(); i++) {
if (keyScrollerKeysPressed.charAt(i) != ke.getKeyChar()) {
isSameKeyPress = false;
break;
}
}
// If they keep pressing the same key, then only match on the first character
if (isSameKeyPress) {
strToMatch = String.valueOf(ke.getKeyChar());
}
// Otherwise match on everything they have typed
else {
strToMatch = keyScrollerKeysPressed.toString();
}
// Start searching from our current location
for (int i=currentIndex+1; i<nodes.size(); i++) {
DisplayNode aNode = nodes.get(i);
int length = strToMatch.length();
String nodeStr = aNode.toString();
if ( nodeStr != null &&
nodeStr.length() >= length &&
nodeStr.substring(0, length).equalsIgnoreCase(strToMatch.toString())) {
nodeToSelect = aNode;
indexToSelect = i+1;
break;
}
}
// If no match, continue searching from the start
if (nodeToSelect == null) {
for (int i=0; i<currentIndex; i++) {
DisplayNode aNode = nodes.get(i);
int length = strToMatch.length();
String nodeStr = (aNode == null) ? null : aNode.toString();
if ( nodeStr != null &&
nodeStr.length() >= length &&
nodeStr.substring(0, length).equalsIgnoreCase(strToMatch.toString())) {
nodeToSelect = aNode;