source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/AutoCompleteComboBox.java@ 2231

Last change on this file since 2231 was 2231, checked in by Gubaer, 15 years ago

Make sure the changeset comment field has focus and is selected when the upload dialog is launched

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