Search in sources :

Example 11 with IContentProposalProvider

use of org.eclipse.jface.fieldassist.IContentProposalProvider in project egit by eclipse.

the class UIUtils method addContentProposalToText.

/**
 * Adds a content proposal for arbitrary elements to a text field.
 *
 * @param <T>
 *            type of the proposal candidate objects
 *
 * @param textField
 *            the text field
 * @param candidateProvider
 *            {@link IContentProposalCandidateProvider} providing the
 *            candidates eligible for creating {@link IContentProposal}s
 * @param factory
 *            {@link IContentProposalFactory} to use to create proposals
 *            from candidates
 * @param patternProvider
 *            to convert the current text of the field into a pattern
 *            suitable for filtering the candidates. If {@code null}, a
 *            default pattern is constructed using
 *            {@link #createProposalPattern(String)}.
 * @param startTypingMessage
 *            hover message if no content assist key binding is active
 * @param shortcutMessage
 *            hover message if a content assist key binding is active,
 *            should have a "{0}" placeholder that will be filled by the
 *            appropriate keystroke
 * @return the content proposal adapter set on the {@code textField}
 */
public static final <T> ExplicitContentProposalAdapter addContentProposalToText(Text textField, IContentProposalCandidateProvider<T> candidateProvider, IContentProposalFactory<T> factory, Function<String, Pattern> patternProvider, String startTypingMessage, String shortcutMessage) {
    KeyStroke stroke = UIUtils.getKeystrokeOfBestActiveBindingFor(IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST);
    if (stroke == null) {
        if (startTypingMessage == null) {
            return null;
        }
        addBulbDecorator(textField, startTypingMessage);
    } else {
        addBulbDecorator(textField, NLS.bind(shortcutMessage, stroke.format()));
    }
    IContentProposalProvider cp = new IContentProposalProvider() {

        @Override
        public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<>();
            Collection<? extends T> candidates = candidateProvider.getCandidates();
            if (candidates == null) {
                return null;
            }
            Pattern pattern = patternProvider != null ? patternProvider.apply(contents) : createProposalPattern(contents);
            for (final T candidate : candidates) {
                IContentProposal proposal = factory.getProposal(pattern, candidate);
                if (proposal != null) {
                    resultList.add(proposal);
                }
            }
            return resultList.toArray(new IContentProposal[resultList.size()]);
        }
    };
    ExplicitContentProposalAdapter adapter = new ExplicitContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke, UIUtils.VALUE_HELP_ACTIVATIONCHARS);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
    return adapter;
}
Also used : IContentProposalProvider(org.eclipse.jface.fieldassist.IContentProposalProvider) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) Pattern(java.util.regex.Pattern) SWT(org.eclipse.swt.SWT) KeyStroke(org.eclipse.jface.bindings.keys.KeyStroke) ArrayList(java.util.ArrayList) TextContentAdapter(org.eclipse.jface.fieldassist.TextContentAdapter)

Aggregations

IContentProposalProvider (org.eclipse.jface.fieldassist.IContentProposalProvider)11 ArrayList (java.util.ArrayList)5 IContentProposal (org.eclipse.jface.fieldassist.IContentProposal)5 KeyStroke (org.eclipse.jface.bindings.keys.KeyStroke)4 Pattern (java.util.regex.Pattern)2 IControlContentAdapter (org.eclipse.jface.fieldassist.IControlContentAdapter)2 TextContentAdapter (org.eclipse.jface.fieldassist.TextContentAdapter)2 Point (org.eclipse.swt.graphics.Point)2 StyledTextContentAdapter (org.talend.commons.ui.swt.proposal.StyledTextContentAdapter)2 TalendProposalProvider (org.talend.core.ui.proposal.TalendProposalProvider)2 IDataMapTable (org.talend.designer.abstractmap.model.table.IDataMapTable)2 IColumnEntry (org.talend.designer.abstractmap.model.tableentry.IColumnEntry)2 ExpressionProposalProvider (org.talend.designer.xmlmap.parts.directedit.proposal.ExpressionProposalProvider)2 SQLEditorLabelProvider (org.talend.sqlbuilder.ui.proposal.SQLEditorLabelProvider)2 SQLEditorProposalAdapter (org.talend.sqlbuilder.ui.proposal.SQLEditorProposalAdapter)2 SQLEditorProposalProvider (org.talend.sqlbuilder.ui.proposal.SQLEditorProposalProvider)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)1 ContentProposalAdapter (org.eclipse.jface.fieldassist.ContentProposalAdapter)1 SWT (org.eclipse.swt.SWT)1 ContentProposalAdapterExtended (org.talend.commons.ui.swt.proposal.ContentProposalAdapterExtended)1