在SWT文本字段中自动完成

介绍

对于丰富的应用程序,您必须观察到文本字段的行为类似于动态组合框。 当用户在文本字段中输入一些字符时,将弹出一个弹出窗口,其中包含值列表作为对用户的建议。 此功能称为自动完成。 如果是Eclipse编辑器,则必须具有

看到在编写Java代码时看到的情况,如果开发人员按下“ Ctrl +空格键”,则动态列表框会提供多个建议。 因此,在本文中,我将向您展示在进行eclipse插件开发时如何实现此功能。

技术性

这里的概念非常简单,因为Eclipse提供了所有功能。 首先,创建一个文本字段并将ContentProposalAdapter附加到该文本字段。 有一个类callec“ SimpleContentProposalProvider”

字符串数组作为向用户提出的建议。 除此之外,您还必须形成一个关键序列,基于此序列,所有建议都将列为清单。 让我给你一个摘录。

1.将所有投标作为String数组获取。

String [] allProposals = {“ a”,“ b”,“ c”}; 等等

2.通过传递提案数组来创建SimpleContentProposalProvider的对象

作为新的SimpleContentProposalProvider(allProposals);

3.形成键序列,以便按键序列将显示提案列表

作为KeyStroke.getInstance(“ CTRL + Space”);

4.最后,将“ ContentProposalAdapter”附加到文本字段,如下所示:

新的ContentProposalAdapter(SWT文本字段,新的TextContentAdapter(),SimpleContentProposalAdapter,键序列,空)等。

现在,让我们拥有完整的代码结构,以便您可以全面了解。

AutoCompletionTextField.java的代码
package com.core.plugin.text; 
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text; 
/**
 * This class is used to ptovide an example of showing
 * completion feature in a text field.
 * @author Debadatta Mishra(PIKU)
 *
 */
public class AutoCompletionTextField 
{
    /**
     * A label for for display of message.
     */
    private static Label label = null;
    /**
     * Object of type {@link Text} to display a text field
     */
    private static Text text = null;
    /**
     * A String array of default proposals for autocompletion
     */
    private static String[] defaultProposals = new String[] { "Assistance 1","Assistance 2", "Assistance 3" , "Assistance 4" , "Assistance 5"};
    /**
     * A String for key press
     */
    private static String KEY_PRESS = "Ctrl+Space"; 
    /**
     * Method used to create a label.
     * 
     * @author Debadatta Mishra (PIKU)
     * @param shell of type {@link Shell}
     */
    private static void createLabel( Shell shell )
    {
        label = new Label( shell , SWT.NONE);
        label.setText("Enter some text in the text field");
        //Alignment of label in the shell
        FormData label1LData = new FormData();
        label1LData.width = 162;
        label1LData.height = 15;
        label1LData.left =  new FormAttachment(0, 1000, 12);
        label1LData.top =  new FormAttachment(0, 1000, 12);
        label.setLayoutData(label1LData);
    } 
    /**
     * Method used to display an array of String data for
     * autocompletion. You can have your own method like
     * this to get the autocompletion data. This method
     * can be customized to get the data from database
     * and you can display as autocompletion array.
     * 
     * @param text of type String
     * @return an array of String data
     * @author Debadatta Mishra (PIKU)
     */
    private static String[] getAllProposals( String text )
    {
        String[] proposals = new String[5];
        if( text == null || text.length() == 0 )
            proposals = defaultProposals;
        else
        {
            for( int i = 0 ; i < 5 ; i++ )
                proposals[i] = text+i;
        }
        return proposals;
    } 
    /**
     * This method is used to provide the implementaion
     * of eclipse autocompletion feature. User has to press
     * "CTRL+Space" to see the autocompletion effect.
     * 
     * @param text of type {@link Text}
     * @param value of type String
     * @author Debadatta Mishra (PIKU)
     */
    private static void setAutoCompletion( Text text , String value )
    {
        try
        {
            ContentProposalAdapter adapter = null;
            String[] defaultProposals = getAllProposals(value);
            SimpleContentProposalProvider scp = new SimpleContentProposalProvider( defaultProposals );
            scp.setProposals(defaultProposals);
            KeyStroke ks = KeyStroke.getInstance(KEY_PRESS);
            adapter = new ContentProposalAdapter(text, new TextContentAdapter(),
                    scp,ks,null);
            adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    } 
    /**
     * Method used to create a text field.
     * 
     * @author Debadatta Mishra (PIKU)
     * @param shell of type {@link Shell}
     * @author Debadatta Mishra (PIKU)
     */
    private static void createText( Shell shell )
    {
        text = new Text(shell,SWT.BORDER);
        //Alignment of Text field in the shell
        FormData text1LData = new FormData();
        text1LData.width = 223;
        text1LData.height = 34;
        text1LData.left =  new FormAttachment(0, 1000, 236);
        text1LData.top =  new FormAttachment(0, 1000, 12);
        text.setLayoutData(text1LData);
        //Method for autocompletion
        setAutoCompletion(text, null); 
        text.addKeyListener( new KeyAdapter()
        {
            public void keyReleased(KeyEvent ke) 
            {
                //Method for autocompletion
                setAutoCompletion(text, text.getText());
            }
        }
        );
    } 
    /**
     * Main method to execute the test
     * 
     * @author Debadatta Mishra (PIKU)
     * @param args of type {@link String}
     */
    public static void main(String[] args) 
    {
        final Display display = new Display ();
        final Shell shell = new Shell (display, SWT.CLOSE);
        shell.setText("A text field with autocompletion support, press CTRL+Space to see the effect");
        shell.setLayout(new FormLayout());
        shell.setSize(600, 200); 
        createLabel(shell);
        createText(shell); 
        shell.open ();
        while (!shell.isDisposed ()) 
        {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    } 
} 
您可以运行上面的片段代码,然后在文本字段中按“ Ctrl + Space”以查看效果。 您还可以下载附加的源代码。

假设条件

我认为这篇文章的读者有

暴露于eclipse插件开发

Java语言知识

有关在Eclipse编辑器中运行程序的知识

测试用例详细信息

我已经在以下条件下测试了上述程序。

操作系统名称:Windows Vista

Eclipse API:3.2

的Java:1.6.0_16

Java编辑器:Eclipse 3.2

结论

希望您喜欢我的文章。 本文不具有任何商业意义,仅适用于学习和新手开发人员。 如有任何问题或错误,请随时通过电子邮件DELETED与我联系。

附加的文件
文件类型:zip AutoCompletionTextField.zip (1.7 KB,1107视图)

From: https://bytes.com/topic/java/insights/879093-auto-completion-swt-text-field

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值