你只需要一個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();
}
}