1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.JTextComponent;
import org.w3c.dom.Element;
public class AutoCompleteDecorator {
public static <T> void decorate(JComboBox<T> combo, boolean emptyByDefault, Collection<T> items) {
decorate(combo, emptyByDefault, items, (i,t)->i==null?false:i.toString().toUpperCase().startsWith(t.toUpperCase()), null);
}
public static <T> void decorate(JComboBox<T> combo, boolean emptyByDefault, Collection<T> items, BiPredicate<T,String> filter) {
decorate(combo, emptyByDefault, items, filter, null);
}
public static <T> void decorate(JComboBox<T> combo, boolean emptyByDefault, Collection<T> items, BiPredicate<T,String> filter, Function<T,String> toStringFunction) {
if ( combo==null ) throw new NullPointerException();
ComboBoxEditor editor = combo.getEditor();
if ( !(editor.getEditorComponent() instanceof JTextComponent) ) {
throw new IllegalArgumentException();
}
JTextComponent component = (JTextComponent) editor.getEditorComponent();
boolean strict = !combo.isEditable();
component.setEditable(true);
if ( !(component.getDocument() instanceof AbstractDocument) ) {
throw new IllegalArgumentException();
}
AbstractDocument document = (AbstractDocument) component.getDocument();
undecorate(combo);
AutoCompleteDecorator.Decorator<T> decorator = new AutoCompleteDecorator.Decorator<>(combo, strict, emptyByDefault, items, filter, toStringFunction, component, document);
combo.putClientProperty(KEY, decorator);
}
public static <T> void undecorate(JComboBox<T> combo) {
Object decorator = combo.getClientProperty(KEY);
if ( decorator instanceof AutoCompleteDecorator.Decorator ) {
((AutoCompleteDecorator.Decorator<?>)decorator).undecorate();
combo.putClientProperty(KEY, null);
}
}
private static final Object KEY = AutoCompleteDecorator.class.getName();
private static class Decorator<T> implements FocusListener, DocumentListener {
private final boolean strict;
private final JTextComponent component;
private final AbstractDocument document;
private final JComboBox<T> combo;
private final BiPredicate<T, String> filter;
private final List<T> data;
private final boolean emptyByDefault;
private final Function<T, String> toStringFunction;
private boolean reset;
private Decorator(JComboBox<T> combo, boolean strict, boolean emptyByDefault, Collection<T> items, BiPredicate<T, String> filter, Function<T,String> toStringFunction, JTextComponent component,
AbstractDocument document) {
this.combo=combo;
this.strict=strict;
this.emptyByDefault=emptyByDefault;
this.component=component;
this.document=document;
this.filter=filter;
this.toStringFunction = toStringFunction==null?String::valueOf:toStringFunction;
combo.setEditable(true);
component.addFocusListener(this);
document.addDocumentListener(this);
this.data = items==null?Collections.<T>emptyList():new ArrayList<T>(items);
filterCombo("",0);
}
public boolean isStrict() {
return strict;
}
public void undecorate() {
if ( isStrict() ) {
combo.setEditable(false);
}
component.removeFocusListener(this);
document.removeDocumentListener(this);
combo.removeAllItems();
data.forEach(combo::addItem);
}
public void filterCombo(String text, int offset) {
reset=true;
try {
combo.setEditable(false);
combo.removeAllItems();
List<T> filtered;
if ( !text.isEmpty() ) {
filtered = data.stream().filter(i-> filter.test(i, text)).collect(Collectors.toList());
}
else if ( !emptyByDefault ) {
filtered = data;
}
else {
filtered = Collections.emptyList();
}
filtered.forEach(combo::addItem);
combo.setEditable(true);
if ( strict ) {
if ( filtered.isEmpty() ) {
combo.setSelectedItem(text);
}
else if ( filtered.size()==1 ) {
combo.setSelectedItem(filtered.stream().iterator().next());
component.setSelectionStart(0);
component.setSelectionEnd(component.getText().length());
}
else {
combo.setSelectedItem(filtered.stream().iterator().next());
if ( offset!=component.getText().length() ) {
component.setCaretPosition(offset);
component.setSelectionStart(offset);
component.setSelectionEnd(component.getText().length());
}
else {
component.setSelectionStart(0);
component.setSelectionEnd(component.getText().length());
}
}
}
else {
combo.setSelectedItem(text);
}
}
finally {
reset=false;
}
}
@Override
public void focusGained(FocusEvent e) {
if ( !component.getText().isEmpty() || combo.getEditor().getEditorComponent().hasFocus() ){
combo.setPopupVisible(true);
}
}
@Override
public void focusLost(FocusEvent e) {
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
private void update() {
if ( reset ) return;
String text = component.getText();
SwingUtilities.invokeLater(()-> {
filterCombo(text, component.getCaretPosition());
if ( combo.isShowing() ) {
combo.setPopupVisible(true);
}
});
}
}
} |
Partager