mirror of https://github.com/zlatinb/muwire
Rework the search box dropdown after the contacts selector dropdown
parent
53206b3b84
commit
3ce7dd0f8f
|
@ -26,31 +26,6 @@ class SearchFieldEditor extends BasicComboBoxEditor {
|
||||||
def action = field.getAction()
|
def action = field.getAction()
|
||||||
field.setAction(null)
|
field.setAction(null)
|
||||||
editor.setAction(action)
|
editor.setAction(action)
|
||||||
editor.getDocument().addDocumentListener(new DocumentListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void insertUpdate(DocumentEvent e) {
|
|
||||||
SwingUtilities.invokeLater({
|
|
||||||
field.hidePopup()
|
|
||||||
if (model.onKeyStroke(editor.text))
|
|
||||||
field.showPopup()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeUpdate(DocumentEvent e) {
|
|
||||||
SwingUtilities.invokeLater({
|
|
||||||
field.hidePopup()
|
|
||||||
if (model.onKeyStroke(editor.text))
|
|
||||||
field.showPopup()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void changedUpdate(DocumentEvent e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
editor.addMouseListener(new MouseAdapter() {
|
editor.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -73,11 +48,14 @@ class SearchFieldEditor extends BasicComboBoxEditor {
|
||||||
editor.addKeyListener(new KeyAdapter() {
|
editor.addKeyListener(new KeyAdapter() {
|
||||||
@Override
|
@Override
|
||||||
void keyPressed(KeyEvent e) {
|
void keyPressed(KeyEvent e) {
|
||||||
if (e.getKeyCode() != KeyEvent.VK_ENTER)
|
int keyCode = e.getKeyCode()
|
||||||
|
if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN)
|
||||||
return
|
return
|
||||||
if (model.getSelectedItem() != null) {
|
SwingUtilities.invokeLater({
|
||||||
editor.setText((String)model.getSelectedItem())
|
field.hidePopup()
|
||||||
}
|
if (model.onKeyStroke(editor.text))
|
||||||
|
field.showPopup()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
package com.muwire.gui
|
package com.muwire.gui
|
||||||
|
|
||||||
import javax.swing.AbstractListModel
|
import javax.swing.AbstractListModel
|
||||||
|
import javax.swing.DefaultComboBoxModel
|
||||||
import javax.swing.MutableComboBoxModel
|
import javax.swing.MutableComboBoxModel
|
||||||
|
|
||||||
class SearchFieldModel extends AbstractListModel implements MutableComboBoxModel {
|
class SearchFieldModel extends DefaultComboBoxModel implements MutableComboBoxModel {
|
||||||
private final UISettings uiSettings
|
private final UISettings uiSettings
|
||||||
private final File settingsFile
|
private final File settingsFile
|
||||||
private final List<String> objects = new ArrayList<>()
|
|
||||||
private String selectedObject
|
|
||||||
|
|
||||||
SearchFieldModel(UISettings uiSettings, File home) {
|
SearchFieldModel(UISettings uiSettings, File home) {
|
||||||
super()
|
super()
|
||||||
this.uiSettings = uiSettings
|
this.uiSettings = uiSettings
|
||||||
this.settingsFile = new File(home, "gui.properties")
|
this.settingsFile = new File(home, "gui.properties")
|
||||||
uiSettings.searchHistory.each { objects.add(it) }
|
addAll(uiSettings.searchHistory)
|
||||||
fireIntervalAdded(this, 0, objects.size() - 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addElement(Object string) {
|
public void addElement(Object string) {
|
||||||
if (string == null)
|
if (string == null)
|
||||||
return
|
return
|
||||||
|
@ -25,75 +23,27 @@ class SearchFieldModel extends AbstractListModel implements MutableComboBoxModel
|
||||||
return
|
return
|
||||||
settingsFile.withOutputStream { uiSettings.write(it) }
|
settingsFile.withOutputStream { uiSettings.write(it) }
|
||||||
}
|
}
|
||||||
objects.add(string);
|
super.addElement(string)
|
||||||
fireIntervalAdded(this,objects.size()-1, objects.size()-1);
|
|
||||||
if ( objects.size() == 1 && selectedObject == null && string != null ) {
|
|
||||||
setSelectedItem( string );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean onKeyStroke(String selected) {
|
boolean onKeyStroke(String selected) {
|
||||||
selectedObject = selected
|
|
||||||
if (selected == null || selected.length() == 0) {
|
if (selected == null || selected.length() == 0) {
|
||||||
objects.clear()
|
removeAllElements()
|
||||||
uiSettings.searchHistory.each { objects.add(it) }
|
addAll(uiSettings.searchHistory)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
objects.clear()
|
|
||||||
|
|
||||||
Set<String> matching = new HashSet<>(uiSettings.searchHistory)
|
removeAllElements()
|
||||||
|
setSelectedItem(selected)
|
||||||
|
|
||||||
|
List<String> matching = new ArrayList<>(uiSettings.searchHistory)
|
||||||
matching.retainAll { it.containsIgnoreCase(selected) }
|
matching.retainAll { it.containsIgnoreCase(selected) }
|
||||||
|
|
||||||
matching.each {
|
|
||||||
objects.add(it)
|
|
||||||
}
|
|
||||||
Collections.sort(objects)
|
|
||||||
if (!objects.isEmpty()) {
|
|
||||||
fireIntervalAdded(this, 0, objects.size() - 1)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (matching.isEmpty())
|
||||||
public void setSelectedItem(Object anObject) {
|
return false
|
||||||
if ((selectedObject != null && !selectedObject.equals( anObject )) ||
|
|
||||||
selectedObject == null && anObject != null) {
|
|
||||||
selectedObject = anObject;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
Collections.sort(matching)
|
||||||
public Object getSelectedItem() {
|
addAll(matching)
|
||||||
selectedObject
|
return true
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getSize() {
|
|
||||||
objects.size()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getElementAt(int index) {
|
|
||||||
if ( index >= 0 && index < objects.size() )
|
|
||||||
return objects.get(index);
|
|
||||||
else
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeElement(Object obj) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void insertElementAt(Object item, int index) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeElementAt(int index) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue