public void keyPressed(KeyEvent e) { // Nothing to do if the contact list is empty if (contactList.getModel().getSize() <= 0) return; int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { closeGroup(); } else if (keyCode == KeyEvent.VK_RIGHT) { openGroup(); } else if (keyCode == KeyEvent.VK_ENTER) { openOrCloseGroup(); } }
/** * Searches the contact list when any key, different from "space", "+" or "-" is typed. Selects * the Contact name closest to the typed string. The time between two button presses is checked to * determine whether the user makes a new search or a continuous search. When user types the same * letter consecutively the search mechanism selects the next Contact name starting with the same * letter. */ public void keyTyped(KeyEvent e) { // Nothing to do if the contact list is empty if (contactList.getModel().getSize() <= 0) return; long eventTimestamp = e.getWhen(); char keyChar = e.getKeyChar(); if (keyChar == ' ') { openOrCloseGroup(); } else if (keyChar == '+') { openGroup(); } else if (keyChar == '-') { closeGroup(); } else { if ((lastTypedTimestamp - eventTimestamp) > 1000) { keyBuffer.delete(0, keyBuffer.length() - 1); } this.lastTypedTimestamp = eventTimestamp; this.keyBuffer.append(keyChar); boolean selectedSameLetterContact = false; int selectedIndex = this.contactList.getSelectedIndex(); // Check if there's any selected contact node and get its name. if (selectedIndex != -1) { Object selectedObject = this.contactList.getSelectedValue(); if (selectedObject instanceof MetaContact) { String selectedContactName = ((MetaContact) selectedObject).getDisplayName(); if (selectedContactName != null) { selectedSameLetterContact = selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString()); } } else if (selectedObject instanceof ConferenceChatContact) { String selectedContactName = ((ConferenceChatContact) selectedObject).getName(); if (selectedContactName != null) { selectedSameLetterContact = selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString()); } } } // The search starts from the beginning if: // 1) the newly entered character is different from the last one // or // 2) the currently selected contact starts with a different letter int contactIndex = contactList.getNextMatch( keyBuffer.toString(), (lastTypedKey != keyChar || !selectedSameLetterContact) ? 0 : selectedIndex + 1, Position.Bias.Forward); int currentlySelectedIndex = this.contactList.getSelectedIndex(); if (currentlySelectedIndex != contactIndex && contactIndex != -1) { this.contactList.setSelectedIndex(contactIndex); currentlySelectedIndex = contactList.getSelectedIndex(); } this.contactList.ensureIndexIsVisible(currentlySelectedIndex); this.lastTypedKey = keyChar; } }