org.eclipse.swt.widets.Text ModifyListener 監聽事件

 

  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.     }  
 
模擬刷卡器效果
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章