entanglement:
lina:
Evidentemente, se você tiver duas LINA cadastrada (o que é muito dificil existir 2 nome desse no mundo), o seu ArrayList conterá 2 add LINA. Justamente porque o ArrayList não elimina os indices duplicados (o que é correto).
Só para tumultuar. Estava com minha esposa em uma loja de bijuterias - e havia bijuterias da marca chinesa "Li Na" :) - talvez fosse referência a uma tenista chinesa, 李娜 (Li Na).
Oi,
Sabe que eu não sei o real significado do meu nome? Vai ver que é esse! rsrs
Então, aproveitando... não consegui uma solução plausível para o nosso amigo! Acho que colocar o código na frente dos itens do Combo pode não ter sido uma boa ideia.
O detalhe é que nesse auto-complete, quando existem nomes como por exemplo: Administrador, Automação a logica está falha.
01-Administrador
02-Automação
O método lookupItem(String) faz uso de startsWithIgnoreAccentsAndCase, e quando o usuário digita A, automaticamente ele pega o índice Administrador. Como ele testa se existe um item selecionado antes, ele não dá a opção do usuário continuar digitando o restante da palavra (que seria utomação).
private Object lookupItem(String pattern) {
Object selectedItem = model.getSelectedItem();
// only search for a different item if the currently selected does not match
if (selectedItem != null && startsWithIgnoreAccentsAndCase(selectedItem.toString(), pattern)) {
return selectedItem;
} else {
// iterate over all items
for (int i=0, n=model.getSize(); i < n; i++) {
Object currentItem = model.getElementAt(i);
// current item starts with the pattern?
if (currentItem != null && startsWithIgnoreAccentsAndCase(currentItem.toString(), pattern)) {
return currentItem;
}
}
}
// no item starts with the pattern => return null
return null;
}
Se eu tirar a primeira condição IF do método, implica em outro método insertString:
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
// return immediately when selecting an item
if (selecting) return;
// insert the string into the document
super.insertString(offs, str, a);
// lookup and select a matching item
Object item = lookupItem(getText(0, getLength()));
if (item != null) {
setSelectedItem(item);
} else {
// keep old item selected if there is no match
item = comboBox.getSelectedItem();
// imitate no insert (later on offs will be incremented by str.length(): selection won't move forward)
offs = offs-str.length();
// provide feedback to the user that his input has been received but can not be accepted
comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
}
if(item != null)
setText(item.toString());
else
setText("");
// select the completed part
highlightCompletedText(offs+str.length());
}
Se eu remover o código do nome em lookupItem e tirasse o primeiro IF, também não funcionaria:
private Object lookupItem(String pattern) {
// Object selectedItem = model.getSelectedItem();
// only search for a different item if the currently selected does not match
// if (selectedItem != null && startsWithIgnoreAccentsAndCase(selectedItem.toString(), pattern)) {
// return selectedItem;
// } else {
// iterate over all items
for (int i=0, n=model.getSize(); i < n; i++) {
Object currentItem = model.getElementAt(i);
// current item starts with the pattern?
if (currentItem != null && startsWithIgnoreAccentsAndCase(currentItem.toString().replaceAll("[^a-z|A-Z]", ""), pattern)) {
return currentItem;
}
}
// }
// no item starts with the pattern => return null
return null;
}
Como estou sem tempo para ficar testando, fiquei numa sinuca de bico =)
Tchauzin!