1

这个问题与Eclipse插件开发有关。

我想编写一个 SWT 文本字段(或 Combo),就像“更改方法签名”重构对话框页面中的“返回类型”字段一样。我希望拥有与该字段相同的自动完成行为。

我知道我可以使用 AutoCompleteField 和 TextContentAdapter,但我不知道如何使用项目类路径中的所有类设置提案数组。类名左侧的那些图像装饰呢?

我会很感激任何帮助。

4

1 回答 1

0

您是否只需要一个 ContentProposals 示例,或者您想知道如何获取 classPath 中的所有类名?首先我可以帮助你。希望这段代码有帮助,如果您有问题,请提出。

package com.porsche.dps.core;

import org.apache.log4j.Logger;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.fieldassist.ContentProposal;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ContentProposalsTest {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setLayout(new GridLayout(1, false));

      Text text = new Text(shell, SWT.BORDER);

      String[] proposals = new String[] {"abc", "bcd", "cde", "abc2", "bcd2", "cde2"};
      String autoActivationCharacters = "";
      SimpleContentProposalProvider simpleContentProposalProvider = new     SimpleContentProposalProvider(proposals);

      for (int i = 0; i < proposals.length; i++) {

         if (proposals[i].length() == 0)
            continue;

         char c = proposals[i].charAt(0);

         if (autoActivationCharacters.indexOf(c) == -1) {
            autoActivationCharacters += c;
         }
      }

      KeyStroke keyStroke = null;
      simpleContentProposalProvider.setFiltering(true);
      try {
         keyStroke = KeyStroke.getInstance("Ctrl+Space");
      } catch (ParseException e) {
         Logger.getLogger("").error("KeyStroke Parse Exception");
      }
      ContentProposalAdapter proposalAdapter = new ContentProposalAdapter(text, new TextContentAdapter(),
     simpleContentProposalProvider, keyStroke, autoActivationCharacters.toCharArray());

          proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
      proposalAdapter.setLabelProvider(new ILabelProvider() {

         @Override
         public void removeListener(ILabelProviderListener listener) {}

         @Override
         public boolean isLabelProperty(Object element, String property) {
            return false;
         }

         @Override
         public void dispose() {}

         @Override
         public void addListener(ILabelProviderListener listener) {}

         @Override
         public String getText(Object element) {
            return ((ContentProposal)element).getLabel();
         }

         @Override
         public Image getImage(Object element) {
            return <insert image>;
         }
      });
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}
于 2012-09-26T13:01:55.820 回答