source: josm/trunk/src/org/openstreetmap/josm/tools/AutoCompleteComboBox.java@ 627

Last change on this file since 627 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.util.Collection;
5
6import javax.swing.ComboBoxModel;
7import javax.swing.DefaultComboBoxModel;
8import javax.swing.JComboBox;
9import javax.swing.text.AttributeSet;
10import javax.swing.text.BadLocationException;
11import javax.swing.text.JTextComponent;
12import javax.swing.text.PlainDocument;
13
14/**
15 * @author guilhem.bonnefille@gmail.com
16 */
17public 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 super.insertString(offs, str, a);
40
41 // return immediately when selecting an item
42 // Nota: this is done after calling super method because we need
43 // ActionListener informed
44 if (selecting)
45 return;
46
47 // lookup and select a matching item
48 Object item = lookupItem(getText(0, getLength()));
49 if (item != null) {
50 // remove all text and insert the completed string
51 super.remove(0, getLength());
52 super.insertString(0, item.toString(), a);
53
54 }
55
56 setSelectedItem(item);
57
58 // select the completed part
59 JTextComponent editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
60 editor.setSelectionStart(offs + str.length());
61 editor.setSelectionEnd(getLength());
62 }
63
64 private void setSelectedItem(Object item) {
65 selecting = true;
66 comboBox.setSelectedItem(item);
67 selecting = false;
68 }
69
70 private Object lookupItem(String pattern) {
71 ComboBoxModel model = comboBox.getModel();
72 for (int i = 0, n = model.getSize(); i < n; i++) {
73 Object currentItem = model.getElementAt(i);
74 if (currentItem.toString().startsWith(pattern))
75 return currentItem;
76 }
77 return null;
78 }
79 }
80
81 public AutoCompleteComboBox() {
82 JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent();
83 editor.setDocument(new AutoCompleteComboBoxDocument(this));
84 }
85
86 public void setPossibleItems(Collection<String> elems) {
87 DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
88 Object oldValue = this.getEditor().getItem();
89 model.removeAllElements();
90 for (String elem : elems) model.addElement(elem);
91 this.getEditor().setItem(oldValue);
92 this.getEditor().selectAll();
93 }
94}
Note: See TracBrowser for help on using the repository browser.