SWT中为Text组件添加键盘回车事件

以下是我自己写的代码


   

package com省略;



import java.util.List;


import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.topshinetech.topway.comm.communication.bean.base.Node;
import com.topshinetech.topway.cu.commn.util.MSGDialogUtil;
import com.topshinetech.topway.cu.commn.util.functional.Functions;
import com.topshinetech.topway.cu.commn.util.functional.Matcher;
import com.topshinetech.topway.cu.dis.bean.GroupBean;
import com.topshinetech.topway.cu.dis.bean.UserBean;
import com.topshinetech.topway.cu.main.util.PinyinFunc;


/** 

* 搜索文本框回车事件
*/
public class EKeySearchAction implements KeyListener {


public static final Logger LOGGER = LoggerFactory.getLogger(EKeySearchAction.class);


private TreeViewer treeViewer;

private Text searchText;

public EKeySearchAction(TreeViewer treeViewer, Text searchText)
{
super();
this.treeViewer = treeViewer;
this.searchText = searchText;
}




@Override
public void keyPressed(KeyEvent arg0) {



}




@Override

public void keyReleased(KeyEvent e) {

//特别注意一下几行,事件是否能够正常使用,下面几行是一定要有的。

if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
            // 让按键原有的功能失效
            e.doit = false;
            // 执行你自己的事件

            String keyWord=searchText.getText();
       //MSGDialogUtil.showMessageBox(null, keyWord, SWT.ICON_WARNING | SWT.YES);
    if(keyWord==null || keyWord.trim().isEmpty())
    return;
    final String key=keyWord.trim();
    @SuppressWarnings("unchecked")
    List<Node> input=(List<Node>)treeViewer.getInput();//获取树的根节点
    Node root=input.get(0);//获取树的根节点
   
    //直接匹配
    List<Node> list=Functions.filter(root.breadthFirst(), new Matcher<Node>()
    {
    public int match(Node target)
    {
//     target.getAttach(target.getName());
    String type = target.getType().getName();
    if(type.equals("group")){
    GroupBean group = (GroupBean)target.getAttach(Node.ATTACH_KEY_FOR_GROUP);
    return target.getName().indexOf(key);
    }
    if(type.equals("user")){
    UserBean user = (UserBean)target.getAttach(Node.ATTACH_KEY_FOR_USER);
    String temp = user.getUsername()+user.getUsernum();
    return temp.indexOf(key);
    }
    return 0;
//     MSGDialogUtil.showMessageBox(null, type, SWT.ICON_WARNING | SWT.YES);
//     return target.getName().indexOf(key);
    }
    });
   
    //如果直接匹配失败,运行一下代码
    if(null == list || list.size() <= 0)
    {   
       if(key.matches("[a-zA-Z]"))
       {
           list =Functions.filter(root.breadthFirst(), new Matcher<Node>()
                    {
                        public int match(Node target)
                        {
                            return PinyinFunc.getFirstSpell(target.getName()).indexOf(key.toLowerCase());               
                        }
                    });
           List<Node> fullList=Functions.filter(root.breadthFirst(), new Matcher<Node>()
                    {
                        public int match(Node target)
                        {
                            return PinyinFunc.getFullSpell(target.getName()).indexOf(key.toLowerCase());               
                        }
                    });
           list.addAll(fullList);
                }
       else
                {
                    LOGGER.error("search key:"+key+" no pingyin");
                }
    }
   
    ISelection selection = new StructuredSelection(list.toArray());
    treeViewer.setSelection(selection, true);
            
        }

}

}



看完后希望的大家以后的工作或者学习有益。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值