实现内容助理(1. 自动完成)

自:http://www.cnblogs.com/bjzhanghao/archive/2007/09/28/908648.html

 

在实际项目应用里,如果需要用户手动输入比较复杂的文本内容时可以考虑利用内容助理(Content Assistant)功能减轻用户负担,同时减低出错的机会。Jface的SourceViewer支持内容助理,这篇帖子里介绍一下如 何实现自动完成(Auto Completion)功能,即向用户提示接下来可能输入的内容。

// Create a new source viewer
sourceViewer  =   new  SourceViewer(shell,  null , SWT.BORDER  |  SWT.WRAP  |  SWT.V_SCROLL);

// Set a blank document
sourceViewer.setDocument( new  Document( "" ));
sourceViewer.setEditable(
true );

StyledText txtSource  =  sourceViewer.getTextWidget();
GridData gd 
=   new  GridData(GridData.FILL_BOTH);
txtSource.setLayoutData(gd);

自动完成功能一般在以下两种条件下弹出一个小窗口向用户提示当前可供选择的选项,一是用户按下指定的组合键时,二是用户输入了特定的字 符时,SourceViewer支持这两种触发方式。在程序里使用SourceViewer和使用一般控件没有很大的分别,只是SourceViewer 是StyledText的包装,所以一些操作要通过getTextWidget()完成,如下所示:

// Configure source viewer, add content assistant support
sourceViewer.configure( new  SourceViewerConfiguration() {
    @Override
    
public  IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
        ContentAssistant assistant 
=   new  ContentAssistant();
        IContentAssistProcessor cap 
=   new  MyContentAssistProcessor();
        assistant.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE);
        assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
        assistant.enableAutoActivation(
true );
        
return  assistant;
    }
});

现在这个SourceViewer还不能弹出任何提示,因为我们还没有给它一个SourceViewerConfiguration,后者通过 getContentAssistant()负责提供一个IContentAssistant的实现。下面的代码显示了如何为SourceViewer设 置SourceViewerConfiguration,这个例子里不论当前文本框里是什么内容都弹出一样的提示选项,在实际应用里可以根据内容改变选 项:

public  ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,  int  offset) {
    String content 
=  viewer.getTextWidget().getText();

    
try  {

        
// Demo options
         final  String[] options  =   new  String[] {  " sum() " " count() " " sort() "  };

        
// Dynamically generate proposal
        ArrayList result  =   new  ArrayList();
        
for  ( int  i  =   0 ; i  <  options.length; i ++ ) {
            CompletionProposal proposal 
=   new  CompletionProposal(options[i], offset,  0 , options[i].length());
            result.add(proposal);
        }
        
return  (ICompletionProposal[]) result.toArray( new  ICompletionProposal[result.size()]);
    } 
catch  (Exception e) {
        e.printStackTrace();
    }
    
return   null ;
}

public   char [] getCompletionProposalAutoActivationCharacters() {
    
return  TRIGGER_TOKENS;
}

上面代码里,MyContentAssistProcessor是我们对IContentAssistant接口的实现,它里面与自动完成有关的是 computeCompletionProposals()和 getCompletionProposalAutoActivationCharacters()这两个方法,前者返回的结果数组将作为弹出提示窗口里 的选项,后者返回的字符数组包含了可以触发弹出窗口的特殊字符。

最后,我们还要支持用户触发内容助理,这要求为SourceViewer添加一个监听器:

sourceViewer.appendVerifyKeyListener( new  VerifyKeyListener() {
    
public   void  verifyKey(VerifyEvent event) {
        
//  Check for Alt+/
         if  (event.stateMask  ==  SWT.ALT  &&  event.character  ==   ' / ' ) {
            
//  Check if source viewer is able to perform operation
             if  (sourceViewer.canDoOperation(SourceViewer.CONTENTASSIST_PROPOSALS))
                
//  Perform operation
                sourceViewer.doOperation(SourceViewer.CONTENTASSIST_PROPOSALS);
            
//  Veto this key press to avoid further processing
            event.doit  =   false ;
        }
    }
});

实现后的结果截图如下图所示,(示例代码下载 ):

相关参考链接:
为 SWT 应用程序配备内容助理
FAQ How do I add Content Assist to my language editor?
Eclipse Help - Content Assist

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值