1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.tagging.ac;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.event.FocusEvent;
|
---|
6 | import java.awt.event.FocusListener;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import javax.swing.ComboBoxEditor;
|
---|
10 | import javax.swing.ComboBoxModel;
|
---|
11 | import javax.swing.DefaultComboBoxModel;
|
---|
12 | import javax.swing.JComboBox;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JList;
|
---|
15 | import javax.swing.ListCellRenderer;
|
---|
16 | import javax.swing.text.AttributeSet;
|
---|
17 | import javax.swing.text.BadLocationException;
|
---|
18 | import javax.swing.text.JTextComponent;
|
---|
19 | import javax.swing.text.PlainDocument;
|
---|
20 | import javax.swing.text.StyleConstants;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @author guilhem.bonnefille@gmail.com
|
---|
26 | */
|
---|
27 | public class AutoCompletingComboBox extends JComboBox {
|
---|
28 |
|
---|
29 | private boolean autocompleteEnabled = true;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Auto-complete a JComboBox.
|
---|
33 | *
|
---|
34 | * Inspired by http://www.orbital-computer.de/JComboBox/
|
---|
35 | */
|
---|
36 | class AutoCompletingComboBoxDocument extends PlainDocument {
|
---|
37 | private JComboBox comboBox;
|
---|
38 | private boolean selecting = false;
|
---|
39 |
|
---|
40 | public AutoCompletingComboBoxDocument(final JComboBox comboBox) {
|
---|
41 | this.comboBox = comboBox;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override public void remove(int offs, int len) throws BadLocationException {
|
---|
45 | if (selecting)
|
---|
46 | return;
|
---|
47 | super.remove(offs, len);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
|
---|
51 | if (selecting || (offs == 0 && str.equals(getText(0, getLength()))))
|
---|
52 | return;
|
---|
53 | boolean initial = (offs == 0 && getLength() == 0 && str.length() > 1);
|
---|
54 | super.insertString(offs, str, a);
|
---|
55 |
|
---|
56 | // return immediately when selecting an item
|
---|
57 | // Note: this is done after calling super method because we need
|
---|
58 | // ActionListener informed
|
---|
59 | if (selecting)
|
---|
60 | return;
|
---|
61 | if (!autocompleteEnabled)
|
---|
62 | return;
|
---|
63 | // input method for non-latin characters (e.g. scim)
|
---|
64 | if (a != null && a.isDefined(StyleConstants.ComposedTextAttribute))
|
---|
65 | return;
|
---|
66 |
|
---|
67 | int size = getLength();
|
---|
68 | int start = offs+str.length();
|
---|
69 | int end = start;
|
---|
70 | String curText = getText(0, size);
|
---|
71 |
|
---|
72 | // if the text starts with a number we don't autocomplete
|
---|
73 | if (Main.pref.getBoolean("autocomplete.dont_complete_numbers", true)) {
|
---|
74 | try {
|
---|
75 | Long.parseLong(str);
|
---|
76 | if (curText.length() == 0)
|
---|
77 | // we don't autocomplete on numbers
|
---|
78 | return;
|
---|
79 | Long.parseLong(curText);
|
---|
80 | return;
|
---|
81 | } catch (NumberFormatException e) {
|
---|
82 | // either the new text or the current text isn't a number. We continue with
|
---|
83 | // autocompletion
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | // lookup and select a matching item
|
---|
88 | Object item = lookupItem(curText);
|
---|
89 | setSelectedItem(item);
|
---|
90 | if (initial) {
|
---|
91 | start = 0;
|
---|
92 | }
|
---|
93 | if (item != null) {
|
---|
94 | String newText = ((AutoCompletionListItem) item).getValue();
|
---|
95 | if (!newText.equals(curText))
|
---|
96 | {
|
---|
97 | selecting = true;
|
---|
98 | super.remove(0, size);
|
---|
99 | super.insertString(0, newText, a);
|
---|
100 | selecting = false;
|
---|
101 | start = size;
|
---|
102 | end = getLength();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | JTextComponent editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
|
---|
106 | editor.setSelectionStart(start);
|
---|
107 | editor.setSelectionEnd(end);
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void setSelectedItem(Object item) {
|
---|
111 | selecting = true;
|
---|
112 | comboBox.setSelectedItem(item);
|
---|
113 | selecting = false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private Object lookupItem(String pattern) {
|
---|
117 | ComboBoxModel model = comboBox.getModel();
|
---|
118 | AutoCompletionListItem bestItem = null;
|
---|
119 | for (int i = 0, n = model.getSize(); i < n; i++) {
|
---|
120 | AutoCompletionListItem currentItem = (AutoCompletionListItem) model.getElementAt(i);
|
---|
121 | if (currentItem.getValue().equals(pattern)) {
|
---|
122 | return currentItem;
|
---|
123 | }
|
---|
124 | if (currentItem.getValue().startsWith(pattern)) {
|
---|
125 | if (bestItem == null || currentItem.getPriority().compareTo(bestItem.getPriority()) > 0) {
|
---|
126 | bestItem = currentItem;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | return bestItem; // may be null
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public AutoCompletingComboBox() {
|
---|
135 | setRenderer(new AutoCompleteListCellRenderer());
|
---|
136 | final JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent();
|
---|
137 | editor.setDocument(new AutoCompletingComboBoxDocument(this));
|
---|
138 | editor.addFocusListener(
|
---|
139 | new FocusListener() {
|
---|
140 | public void focusLost(FocusEvent e) {
|
---|
141 | }
|
---|
142 | public void focusGained(FocusEvent e) {
|
---|
143 | editor.selectAll();
|
---|
144 | }
|
---|
145 | }
|
---|
146 | );
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Convert the selected item into a String
|
---|
151 | * that can be edited in the editor component.
|
---|
152 | *
|
---|
153 | * @param editor the editor
|
---|
154 | * @param item excepts AutoCompletionListItem, String and null
|
---|
155 | */
|
---|
156 | @Override public void configureEditor(ComboBoxEditor editor, Object item) {
|
---|
157 | if (item == null) {
|
---|
158 | editor.setItem(null);
|
---|
159 | } else if (item instanceof String) {
|
---|
160 | editor.setItem(item);
|
---|
161 | } else if (item instanceof AutoCompletionListItem) {
|
---|
162 | editor.setItem(((AutoCompletionListItem)item).getValue());
|
---|
163 | } else
|
---|
164 | throw new IllegalArgumentException();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Selects a given item in the ComboBox model
|
---|
169 | * @param item excepts AutoCompletionListItem, String and null
|
---|
170 | */
|
---|
171 | @Override public void setSelectedItem(Object item) {
|
---|
172 | if (item == null) {
|
---|
173 | super.setSelectedItem(null);
|
---|
174 | } else if (item instanceof AutoCompletionListItem) {
|
---|
175 | super.setSelectedItem(item);
|
---|
176 | } else if (item instanceof String) {
|
---|
177 | String s = (String) item;
|
---|
178 | // find the string in the model or create a new item
|
---|
179 | for (int i=0; i< getModel().getSize(); i++) {
|
---|
180 | AutoCompletionListItem acItem = (AutoCompletionListItem) getModel().getElementAt(i);
|
---|
181 | if (s.equals(acItem.getValue())) {
|
---|
182 | super.setSelectedItem(acItem);
|
---|
183 | return;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | super.setSelectedItem(new AutoCompletionListItem(s, AutoCompletionItemPritority.UNKNOWN));
|
---|
187 | } else
|
---|
188 | throw new IllegalArgumentException();
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * sets the items of the combobox to the given strings
|
---|
193 | */
|
---|
194 | public void setPossibleItems(Collection<String> elems) {
|
---|
195 | DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
|
---|
196 | Object oldValue = this.getEditor().getItem();
|
---|
197 | model.removeAllElements();
|
---|
198 | for (String elem : elems) {
|
---|
199 | model.addElement(new AutoCompletionListItem(elem, AutoCompletionItemPritority.UNKNOWN));
|
---|
200 | }
|
---|
201 | this.getEditor().setItem(oldValue);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * sets the items of the combobox to the given AutoCompletionListItems
|
---|
206 | */
|
---|
207 | public void setPossibleACItems(Collection<AutoCompletionListItem> elems) {
|
---|
208 | DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
|
---|
209 | Object oldValue = this.getEditor().getItem();
|
---|
210 | model.removeAllElements();
|
---|
211 | for (AutoCompletionListItem elem : elems) {
|
---|
212 | model.addElement(elem);
|
---|
213 | }
|
---|
214 | this.getEditor().setItem(oldValue);
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | protected boolean isAutocompleteEnabled() {
|
---|
219 | return autocompleteEnabled;
|
---|
220 | }
|
---|
221 |
|
---|
222 | protected void setAutocompleteEnabled(boolean autocompleteEnabled) {
|
---|
223 | this.autocompleteEnabled = autocompleteEnabled;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * ListCellRenderer for AutoCompletingComboBox
|
---|
228 | * renders an AutoCompletionListItem by showing only the string value part
|
---|
229 | */
|
---|
230 | public static class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer {
|
---|
231 |
|
---|
232 | public AutoCompleteListCellRenderer() {
|
---|
233 | setOpaque(true);
|
---|
234 | }
|
---|
235 |
|
---|
236 | public Component getListCellRendererComponent(
|
---|
237 | JList list,
|
---|
238 | Object value,
|
---|
239 | int index,
|
---|
240 | boolean isSelected,
|
---|
241 | boolean cellHasFocus)
|
---|
242 | {
|
---|
243 | if (isSelected) {
|
---|
244 | setBackground(list.getSelectionBackground());
|
---|
245 | setForeground(list.getSelectionForeground());
|
---|
246 | } else {
|
---|
247 | setBackground(list.getBackground());
|
---|
248 | setForeground(list.getForeground());
|
---|
249 | }
|
---|
250 |
|
---|
251 | AutoCompletionListItem item = (AutoCompletionListItem) value;
|
---|
252 | setText(item.getValue());
|
---|
253 | return this;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|