2012-07-04 75 views
1

此問題與Eclipse插件開發有關。如何在Eclipse中創建一個AutoCompleteField,就像重構對話框一樣?

我想編寫一個SWT文本字段(或組合),就像在「更改方法簽名」重構對話框頁面中的「返回類型」字段。我想要具有與此字段相同的自動完成行爲。

我知道我可以使用AutoCompleteField和TextContentAdapter,但我不知道如何設置項目類路徑中的所有類的提議數組。那些在類名左邊的圖片裝飾呢?

我很感激任何幫助。

回答

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(); 
    } 
} 
相關問題