1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.ac;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.datatransfer.Clipboard;
|
---|
6 | import java.awt.datatransfer.Transferable;
|
---|
7 | import java.awt.event.FocusEvent;
|
---|
8 | import java.awt.event.FocusListener;
|
---|
9 | import java.awt.im.InputContext;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Locale;
|
---|
12 |
|
---|
13 | import javax.swing.ComboBoxEditor;
|
---|
14 | import javax.swing.ComboBoxModel;
|
---|
15 | import javax.swing.DefaultComboBoxModel;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JList;
|
---|
18 | import javax.swing.ListCellRenderer;
|
---|
19 | import javax.swing.text.AttributeSet;
|
---|
20 | import javax.swing.text.BadLocationException;
|
---|
21 | import javax.swing.text.JTextComponent;
|
---|
22 | import javax.swing.text.PlainDocument;
|
---|
23 | import javax.swing.text.StyleConstants;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
28 | import org.openstreetmap.josm.tools.Utils;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Auto-completing ComboBox.
|
---|
32 | * @author guilhem.bonnefille@gmail.com
|
---|
33 | * @since 272
|
---|
34 | */
|
---|
35 | public class AutoCompletingComboBox extends JosmComboBox<AutoCompletionListItem> {
|
---|
36 |
|
---|
37 | private boolean autocompleteEnabled = true;
|
---|
38 |
|
---|
39 | private int maxTextLength = -1;
|
---|
40 | private boolean useFixedLocale;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Auto-complete a JosmComboBox.
|
---|
44 | * <br>
|
---|
45 | * Inspired by <a href="http://www.orbital-computer.de/JComboBox">Thomas Bierhance example</a>.
|
---|
46 | */
|
---|
47 | class AutoCompletingComboBoxDocument extends PlainDocument {
|
---|
48 | private final JosmComboBox<AutoCompletionListItem> comboBox;
|
---|
49 | private boolean selecting;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Constructs a new {@code AutoCompletingComboBoxDocument}.
|
---|
53 | * @param comboBox the combobox
|
---|
54 | */
|
---|
55 | AutoCompletingComboBoxDocument(final JosmComboBox<AutoCompletionListItem> comboBox) {
|
---|
56 | this.comboBox = comboBox;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void remove(int offs, int len) throws BadLocationException {
|
---|
61 | if (selecting)
|
---|
62 | return;
|
---|
63 | super.remove(offs, len);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
|
---|
68 | // TODO get rid of code duplication w.r.t. AutoCompletingTextField.AutoCompletionDocument.insertString
|
---|
69 |
|
---|
70 | if (selecting || (offs == 0 && str.equals(getText(0, getLength()))))
|
---|
71 | return;
|
---|
72 | if (maxTextLength > -1 && str.length()+getLength() > maxTextLength)
|
---|
73 | return;
|
---|
74 | boolean initial = offs == 0 && getLength() == 0 && str.length() > 1;
|
---|
75 | super.insertString(offs, str, a);
|
---|
76 |
|
---|
77 | // return immediately when selecting an item
|
---|
78 | // Note: this is done after calling super method because we need
|
---|
79 | // ActionListener informed
|
---|
80 | if (selecting)
|
---|
81 | return;
|
---|
82 | if (!autocompleteEnabled)
|
---|
83 | return;
|
---|
84 | // input method for non-latin characters (e.g. scim)
|
---|
85 | if (a != null && a.isDefined(StyleConstants.ComposedTextAttribute))
|
---|
86 | return;
|
---|
87 |
|
---|
88 | // if the current offset isn't at the end of the document we don't autocomplete.
|
---|
89 | // If a highlighted autocompleted suffix was present and we get here Swing has
|
---|
90 | // already removed it from the document. getLength() therefore doesn't include the autocompleted suffix.
|
---|
91 | if (offs + str.length() < getLength()) {
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | int size = getLength();
|
---|
96 | int start = offs+str.length();
|
---|
97 | int end = start;
|
---|
98 | String curText = getText(0, size);
|
---|
99 |
|
---|
100 | // item for lookup and selection
|
---|
101 | Object item = null;
|
---|
102 | // if the text is a number we don't autocomplete
|
---|
103 | if (Main.pref.getBoolean("autocomplete.dont_complete_numbers", true)) {
|
---|
104 | try {
|
---|
105 | Long.parseLong(str);
|
---|
106 | if (!curText.isEmpty())
|
---|
107 | Long.parseLong(curText);
|
---|
108 | item = lookupItem(curText, true);
|
---|
109 | } catch (NumberFormatException e) {
|
---|
110 | // either the new text or the current text isn't a number. We continue with autocompletion
|
---|
111 | item = lookupItem(curText, false);
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | item = lookupItem(curText, false);
|
---|
115 | }
|
---|
116 |
|
---|
117 | setSelectedItem(item);
|
---|
118 | if (initial) {
|
---|
119 | start = 0;
|
---|
120 | }
|
---|
121 | if (item != null) {
|
---|
122 | String newText = ((AutoCompletionListItem) item).getValue();
|
---|
123 | if (!newText.equals(curText)) {
|
---|
124 | selecting = true;
|
---|
125 | super.remove(0, size);
|
---|
126 | super.insertString(0, newText, a);
|
---|
127 | selecting = false;
|
---|
128 | start = size;
|
---|
129 | end = getLength();
|
---|
130 | }
|
---|
131 | }
|
---|
132 | final JTextComponent editorComponent = comboBox.getEditorComponent();
|
---|
133 | // save unix system selection (middle mouse paste)
|
---|
134 | Clipboard sysSel = GuiHelper.getSystemSelection();
|
---|
135 | if (sysSel != null) {
|
---|
136 | Transferable old = Utils.getTransferableContent(sysSel);
|
---|
137 | editorComponent.select(start, end);
|
---|
138 | if (old != null) {
|
---|
139 | sysSel.setContents(old, null);
|
---|
140 | }
|
---|
141 | } else {
|
---|
142 | editorComponent.select(start, end);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void setSelectedItem(Object item) {
|
---|
147 | selecting = true;
|
---|
148 | comboBox.setSelectedItem(item);
|
---|
149 | selecting = false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private Object lookupItem(String pattern, boolean match) {
|
---|
153 | ComboBoxModel<AutoCompletionListItem> model = comboBox.getModel();
|
---|
154 | AutoCompletionListItem bestItem = null;
|
---|
155 | for (int i = 0, n = model.getSize(); i < n; i++) {
|
---|
156 | AutoCompletionListItem currentItem = model.getElementAt(i);
|
---|
157 | if (currentItem.getValue().equals(pattern))
|
---|
158 | return currentItem;
|
---|
159 | if (!match && currentItem.getValue().startsWith(pattern)
|
---|
160 | && (bestItem == null || currentItem.getPriority().compareTo(bestItem.getPriority()) > 0)) {
|
---|
161 | bestItem = currentItem;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | return bestItem; // may be null
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Creates a <code>AutoCompletingComboBox</code> with a default prototype display value.
|
---|
170 | */
|
---|
171 | public AutoCompletingComboBox() {
|
---|
172 | this("Foo");
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Creates a <code>AutoCompletingComboBox</code> with the specified prototype display value.
|
---|
177 | * @param prototype the <code>Object</code> used to compute the maximum number of elements to be displayed at once
|
---|
178 | * before displaying a scroll bar. It also affects the initial width of the combo box.
|
---|
179 | * @since 5520
|
---|
180 | */
|
---|
181 | public AutoCompletingComboBox(String prototype) {
|
---|
182 | super(new AutoCompletionListItem(prototype));
|
---|
183 | setRenderer(new AutoCompleteListCellRenderer());
|
---|
184 | final JTextComponent editorComponent = this.getEditorComponent();
|
---|
185 | editorComponent.setDocument(new AutoCompletingComboBoxDocument(this));
|
---|
186 | editorComponent.addFocusListener(
|
---|
187 | new FocusListener() {
|
---|
188 | @Override
|
---|
189 | public void focusLost(FocusEvent e) {
|
---|
190 | if (Main.map != null) {
|
---|
191 | Main.map.keyDetector.setEnabled(true);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public void focusGained(FocusEvent e) {
|
---|
197 | if (Main.map != null) {
|
---|
198 | Main.map.keyDetector.setEnabled(false);
|
---|
199 | }
|
---|
200 | // save unix system selection (middle mouse paste)
|
---|
201 | Clipboard sysSel = GuiHelper.getSystemSelection();
|
---|
202 | if (sysSel != null) {
|
---|
203 | Transferable old = Utils.getTransferableContent(sysSel);
|
---|
204 | editorComponent.selectAll();
|
---|
205 | if (old != null) {
|
---|
206 | sysSel.setContents(old, null);
|
---|
207 | }
|
---|
208 | } else {
|
---|
209 | editorComponent.selectAll();
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | );
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Sets the maximum text length.
|
---|
218 | * @param length the maximum text length in number of characters
|
---|
219 | */
|
---|
220 | public void setMaxTextLength(int length) {
|
---|
221 | this.maxTextLength = length;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Convert the selected item into a String that can be edited in the editor component.
|
---|
226 | *
|
---|
227 | * @param cbEditor the editor
|
---|
228 | * @param item excepts AutoCompletionListItem, String and null
|
---|
229 | */
|
---|
230 | @Override
|
---|
231 | public void configureEditor(ComboBoxEditor cbEditor, Object item) {
|
---|
232 | if (item == null) {
|
---|
233 | cbEditor.setItem(null);
|
---|
234 | } else if (item instanceof String) {
|
---|
235 | cbEditor.setItem(item);
|
---|
236 | } else if (item instanceof AutoCompletionListItem) {
|
---|
237 | cbEditor.setItem(((AutoCompletionListItem) item).getValue());
|
---|
238 | } else
|
---|
239 | throw new IllegalArgumentException("Unsupported item: "+item);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Selects a given item in the ComboBox model
|
---|
244 | * @param item excepts AutoCompletionListItem, String and null
|
---|
245 | */
|
---|
246 | @Override
|
---|
247 | public void setSelectedItem(Object item) {
|
---|
248 | if (item == null) {
|
---|
249 | super.setSelectedItem(null);
|
---|
250 | } else if (item instanceof AutoCompletionListItem) {
|
---|
251 | super.setSelectedItem(item);
|
---|
252 | } else if (item instanceof String) {
|
---|
253 | String s = (String) item;
|
---|
254 | // find the string in the model or create a new item
|
---|
255 | for (int i = 0; i < getModel().getSize(); i++) {
|
---|
256 | AutoCompletionListItem acItem = getModel().getElementAt(i);
|
---|
257 | if (s.equals(acItem.getValue())) {
|
---|
258 | super.setSelectedItem(acItem);
|
---|
259 | return;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | super.setSelectedItem(new AutoCompletionListItem(s, AutoCompletionItemPriority.UNKNOWN));
|
---|
263 | } else {
|
---|
264 | throw new IllegalArgumentException("Unsupported item: "+item);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Sets the items of the combobox to the given {@code String}s.
|
---|
270 | * @param elems String items
|
---|
271 | */
|
---|
272 | public void setPossibleItems(Collection<String> elems) {
|
---|
273 | DefaultComboBoxModel<AutoCompletionListItem> model = (DefaultComboBoxModel<AutoCompletionListItem>) this.getModel();
|
---|
274 | Object oldValue = this.getEditor().getItem(); // Do not use getSelectedItem(); (fix #8013)
|
---|
275 | model.removeAllElements();
|
---|
276 | for (String elem : elems) {
|
---|
277 | model.addElement(new AutoCompletionListItem(elem, AutoCompletionItemPriority.UNKNOWN));
|
---|
278 | }
|
---|
279 | // disable autocomplete to prevent unnecessary actions in AutoCompletingComboBoxDocument#insertString
|
---|
280 | autocompleteEnabled = false;
|
---|
281 | this.getEditor().setItem(oldValue); // Do not use setSelectedItem(oldValue); (fix #8013)
|
---|
282 | autocompleteEnabled = true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Sets the items of the combobox to the given {@code AutoCompletionListItem}s.
|
---|
287 | * @param elems AutoCompletionListItem items
|
---|
288 | */
|
---|
289 | public void setPossibleACItems(Collection<AutoCompletionListItem> elems) {
|
---|
290 | DefaultComboBoxModel<AutoCompletionListItem> model = (DefaultComboBoxModel<AutoCompletionListItem>) this.getModel();
|
---|
291 | Object oldValue = getSelectedItem();
|
---|
292 | Object editorOldValue = this.getEditor().getItem();
|
---|
293 | model.removeAllElements();
|
---|
294 | for (AutoCompletionListItem elem : elems) {
|
---|
295 | model.addElement(elem);
|
---|
296 | }
|
---|
297 | setSelectedItem(oldValue);
|
---|
298 | this.getEditor().setItem(editorOldValue);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Determines if autocompletion is enabled.
|
---|
303 | * @return {@code true} if autocompletion is enabled, {@code false} otherwise.
|
---|
304 | */
|
---|
305 | public final boolean isAutocompleteEnabled() {
|
---|
306 | return autocompleteEnabled;
|
---|
307 | }
|
---|
308 |
|
---|
309 | protected void setAutocompleteEnabled(boolean autocompleteEnabled) {
|
---|
310 | this.autocompleteEnabled = autocompleteEnabled;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * If the locale is fixed, English keyboard layout will be used by default for this combobox
|
---|
315 | * all other components can still have different keyboard layout selected
|
---|
316 | * @param f fixed locale
|
---|
317 | */
|
---|
318 | public void setFixedLocale(boolean f) {
|
---|
319 | useFixedLocale = f;
|
---|
320 | if (useFixedLocale) {
|
---|
321 | Locale oldLocale = privateInputContext.getLocale();
|
---|
322 | Main.info("Using English input method");
|
---|
323 | if (!privateInputContext.selectInputMethod(new Locale("en", "US"))) {
|
---|
324 | // Unable to use English keyboard layout, disable the feature
|
---|
325 | Main.warn("Unable to use English input method");
|
---|
326 | useFixedLocale = false;
|
---|
327 | if (oldLocale != null) {
|
---|
328 | Main.info("Restoring input method to " + oldLocale);
|
---|
329 | if (!privateInputContext.selectInputMethod(oldLocale)) {
|
---|
330 | Main.warn("Unable to restore input method to " + oldLocale);
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | private final InputContext privateInputContext = InputContext.getInstance();
|
---|
338 |
|
---|
339 | @Override
|
---|
340 | public InputContext getInputContext() {
|
---|
341 | if (useFixedLocale) {
|
---|
342 | return privateInputContext;
|
---|
343 | }
|
---|
344 | return super.getInputContext();
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * ListCellRenderer for AutoCompletingComboBox
|
---|
349 | * renders an AutoCompletionListItem by showing only the string value part
|
---|
350 | */
|
---|
351 | public static class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer<AutoCompletionListItem> {
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Constructs a new {@code AutoCompleteListCellRenderer}.
|
---|
355 | */
|
---|
356 | public AutoCompleteListCellRenderer() {
|
---|
357 | setOpaque(true);
|
---|
358 | }
|
---|
359 |
|
---|
360 | @Override
|
---|
361 | public Component getListCellRendererComponent(
|
---|
362 | JList<? extends AutoCompletionListItem> list,
|
---|
363 | AutoCompletionListItem item,
|
---|
364 | int index,
|
---|
365 | boolean isSelected,
|
---|
366 | boolean cellHasFocus) {
|
---|
367 | if (isSelected) {
|
---|
368 | setBackground(list.getSelectionBackground());
|
---|
369 | setForeground(list.getSelectionForeground());
|
---|
370 | } else {
|
---|
371 | setBackground(list.getBackground());
|
---|
372 | setForeground(list.getForeground());
|
---|
373 | }
|
---|
374 |
|
---|
375 | setText(item.getValue());
|
---|
376 | return this;
|
---|
377 | }
|
---|
378 | }
|
---|
379 | }
|
---|