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.event.FocusAdapter;
|
---|
6 | import java.awt.event.FocusEvent;
|
---|
7 | import java.awt.event.KeyAdapter;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.EventObject;
|
---|
10 | import java.util.Objects;
|
---|
11 |
|
---|
12 | import javax.swing.ComboBoxEditor;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.event.CellEditorListener;
|
---|
15 | import javax.swing.table.TableCellEditor;
|
---|
16 | import javax.swing.text.AttributeSet;
|
---|
17 | import javax.swing.text.BadLocationException;
|
---|
18 | import javax.swing.text.Document;
|
---|
19 | import javax.swing.text.PlainDocument;
|
---|
20 | import javax.swing.text.StyleConstants;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.gui.util.CellEditorSupport;
|
---|
24 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * AutoCompletingTextField is a text field with autocompletion behaviour. It
|
---|
28 | * can be used as table cell editor in {@link JTable}s.
|
---|
29 | *
|
---|
30 | * Autocompletion is controlled by a list of {@link AutoCompletionListItem}s
|
---|
31 | * managed in a {@link AutoCompletionList}.
|
---|
32 | *
|
---|
33 | * @since 1762
|
---|
34 | */
|
---|
35 | public class AutoCompletingTextField extends JosmTextField implements ComboBoxEditor, TableCellEditor {
|
---|
36 |
|
---|
37 | private Integer maxChars;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The document model for the editor
|
---|
41 | */
|
---|
42 | class AutoCompletionDocument extends PlainDocument {
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
|
---|
46 |
|
---|
47 | // If a maximum number of characters is specified, avoid to exceed it
|
---|
48 | if (maxChars != null && str != null && getLength() + str.length() > maxChars) {
|
---|
49 | int allowedLength = maxChars-getLength();
|
---|
50 | if (allowedLength > 0) {
|
---|
51 | str = str.substring(0, allowedLength);
|
---|
52 | } else {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (autoCompletionList == null) {
|
---|
58 | super.insertString(offs, str, a);
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // input method for non-latin characters (e.g. scim)
|
---|
63 | if (a != null && a.isDefined(StyleConstants.ComposedTextAttribute)) {
|
---|
64 | super.insertString(offs, str, a);
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // if the current offset isn't at the end of the document we don't autocomplete.
|
---|
69 | // If a highlighted autocompleted suffix was present and we get here Swing has
|
---|
70 | // already removed it from the document. getLength() therefore doesn't include the
|
---|
71 | // autocompleted suffix.
|
---|
72 | //
|
---|
73 | if (offs < getLength()) {
|
---|
74 | super.insertString(offs, str, a);
|
---|
75 | return;
|
---|
76 | }
|
---|
77 |
|
---|
78 | String currentText = getText(0, getLength());
|
---|
79 | // if the text starts with a number we don't autocomplete
|
---|
80 | if (Main.pref.getBoolean("autocomplete.dont_complete_numbers", true)) {
|
---|
81 | try {
|
---|
82 | Long.parseLong(str);
|
---|
83 | if (currentText.isEmpty()) {
|
---|
84 | // we don't autocomplete on numbers
|
---|
85 | super.insertString(offs, str, a);
|
---|
86 | return;
|
---|
87 | }
|
---|
88 | Long.parseLong(currentText);
|
---|
89 | super.insertString(offs, str, a);
|
---|
90 | return;
|
---|
91 | } catch (NumberFormatException e) {
|
---|
92 | // either the new text or the current text isn't a number. We continue with autocompletion
|
---|
93 | if (Main.isTraceEnabled()) {
|
---|
94 | Main.trace(e.getMessage());
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | String prefix = currentText.substring(0, offs);
|
---|
99 | autoCompletionList.applyFilter(prefix+str);
|
---|
100 | if (autoCompletionList.getFilteredSize() > 0 && !Objects.equals(str, noAutoCompletionString)) {
|
---|
101 | // there are matches. Insert the new text and highlight the auto completed suffix
|
---|
102 | String matchingString = autoCompletionList.getFilteredItem(0).getValue();
|
---|
103 | remove(0, getLength());
|
---|
104 | super.insertString(0, matchingString, a);
|
---|
105 |
|
---|
106 | // highlight from insert position to end position to put the caret at the end
|
---|
107 | setCaretPosition(offs + str.length());
|
---|
108 | moveCaretPosition(getLength());
|
---|
109 | } else {
|
---|
110 | // there are no matches. Insert the new text, do not highlight
|
---|
111 | //
|
---|
112 | String newText = prefix + str;
|
---|
113 | remove(0, getLength());
|
---|
114 | super.insertString(0, newText, a);
|
---|
115 | setCaretPosition(getLength());
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** the auto completion list user input is matched against */
|
---|
121 | protected AutoCompletionList autoCompletionList = null;
|
---|
122 | /** a string which should not be auto completed */
|
---|
123 | protected String noAutoCompletionString = null;
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | protected Document createDefaultModel() {
|
---|
127 | return new AutoCompletionDocument();
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected final void init() {
|
---|
131 | addFocusListener(
|
---|
132 | new FocusAdapter() {
|
---|
133 | @Override public void focusGained(FocusEvent e) {
|
---|
134 | selectAll();
|
---|
135 | applyFilter(getText());
|
---|
136 | }
|
---|
137 | }
|
---|
138 | );
|
---|
139 |
|
---|
140 | addKeyListener(
|
---|
141 | new KeyAdapter() {
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void keyReleased(KeyEvent e) {
|
---|
145 | if (getText().isEmpty()) {
|
---|
146 | applyFilter("");
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | );
|
---|
151 | tableCellEditorSupport = new CellEditorSupport(this);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Constructs a new {@code AutoCompletingTextField}.
|
---|
156 | */
|
---|
157 | public AutoCompletingTextField() {
|
---|
158 | this(0);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Constructs a new {@code AutoCompletingTextField}.
|
---|
163 | * @param columns the number of columns to use to calculate the preferred width;
|
---|
164 | * if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
|
---|
165 | */
|
---|
166 | public AutoCompletingTextField(int columns) {
|
---|
167 | this(columns, true);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Constructs a new {@code AutoCompletingTextField}.
|
---|
172 | * @param columns the number of columns to use to calculate the preferred width;
|
---|
173 | * if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
|
---|
174 | * @param undoRedo Enables or not Undo/Redo feature. Not recommended for table cell editors, unless each cell provides its own editor
|
---|
175 | */
|
---|
176 | public AutoCompletingTextField(int columns, boolean undoRedo) {
|
---|
177 | super(null, null, columns, undoRedo);
|
---|
178 | init();
|
---|
179 | }
|
---|
180 |
|
---|
181 | protected void applyFilter(String filter) {
|
---|
182 | if (autoCompletionList != null) {
|
---|
183 | autoCompletionList.applyFilter(filter);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Returns the auto completion list.
|
---|
189 | * @return the auto completion list; may be null, if no auto completion list is set
|
---|
190 | */
|
---|
191 | public AutoCompletionList getAutoCompletionList() {
|
---|
192 | return autoCompletionList;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Sets the auto completion list.
|
---|
197 | * @param autoCompletionList the auto completion list; if null, auto completion is
|
---|
198 | * disabled
|
---|
199 | */
|
---|
200 | public void setAutoCompletionList(AutoCompletionList autoCompletionList) {
|
---|
201 | this.autoCompletionList = autoCompletionList;
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public Component getEditorComponent() {
|
---|
206 | return this;
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public Object getItem() {
|
---|
211 | return getText();
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public void setItem(Object anObject) {
|
---|
216 | if (anObject == null) {
|
---|
217 | setText("");
|
---|
218 | } else {
|
---|
219 | setText(anObject.toString());
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public void setText(String t) {
|
---|
225 | // disallow auto completion for this explicitly set string
|
---|
226 | this.noAutoCompletionString = t;
|
---|
227 | super.setText(t);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Sets the maximum number of characters allowed.
|
---|
232 | * @param max maximum number of characters allowed
|
---|
233 | * @since 5579
|
---|
234 | */
|
---|
235 | public void setMaxChars(Integer max) {
|
---|
236 | maxChars = max;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* ------------------------------------------------------------------------------------ */
|
---|
240 | /* TableCellEditor interface */
|
---|
241 | /* ------------------------------------------------------------------------------------ */
|
---|
242 |
|
---|
243 | private transient CellEditorSupport tableCellEditorSupport;
|
---|
244 | private String originalValue;
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public void addCellEditorListener(CellEditorListener l) {
|
---|
248 | tableCellEditorSupport.addCellEditorListener(l);
|
---|
249 | }
|
---|
250 |
|
---|
251 | protected void rememberOriginalValue(String value) {
|
---|
252 | this.originalValue = value;
|
---|
253 | }
|
---|
254 |
|
---|
255 | protected void restoreOriginalValue() {
|
---|
256 | setText(originalValue);
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public void removeCellEditorListener(CellEditorListener l) {
|
---|
261 | tableCellEditorSupport.removeCellEditorListener(l);
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public void cancelCellEditing() {
|
---|
266 | restoreOriginalValue();
|
---|
267 | tableCellEditorSupport.fireEditingCanceled();
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public Object getCellEditorValue() {
|
---|
272 | return getText();
|
---|
273 | }
|
---|
274 |
|
---|
275 | @Override
|
---|
276 | public boolean isCellEditable(EventObject anEvent) {
|
---|
277 | return true;
|
---|
278 | }
|
---|
279 |
|
---|
280 | @Override
|
---|
281 | public boolean shouldSelectCell(EventObject anEvent) {
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public boolean stopCellEditing() {
|
---|
287 | tableCellEditorSupport.fireEditingStopped();
|
---|
288 | return true;
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
293 | setText(value == null ? "" : value.toString());
|
---|
294 | rememberOriginalValue(getText());
|
---|
295 | return this;
|
---|
296 | }
|
---|
297 | }
|
---|