1. package cn.com.tt.listener;  
  2.  
  3. import org.eclipse.swt.events.ModifyEvent;  
  4. import org.eclipse.swt.events.ModifyListener;  
  5. import org.eclipse.swt.widgets.Text;  
  6.  
  7. /**  
  8.  * <b>Class Name:</b>TextModifyListener<br>  
  9.  * <b>Class Description:</b>针对org.eclipse.swt.widgets.Text控件做的一个特殊处理.  
  10.  * <p>  
  11.  * 详细描述: 往Text控件写入值时,如果程序在向Text写入值之前,无权做任何处理,但又必须要将写入Text的值进行特殊处理,然后写入Text,  
  12.  * 则这个类便可以实现此功能.(此类是通过截取字符长度来演示)  
  13.  * </p>  
  14.  *   
  15.  * @author TangKai  
  16.  * @version $version 1.0 $ date:2013-02-25  
  17.  *   
  18.  */ 
  19. public class TextModifyListener implements ModifyListener {  
  20.  
  21.     /** Text主控件 */ 
  22.     private Text text;  
  23.  
  24.     /** 截取字符串长度 */ 
  25.     private int len;  
  26.  
  27.     /** 避免重复执行标志 (非常重要的一个标记) */ 
  28.     private boolean writeFlag = true;  
  29.  
  30.     /** 写入状态 */ 
  31.     private String value = "";  
  32.  
  33.     /**  
  34.      * 按长度进行截取  
  35.      *   
  36.      * @param text  
  37.      *            主控件  
  38.      * @param len  
  39.      *            截取字符串长度  
  40.      */ 
  41.     public TextModifyListener(Text text, int len) {  
  42.         this.text = text;  
  43.         this.len = len;  
  44.     }  
  45.  
  46.     public void modifyText(ModifyEvent e) {  
  47.         String str = ((Text) e.widget).getText();  
  48.  
  49.         if (str == null || str.trim().length() <= 0 || this.text == null)  
  50.             return;  
  51.  
  52.         // 接收手动输入的字符时,当接收输入字符超过1个时,直接退出,且退出前必须要将全局变量value值清空  
  53.         if (str.trim().length() > 1) {  
  54.             value = "";  
  55.             return;  
  56.         }  
  57.  
  58.         // 判断Text写入状态flag,当上一次状态为false时,需要将写入状态改回true;  
  59.         // 同时需要将上一次写入Text的全局变量value值清空,开始重新接收外部写入的字符  
  60.         if (!writeFlag) {  
  61.             value = "";  
  62.             writeFlag = true;  
  63.         }  
  64.  
  65.         // 循环接收输入字符,并拼接成新的字符串  
  66.         value += str;  
  67.  
  68.         // 外部结束输入字符时,将最后拼接得到字符串value写入Text  
  69.         text.getDisplay().asyncExec(new Runnable() {  
  70.             public void run() {  
  71.                 if (writeFlag) {  
  72.                     text.setText("");// 每次向Text写值前,可以先把原来值清空  
  73.                     if (value.trim().length() > len) {  
  74.                         text.setText(value.substring(0, len));  
  75.                     } else {  
  76.                         text.setText(value);  
  77.                     }  
  78.  
  79.                     /*  
  80.                      * 定位光标: 主要解决刷卡器刷卡与手动输入卡号来回切换引起的问题。  
  81.                      * 如果不设置光标定位,有时候会在手动输入一个字符后,光标会 自动移动到字符左边,  
  82.                      * 导致正常的输入顺序被打乱,操作速度快的时候极影响输入字符正确性。  
  83.                      */ 
  84.                     int l = text.getText().length();  
  85.                     text.setSelection(l, l);  
  86.                     writeFlag = false;  
  87.                 }  
  88.             }  
  89.         });  
  90.     }  
  91. }  
  92.  
  93.  
  94. package cn.com.tt.listener;  
  95.  
  96. import org.eclipse.swt.SWT;  
  97. import org.eclipse.swt.events.SelectionEvent;  
  98. import org.eclipse.swt.events.SelectionListener;  
  99. import org.eclipse.swt.widgets.Button;  
  100. import org.eclipse.swt.widgets.Display;  
  101. import org.eclipse.swt.widgets.Label;  
  102. import org.eclipse.swt.widgets.Shell;  
  103. import org.eclipse.swt.widgets.Text;  
  104.  
  105. /**  
  106.  * Class Description: 演示某医院刷卡器示例,某公司医疗系统+某设备商提供刷卡器。<br>  
  107.  * <br>  
  108.  * <p>  
  109.  * 需求:医院觉得刷卡器刷卡时,将磁卡中不需要的号码也刷了出来(如:1111111=2222),医院只需要等号前号码,现在医院要求医疗系统进行处理。<br>  
  110.  *   
  111.  * 这种情况下,医疗系统这边首先确认刷卡器时如何向可编辑文本框写字符的。确认刷卡器是一位一位写字符后,  
  112.  * 便可以使用TextModifyListener监听类的实现方式完成(刷卡器 与手动输入都满足)。  
  113.  * </P>  
  114.  *   
  115.  * @author TangKai  
  116.  * @version $version 1.0 $ date:2013-02-25  
  117.  *   
  118.  */ 
  119. public class Test {  
  120.     public static void main(String[] args) {  
  121.         Display disPlay = Display.getDefault();  
  122.         Shell shell = new Shell();  
  123.         shell.setBounds(300300360150);  
  124.         shell.setText("模拟某医院刷卡器刷卡操作");  
  125.  
  126.         Label l = new Label(shell, SWT.NONE);  
  127.         l.setText("磁条卡号:");  
  128.         l.setBounds(10166020);  
  129.  
  130.         // 磁卡卡号  
  131.         final Text cardnum = new Text(shell, SWT.BORDER);  
  132.         cardnum.setText("1111111111111111111=2222222222222222222");  
  133.         cardnum.setBounds(701225020);  
  134.         Label l2 = new Label(shell, SWT.NONE);  
  135.         l2.setText("卡号:");  
  136.         l2.setBounds(10465020);  
  137.  
  138.         final Text text = new Text(shell, SWT.BORDER);  
  139.         text.setBounds(704225020);  
  140.         text.setFocus();  
  141.         text.addModifyListener(new TextModifyListener(text, 19));  
  142.  
  143.         // 通过Button事件模拟刷卡器刷卡操作  
  144.         Button btn = new Button(shell, SWT.NORMAL);  
  145.         btn.setBounds(70856020);  
  146.         btn.setText("开始刷卡");  
  147.         btn.addSelectionListener(new SelectionListener() {  
  148.             public void widgetSelected(SelectionEvent e) {  
  149.                 String s = cardnum.getText();  
  150.                 s = s == null ? "" : s;  
  151.                 // 模拟刷卡器,将卡号一位一位写入Text控件  
  152.                 for (int i = 0; i < s.trim().length(); i++) {  
  153.                     text.setText(s.charAt(i) + "");  
  154.                 }  
  155.             }  
  156.  
  157.             public void widgetDefaultSelected(SelectionEvent e) {  
  158.             }  
  159.         });  
  160.  
  161.         shell.layout();  
  162.         shell.open();  
  163.         while (!shell.isDisposed()) {  
  164.             if (!disPlay.readAndDispatch()) {  
  165.                 disPlay.sleep();  
  166.             }  
  167.         }  
  168.         disPlay.dispose();  
  169.     }  
 
模拟刷卡器效果