如何使用jfce AutoCompleteField从字符串中搜索任何关键字

时间:2017-06-13 09:26:07

标签: swt jface

我有一个swt文本,我写的是" new AutoCompleteField(textSearch,new TextContentProvider(),searchList);它工作,但它发现字符串以表达式开头。我想创建自己的提案提供者,如果我的字符串包含任何关键字,我可以写一些东西,我应该获得autoComplete弹出窗口。

1 个答案:

答案 0 :(得分:0)

由于您需要更改内容提案提供商,因此您无法使用现有的AutoCompleteField

合适的IContentProposalProvider类似于:

public class AnyPositionContentProposalProvider implements IContentProposalProvider
{
  private final String [] proposals;

  public AnyPositionContentProposalProvider(String [] theProposals)
  {
    proposals = theProposals;
  }

  @Override
  public IContentProposal [] getProposals(String contents, int position)
  {
    List<IContentProposal> result = new ArrayList<>();

    for (String proposal : proposals) {
       if (proposal.contains(contents)) {
         result.add(new ContentProposal(proposal));
       }
    }

    return result.toArray(new IContentProposal [result.size()]);
  }
}

以下方法将此设置为AutoCompleteField

// Installs on a Text control
public static void installAnyPositionMatch(Text control, String [] proposals)
{
  installAnyPositionMatch(control, new TextContentAdapter(), proposals);
}

// Install on any control with a content adapter
public static void installAnyPositionMatch(Control control, IControlContentAdapter controlContentAdapter, String [] proposals)
{
  IContentProposalProvider proposalProvider = new AnyPositionContentProposalProvider(proposals);

  ContentProposalAdapter adapter = new ContentProposalAdapter(control, controlContentAdapter, proposalProvider, null, null);
  adapter.setPropagateKeys(true);
  adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}