From 7422cf351c63aa582a5afb6ef6d241ed68a68fba Mon Sep 17 00:00:00 2001 From: Yana Stamcheva Date: Mon, 3 Apr 2006 14:30:10 +0000 Subject: [PATCH] Search in contactlist --- .../contactlist/CListKeySearchListener.java | 94 +++++++------------ .../gui/main/contactlist/ContactList.java | 46 +++++++++ 2 files changed, 82 insertions(+), 58 deletions(-) diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/CListKeySearchListener.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/CListKeySearchListener.java index 9908842e2..5e84210fe 100644 --- a/src/net/java/sip/communicator/impl/gui/main/contactlist/CListKeySearchListener.java +++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/CListKeySearchListener.java @@ -7,6 +7,7 @@ package net.java.sip.communicator.impl.gui.main.contactlist; +import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -21,29 +22,33 @@ public class CListKeySearchListener implements KeyListener { private ContactList contactList; - private char lastTypedKey = 0; + private String lastTypedKey; + private long lastTypedTimestamp = 0; + private StringBuffer keyBuffer = new StringBuffer(); public CListKeySearchListener(ContactList contactList){ this.contactList = contactList; } - public void keyPressed(KeyEvent e) { - + public void keyPressed(KeyEvent e) { } - public void keyReleased(KeyEvent e) { - + public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { - - int contactIndex = - 1; - + long eventTimestamp = e.getWhen(); + String keyChar = String.valueOf(e.getKeyChar()); + + 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(); - String keyChar = String.valueOf(e.getKeyChar()); - //Checks if there's any selected contact node and gets its name. if(selectedIndex != -1){ Object selectedObject = this.contactList.getSelectedValue(); @@ -55,57 +60,30 @@ public void keyTyped(KeyEvent e) { if (selectedContactName != null) selectedSameLetterContact = selectedContactName.substring(0, 1) - .equalsIgnoreCase(keyChar); + .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 - */ - if(lastTypedKey != e.getKeyChar() || !selectedSameLetterContact) { - contactIndex = this.getNextMatch(keyChar, 0); + // 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 = -1; + if(lastTypedKey != keyChar || !selectedSameLetterContact) { + contactIndex = this.contactList.getNextMatch(keyBuffer.toString(), + 0, Position.Bias.Forward); } else { - contactIndex = this.getNextMatch( keyChar, - selectedIndex + 1); - } - - if(contactIndex != -1){ - this.contactList.setSelectedIndex(contactIndex); - this.contactList.ensureIndexIsVisible(contactIndex); - } - - this.lastTypedKey = e.getKeyChar(); - } - - /** - * Returns the next contact node that starts with the given prefix. - * - * @param keyChar The char to test for a match. - * @param startIndex The index for starting the search. - * @return The index of the next contact that starts with the prefix. - */ - private int getNextMatch(String keyChar, int startIndex){ - - int indexToSelect = -1; - - int index = this.contactList.getNextMatch( keyChar, - startIndex, - Position.Bias.Forward); - if(index != -1){ - Object element = this.contactList.getModel().getElementAt(index); - - if(element instanceof MetaContact){ - indexToSelect = index; - } - else{ - indexToSelect = getNextMatch(keyChar, index + 1); - } - } - - return indexToSelect; + contactIndex = this.contactList.getNextMatch(keyBuffer.toString(), + selectedIndex + 1, Position.Bias.Forward); + } + + int currentlySelectedIndex = this.contactList.getSelectedIndex(); + + if(currentlySelectedIndex != contactIndex && contactIndex != -1) + this.contactList.setSelectedIndex(contactIndex); + + this.contactList.ensureIndexIsVisible(currentlySelectedIndex); + + this.lastTypedKey = keyChar; } } \ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactList.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactList.java index 0e9951963..ab54b8f7e 100644 --- a/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactList.java +++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactList.java @@ -214,6 +214,52 @@ private void groupAdded(MetaContactGroup group){ this.groupAdded(subGroup); } } + + /** + * Returns the next list element that starts with + * a prefix. + * + * @param prefix the string to test for a match + * @param startIndex the index for starting the search + * @param bias the search direction, either + * Position.Bias.Forward or Position.Bias.Backward. + * @return the index of the next list element that + * starts with the prefix; otherwise -1 + * @exception IllegalArgumentException if prefix is null + * or startIndex is out of bounds + */ + public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { + ContactListModel model = (ContactListModel)this.getModel(); + int max = model.getSize(); + if (prefix == null) { + throw new IllegalArgumentException(); + } + if (startIndex < 0 || startIndex >= max) { + throw new IllegalArgumentException(); + } + prefix = prefix.toUpperCase(); + + // start search from the next element after the selected element + int increment = (bias == Position.Bias.Forward) ? 1 : -1; + int index = startIndex; + do { + Object o = model.getElementAt(index); + + if (o != null) { + String contactName = null; + + if (o instanceof MetaContact) { + contactName = ((MetaContact)o).getDisplayName().toUpperCase(); + } + + if (contactName != null && contactName.startsWith(prefix)) { + return index; + } + } + index = (index + increment + max) % max; + } while (index != startIndex); + return -1; + } }