Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextCellEditor with autocomplete in Eclipse SWT/JFace?

I'd like a TextCellEditor with the standard auto-complete behaviour, that that any user nowadays expects when typing inside an input cell with a list of suggested strings. For a good working example of what I'm after, in Javascript, see this jQuery autocomplete widget.

I couldn't find a good example. I only found (aside from some tiny variations) this TextCellEditorWithContentProposal snippet. But that leaves a lot to be desired:

  • It lists all the words, irrespective of the "partial word" typed in the cell (no partial matching)
  • When the desired word is selected, it is appended to the partial word, instead of replacing it
  • The interaction is ugly and non-intuitive. For example, one would expect the Escape key to tear off the list of suggestions; again, see the Javascript example; here, it also removes the typed letters.

It looks strange to me that such an standard and useful component is not available. Or perhaps it is available? Can someone point to me to a more apt snippet or example?

like image 465
leonbloy Avatar asked Dec 10 '17 20:12

leonbloy


1 Answers

The example you are linking to is a code snippet intended to showcase the API and guide you toward customizing the control to your preference.

Some of your complaints are either invalid or can easily be fixed using public API.

Let's go through them in detail.

All proposals are listed, irrespective of typed text

Note that in the snippet an org.eclipse.jface.fieldassist.SimpleContentProposalProvider is used:

IContentProposalProvider contentProposalProvider = new SimpleContentProposalProvider(new String[] { "red",
                    "green", "blue" });
cellEditor = new TextCellEditorWithContentProposal(viewer.getTable(), contentProposalProvider, null, null);

As suggested in its javadoc it is:

designed to map a static list of Strings to content proposals

To enable a simple filtering of the contents for the snippet, you could call: contentProposalProvider.setFiltering(true);

For anything more complex you will have to replace this with your own implementation of org.eclipse.jface.fieldassist.IContentProposalProvider.

Selection is appended to cell contents, instead of replacing it

The content proposal behavior is defined in the org.eclipse.jface.fieldassist.ContentProposalAdapter. Again a simple method call to org.eclipse.jface.fieldassist.ContentProposalAdapter.setProposalAcceptanceStyle(int) will achieve your target behavior:

contentProposalAdapter = new ContentProposalAdapter(text, new TextContentAdapter(), contentProposalProvider, keyStroke, autoActivationCharacters);
contentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

Cancelling the proposal should not remove typed content

This is hard to do using just the API, since the ContentProposalAdapter does only propagate the key strokes to the opened ContentProposalPopup without storing them.

You would have to subclass ContentProposalAdapter, in order to have access to ContentProposalAdapter.ContentProposalPopup.filterText.


Most of the functionality in this snippet with sensible defaults can also be obtained in a more simple way by using an org.eclipse.jface.fieldassist.AutoCompleteField.

like image 83
tkotisis Avatar answered Sep 29 '22 16:09

tkotisis