前回に続いて実験。 (1) eclipseでコード書いているとctrl + spaceでコードアシストしてくれるので、ためしみました。 キーストロークで"Ctrl+Space"するとよいようです。 (2) 入力された値によって提案するもんもんを変更したい場合。 org.eclipse.jface.fieldassist.IContentProposalProviderを工夫すればよい感じ。 もっといいコードの書き方があるかも。 これが実現できると、入力している語の続きの文字を提案できるから入力を軽減できますね。 実行はこんな感じです。 コードです。
import org.eclipse.jface.bindings.keys.KeyStroke; import org.eclipse.jface.bindings.keys.ParseException; import org.eclipse.jface.fieldassist.ContentProposalAdapter; import org.eclipse.jface.fieldassist.IContentProposal; import org.eclipse.jface.fieldassist.IContentProposalProvider; import org.eclipse.jface.fieldassist.IControlContentAdapter; import org.eclipse.jface.fieldassist.TextContentAdapter; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Text; /** * コードアシストの手習い * * @author nakawakashigeto * */ public class Main extends ApplicationWindow { public Main() { super(null); } public static void main(String[] args) { Main window = new Main(); window.setBlockOnOpen(true); window.open(); Display.getCurrent().dispose(); } protected Control createContents(Composite parent) { Text text = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); IControlContentAdapter contentAdapter = new TextContentAdapter(); /* * 提案する内容提供します。 */ IContentProposalProvider provider = new IContentProposalProvider() { public IContentProposal[] getProposals(String content, int pos) { return createProposals(content, pos); } }; /* * ctrl + spaceでコードアシストするようにしています。 */ ContentProposalAdapter contentProposalAdapter = new ContentProposalAdapter( text, contentAdapter, provider, keystroke("Ctrl+Space"), null); return parent; } /** * <p> * 入力された文字列が「あなたは」で終わっている場合に提案します。 * </p> * * @param content * @param pos * @return */ static public IContentProposal[] createProposals(final String content, final int pos) { if (content.length() == 0 || !(0 < (pos - 3)) || "あなたは".equals(content.substring(pos - 3, pos))) { return new IContentProposal[] {}; } String[] strings = { "愉快な人","ちょっとあぶない", "いい人", "いやな人", "楽しい人"}; IContentProposal[] proposals = new IContentProposal[strings.length]; for (int i = 0; i < proposals.length; i++) { final String s = strings[i]; proposals[i] = new IContentProposal() { public String getContent() { return "、ずばり" + s; } public int getCursorPosition() { return 0; } public String getDescription() { return "あなたは" + s; } public String getLabel() { return s; } }; } return proposals; } static KeyStroke keystroke(String s) { try { return KeyStroke.getInstance(s); } catch (ParseException e) { e.printStackTrace(); } return null; } }