如何在Eclipse中创建AutoCompleteField就像重构对话框一样?

时间:2012-07-04 02:34:54

标签: autocomplete eclipse-plugin swt jface

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

我想编写一个SWT Text字段(或Combo),就像“Change Method Signature”重构对话框页面中的“Return type”字段一样。我想拥有与此字段相同的自动完成行为。

我知道我可以使用AutoCompleteField和TextContentAdapter,但我不知道如何使用项目类路径中的所有类设置proposal数组。那个类名左边的那些图像装饰怎么样?

我很感激任何帮助。

1 个答案:

答案 0 :(得分: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();
   }
}