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