为 SWT 应用程序配备内容助理:通过上下文敏感的智能内容完成建议,提高最终用户的便利性和生产率
2009-12-14 00:00:00 来源:WEB开发网
清单 8. 建议集合// Proposal part before cursor
private final static String[] STRUCTTAGS1 =
new String[] { "<P>", "<A SRC=\"", "<TABLE>", "<TR>", "<TD>" };
// Proposal part after cursor
private final static String[] STRUCTTAGS2 =
new String[] { "", "\"></A>", "</TABLE>", "</TR>", "</TD>" }
可以看到,我们将每个标签建议划分为两个部分:一部分在预计的光标位置之前,一部分在预计的光标位置之后。清单 9 显示了编译这些建议的 computeStructureProposals() 方法。
清单 9. computeStructureProposalsprivate void computeStructureProposals(String qualifier, int documentOffset, List propList) {
int qlen = qualifier.length();
// Loop through all proposals
for (int i = 0; i < STRUCTTAGS1.length; i++) {
String startTag = STRUCTTAGS1[i];
// Check if proposal matches qualifier
if (startTag.startsWith(qualifier)) {
// Yes -- compute whole proposal text
String text = startTag + STRUCTTAGS2[i];
// Derive cursor position
int cursor = startTag.length();
// Construct proposal
CompletionProposal proposal =
new CompletionProposal(text, documentOffset - qlen, qlen, cursor);
// and add to result list
propList.add(proposal);
}
}
}
更多精彩
赞助商链接