| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.gui.widgets;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.event.FocusEvent;
|
|---|
| 5 | import java.awt.event.FocusListener;
|
|---|
| 6 | import java.util.Collection;
|
|---|
| 7 |
|
|---|
| 8 | import javax.swing.ComboBoxModel;
|
|---|
| 9 | import javax.swing.DefaultComboBoxModel;
|
|---|
| 10 | import javax.swing.JComboBox;
|
|---|
| 11 | import javax.swing.text.AttributeSet;
|
|---|
| 12 | import javax.swing.text.BadLocationException;
|
|---|
| 13 | import javax.swing.text.JTextComponent;
|
|---|
| 14 | import javax.swing.text.PlainDocument;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @author guilhem.bonnefille@gmail.com
|
|---|
| 18 | */
|
|---|
| 19 | public class AutoCompleteComboBox extends JComboBox {
|
|---|
| 20 |
|
|---|
| 21 | private boolean autocompleteEnabled = true;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Auto-complete a JComboBox.
|
|---|
| 25 | *
|
|---|
| 26 | * Inspired by http://www.orbital-computer.de/JComboBox/
|
|---|
| 27 | */
|
|---|
| 28 | class AutoCompleteComboBoxDocument extends PlainDocument {
|
|---|
| 29 | private JComboBox comboBox;
|
|---|
| 30 | private boolean selecting = false;
|
|---|
| 31 |
|
|---|
| 32 | public AutoCompleteComboBoxDocument(final JComboBox comboBox) {
|
|---|
| 33 | this.comboBox = comboBox;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | @Override public void remove(int offs, int len) throws BadLocationException {
|
|---|
| 37 | if (selecting)
|
|---|
| 38 | return;
|
|---|
| 39 | super.remove(offs, len);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
|
|---|
| 43 | if(selecting || (offs == 0 && str.equals(getText(0, getLength()))))
|
|---|
| 44 | return;
|
|---|
| 45 | boolean initial = (offs == 0 && getLength() == 0 && str.length() > 1);
|
|---|
| 46 | super.insertString(offs, str, a);
|
|---|
| 47 |
|
|---|
| 48 | // return immediately when selecting an item
|
|---|
| 49 | // Note: this is done after calling super method because we need
|
|---|
| 50 | // ActionListener informed
|
|---|
| 51 | if (selecting)
|
|---|
| 52 | return;
|
|---|
| 53 | if (!autocompleteEnabled)
|
|---|
| 54 | return;
|
|---|
| 55 |
|
|---|
| 56 | int size = getLength();
|
|---|
| 57 | int start = offs+str.length();
|
|---|
| 58 | int end = start;
|
|---|
| 59 | String curText = getText(0, size);
|
|---|
| 60 | // lookup and select a matching item
|
|---|
| 61 | Object item = lookupItem(curText);
|
|---|
| 62 | setSelectedItem(item);
|
|---|
| 63 | if(initial) {
|
|---|
| 64 | start = 0;
|
|---|
| 65 | }
|
|---|
| 66 | if (item != null) {
|
|---|
| 67 | String newText = item.toString();
|
|---|
| 68 | if(!newText.equals(curText))
|
|---|
| 69 | {
|
|---|
| 70 | selecting = true;
|
|---|
| 71 | super.remove(0, size);
|
|---|
| 72 | super.insertString(0, newText, a);
|
|---|
| 73 | selecting = false;
|
|---|
| 74 | start = size;
|
|---|
| 75 | end = getLength();
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | JTextComponent editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
|
|---|
| 79 | editor.setSelectionStart(start);
|
|---|
| 80 | editor.setSelectionEnd(end);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | private void setSelectedItem(Object item) {
|
|---|
| 84 | selecting = true;
|
|---|
| 85 | comboBox.setSelectedItem(item);
|
|---|
| 86 | selecting = false;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | private Object lookupItem(String pattern) {
|
|---|
| 90 | ComboBoxModel model = comboBox.getModel();
|
|---|
| 91 | for (int i = 0, n = model.getSize(); i < n; i++) {
|
|---|
| 92 | Object currentItem = model.getElementAt(i);
|
|---|
| 93 | if (currentItem.toString().startsWith(pattern))
|
|---|
| 94 | return currentItem;
|
|---|
| 95 | }
|
|---|
| 96 | return null;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public AutoCompleteComboBox() {
|
|---|
| 101 | final JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent();
|
|---|
| 102 | editor.setDocument(new AutoCompleteComboBoxDocument(this));
|
|---|
| 103 | editor.addFocusListener(
|
|---|
| 104 | new FocusListener() {
|
|---|
| 105 | public void focusLost(FocusEvent e) {
|
|---|
| 106 | }
|
|---|
| 107 | public void focusGained(FocusEvent e) {
|
|---|
| 108 | editor.selectAll();
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | public void setPossibleItems(Collection<String> elems) {
|
|---|
| 115 | DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
|
|---|
| 116 | Object oldValue = this.getEditor().getItem();
|
|---|
| 117 | model.removeAllElements();
|
|---|
| 118 | for (String elem : elems) {
|
|---|
| 119 | model.addElement(elem);
|
|---|
| 120 | }
|
|---|
| 121 | this.getEditor().setItem(oldValue);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | protected boolean isAutocompleteEnabled() {
|
|---|
| 125 | return autocompleteEnabled;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | protected void setAutocompleteEnabled(boolean autocompleteEnabled) {
|
|---|
| 129 | this.autocompleteEnabled = autocompleteEnabled;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|