source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java@ 3385

Last change on this file since 3385 was 3385, checked in by jttt, 14 years ago

Fix warnings

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.tagging.ac;
3
4import java.awt.Component;
5import java.awt.event.FocusEvent;
6import java.awt.event.FocusListener;
7import java.util.Collection;
8
9import javax.swing.ComboBoxEditor;
10import javax.swing.ComboBoxModel;
11import javax.swing.DefaultComboBoxModel;
12import javax.swing.JComboBox;
13import javax.swing.JLabel;
14import javax.swing.JList;
15import javax.swing.ListCellRenderer;
16import javax.swing.text.AttributeSet;
17import javax.swing.text.BadLocationException;
18import javax.swing.text.JTextComponent;
19import javax.swing.text.PlainDocument;
20import javax.swing.text.StyleConstants;
21
22import org.openstreetmap.josm.Main;
23
24/**
25 * @author guilhem.bonnefille@gmail.com
26 */
27public 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().startsWith(pattern)) {
122 if (bestItem == null || currentItem.getPriority().compareTo(bestItem.getPriority()) > 0) {
123 bestItem = currentItem;
124 }
125 }
126 }
127 return bestItem; // may be null
128 }
129 }
130
131 public AutoCompletingComboBox() {
132 setRenderer(new AutoCompleteListCellRenderer());
133 final JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent();
134 editor.setDocument(new AutoCompletingComboBoxDocument(this));
135 editor.addFocusListener(
136 new FocusListener() {
137 public void focusLost(FocusEvent e) {
138 }
139 public void focusGained(FocusEvent e) {
140 editor.selectAll();
141 }
142 }
143 );
144 }
145
146 /**
147 * Convert the selected item into a String
148 * that can be edited in the editor component.
149 *
150 * @param editor the editor
151 * @param item excepts AutoCompletionListItem, String and null
152 */
153 @Override public void configureEditor(ComboBoxEditor editor, Object item) {
154 if (item == null) {
155 editor.setItem(null);
156 } else if (item instanceof String) {
157 editor.setItem(item);
158 } else if (item instanceof AutoCompletionListItem) {
159 editor.setItem(((AutoCompletionListItem)item).getValue());
160 } else
161 throw new IllegalArgumentException();
162 }
163
164 /**
165 * Selects a given item in the ComboBox model
166 * @param item excepts AutoCompletionListItem, String and null
167 */
168 @Override public void setSelectedItem(Object item) {
169 if (item == null) {
170 super.setSelectedItem(null);
171 } else if (item instanceof AutoCompletionListItem) {
172 super.setSelectedItem(item);
173 } else if (item instanceof String) {
174 String s = (String) item;
175 // find the string in the model or create a new item
176 for (int i=0; i< getModel().getSize(); i++) {
177 AutoCompletionListItem acItem = (AutoCompletionListItem) getModel().getElementAt(i);
178 if (s.equals(acItem.getValue())) {
179 super.setSelectedItem(acItem);
180 return;
181 }
182 }
183 super.setSelectedItem(new AutoCompletionListItem(s, AutoCompletionItemPritority.UNKNOWN));
184 } else
185 throw new IllegalArgumentException();
186 }
187
188 /**
189 * sets the items of the combobox to the given strings
190 */
191 public void setPossibleItems(Collection<String> elems) {
192 DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
193 Object oldValue = this.getEditor().getItem();
194 model.removeAllElements();
195 for (String elem : elems) {
196 model.addElement(new AutoCompletionListItem(elem, AutoCompletionItemPritority.UNKNOWN));
197 }
198 this.getEditor().setItem(oldValue);
199 }
200
201 /**
202 * sets the items of the combobox to the given AutoCompletionListItems
203 */
204 public void setPossibleACItems(Collection<AutoCompletionListItem> elems) {
205 DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel();
206 Object oldValue = this.getEditor().getItem();
207 model.removeAllElements();
208 for (AutoCompletionListItem elem : elems) {
209 model.addElement(elem);
210 }
211 this.getEditor().setItem(oldValue);
212 }
213
214
215 protected boolean isAutocompleteEnabled() {
216 return autocompleteEnabled;
217 }
218
219 protected void setAutocompleteEnabled(boolean autocompleteEnabled) {
220 this.autocompleteEnabled = autocompleteEnabled;
221 }
222
223 /**
224 * ListCellRenderer for AutoCompletingComboBox
225 * renders an AutoCompletionListItem by showing only the string value part
226 */
227 public static class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer {
228
229 public AutoCompleteListCellRenderer() {
230 setOpaque(true);
231 }
232
233 public Component getListCellRendererComponent(
234 JList list,
235 Object value,
236 int index,
237 boolean isSelected,
238 boolean cellHasFocus)
239 {
240 if (isSelected) {
241 setBackground(list.getSelectionBackground());
242 setForeground(list.getSelectionForeground());
243 } else {
244 setBackground(list.getBackground());
245 setForeground(list.getForeground());
246 }
247
248 AutoCompletionListItem item = (AutoCompletionListItem) value;
249 setText(item.getValue());
250 return this;
251 }
252 }
253}
Note: See TracBrowser for help on using the repository browser.