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

xiaoxiao2021-02-28  31

以下是我自己写的代码

   

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);                      } }

}

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

转载请注明原文地址: https://www.6miu.com/read-1600164.html

最新回复(0)