|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2003, 2018 IBM Corporation and others. |
| 2 | + * Copyright (c) 2003, 2025 IBM Corporation and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.e4.ui.internal.workbench.renderers.swt; |
15 | 15 |
|
| 16 | +import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 17 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
16 | 18 | import org.eclipse.e4.ui.workbench.swt.internal.copy.SearchPattern; |
17 | 19 | import org.eclipse.e4.ui.workbench.swt.internal.copy.WorkbenchSWTMessages; |
18 | 20 | import org.eclipse.jface.preference.JFacePreferences; |
|
25 | 27 | import org.eclipse.jface.viewers.Viewer; |
26 | 28 | import org.eclipse.jface.viewers.ViewerFilter; |
27 | 29 | import org.eclipse.swt.SWT; |
| 30 | +import org.eclipse.swt.events.KeyAdapter; |
| 31 | +import org.eclipse.swt.events.KeyEvent; |
28 | 32 | import org.eclipse.swt.events.KeyListener; |
29 | 33 | import org.eclipse.swt.events.MouseEvent; |
30 | 34 | import org.eclipse.swt.events.MouseListener; |
@@ -312,6 +316,19 @@ public TableViewer getTableViewer() { |
312 | 316 | return fTableViewer; |
313 | 317 | } |
314 | 318 |
|
| 319 | + private String suggestCompletionFromTable(String Text) { |
| 320 | + if (Text == null || Text.isEmpty()) { |
| 321 | + return null; |
| 322 | + } |
| 323 | + for (TableItem item : fTableViewer.getTable().getItems()) { |
| 324 | + String textTable = item.getText(); |
| 325 | + if (textTable.toLowerCase().startsWith(Text.toLowerCase())) { |
| 326 | + return textTable; |
| 327 | + } |
| 328 | + } |
| 329 | + return null; |
| 330 | + } |
| 331 | + |
315 | 332 | protected Text createFilterText(Composite parent) { |
316 | 333 | fFilterText = new Text(parent, SWT.NONE); |
317 | 334 |
|
@@ -371,16 +388,54 @@ private void setInfoSystemColor() { |
371 | 388 | setBackgroundColor(background); |
372 | 389 | } |
373 | 390 |
|
374 | | - private void installFilter() { |
| 391 | + private void chevronAutocompleteBehavior() { |
375 | 392 | fFilterText.setMessage(WorkbenchSWTMessages.FilteredTree_FilterMessage); |
376 | 393 | fFilterText.setText(""); //$NON-NLS-1$ |
| 394 | + setMatcherString(""); //$NON-NLS-1$ |
| 395 | + fTableViewer.refresh(); |
| 396 | + |
| 397 | + fFilterText.addKeyListener(new KeyAdapter() { |
| 398 | + @Override |
| 399 | + public void keyReleased(KeyEvent e) { |
| 400 | + int cursor = fFilterText.getSelection().x; |
| 401 | + String input = fFilterText.getText().substring(0, cursor); |
| 402 | + setMatcherString(input); |
| 403 | + fTableViewer.refresh(); |
| 404 | + |
| 405 | + if (e.keyCode == SWT.BS || e.keyCode == SWT.DEL || !Character.isLetterOrDigit(e.character)) |
| 406 | + return; |
| 407 | + |
| 408 | + String suggestion = suggestCompletionFromTable(input); |
| 409 | + if (suggestion != null && suggestion.length() > input.length()) { |
| 410 | + fFilterText.setText(suggestion); |
| 411 | + fFilterText.setSelection(input.length(), suggestion.length()); |
| 412 | + } |
| 413 | + } |
| 414 | + }); |
| 415 | + } |
377 | 416 |
|
| 417 | + private void normalFilterBehavior() { |
| 418 | + fFilterText.setMessage(WorkbenchSWTMessages.FilteredTree_FilterMessage); |
| 419 | + fFilterText.setText(""); //$NON-NLS-1$ |
378 | 420 | fFilterText.addModifyListener(e -> { |
379 | 421 | String text = ((Text) e.widget).getText(); |
380 | 422 | setMatcherString(text); |
| 423 | + fTableViewer.refresh(); |
381 | 424 | }); |
382 | 425 | } |
383 | 426 |
|
| 427 | + private void installFilter() { |
| 428 | + if (isAutocompleteEnabled()) { |
| 429 | + chevronAutocompleteBehavior(); |
| 430 | + } else { |
| 431 | + normalFilterBehavior(); |
| 432 | + } |
| 433 | + } |
| 434 | + |
| 435 | + private boolean isAutocompleteEnabled() { |
| 436 | + IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("org.eclipse.ui.workbench"); //$NON-NLS-1$ |
| 437 | + return prefs.getBoolean("ENABLE_AUTOCOMPLETE_IN_CHEVRON", false); //$NON-NLS-1$ |
| 438 | + } |
384 | 439 | /** |
385 | 440 | * The string matcher has been modified. The default implementation |
386 | 441 | * refreshes the view and selects the first matched element |
|
0 commit comments