Implement content Assistant (1. Automatic completion)

Source: Internet
Author: User

In actual project applications, if you need to manually enter complicated text content, you can use the content assistant function to reduce the burden on users and reduce the chance of errors. Jface's sourceviewer supports content assistants. This post describes how to implement the auto completion function, that is, to notify the user of the content that may be entered next.

// Create a new source Viewer
Sourceviewer =   New Sourceviewer (shell, Null , SWT. Border | SWT. Wrap | SWT. v_scroll );

//Set a blank document
Sourceviewer. setdocument (NewDocument (""));
Sourceviewer. seteditable (True);

Styledtext txtsource=Sourceviewer. gettextwidget ();
Griddata GD= NewGriddata (griddata. fill_both );
Txtsource. setlayoutdata (GD );

The auto-completion function usually pops up a small window in the following two conditions to notify the user of the options currently available. One is that when the user presses the specified key combination, the other is that the user inputs a specific word.
Sourceviewer supports the two triggering methods. InProgramThe use of sourceviewer and the use of general controls are not very big difference, but sourceviewer
Is the packaging of styledtext, so some operations must be completed through gettextwidget (), as shown below:

// Configure source viewer, add content assistant support
Sourceviewer. Configure ( New Sourceviewerconfiguration (){
@ Override
Public Icontentassistant getcontentassistant (isourceviewer sourceviewer ){
Contentassistant Assistant =   New Contentassistant ();
Icontentappsprocessor cap =   New Mycontentappsprocessor ();
Assistant. setcontent1_processor (Cap, idocument. default_content_type );
Assistant. setinformationcontrolcreator (getinformationcontrolcreator (sourceviewer ));
Assistant. enableautoactivation ( True );
Return Assistant;
}
});

Currently, this sourceviewer does not display any prompts, because we have not given it a sourceviewerconfiguration, which provides an icontentassistant implementation through getcontentassistant. The followingCodeShows how to set sourceviewerconfiguration for sourceviewer. In this example, the same prompt option is displayed no matter what content is in the current text box. In actual application, you can change the options according to the content:

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 (){
ReturnTrigger_tokens;
}

In the above Code, mycontentappsprocessor is our implementation of the icontentassistant interface, which is related to Automatic completion of the computecompletionproposals () and getcompletionproposalautoactivationcharacters () methods, the result array returned by the former is used as an option in the pop-up window. The character array returned by the latter contains special characters that can trigger the pop-up window.

Finally, we also need to support user-triggered content Assistant, which requires adding a listener for 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. content__proposals ))
// Perform operation
Sourceviewer. dooperation (sourceviewer. contentassist_proposals );
// Veto this key press to avoid further processing
Event. doit =   False ;
}
}
});

The implementation result is shown in (sample code download ):

Reference Links:
Provides content assistant for SWT applications
FAQ how do I add content assist to my language editor?
Eclipse help-content assist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.