- package cn.com.tt.listener;
- import org.eclipse.swt.events.ModifyEvent;
- import org.eclipse.swt.events.ModifyListener;
- import org.eclipse.swt.widgets.Text;
- /**
- * <b>Class Name:</b>TextModifyListener<br>
- * <b>Class Description:</b>針對org.eclipse.swt.widgets.Text控件做的一個特殊處理.
- * <p>
- * 詳細描述: 往Text控件寫入值時,如果程序在向Text寫入值之前,無權做任何處理,但又必須要將寫入Text的值進行特殊處理,然後寫入Text,
- * 則這個類便可以實現此功能.(此類是通過截取字符長度來演示)
- * </p>
- *
- * @author TangKai
- * @version $version 1.0 $ date:2013-02-25
- *
- */
- public class TextModifyListener implements ModifyListener {
- /** Text主控件 */
- private Text text;
- /** 截取字符串長度 */
- private int len;
- /** 避免重複執行標誌 (非常重要的一個標記) */
- private boolean writeFlag = true;
- /** 寫入狀態 */
- private String value = "";
- /**
- * 按長度進行截取
- *
- * @param text
- * 主控件
- * @param len
- * 截取字符串長度
- */
- public TextModifyListener(Text text, int len) {
- this.text = text;
- this.len = len;
- }
- public void modifyText(ModifyEvent e) {
- String str = ((Text) e.widget).getText();
- if (str == null || str.trim().length() <= 0 || this.text == null)
- return;
- // 接收手動輸入的字符時,當接收輸入字符超過1個時,直接退出,且退出前必須要將全局變量value值清空
- if (str.trim().length() > 1) {
- value = "";
- return;
- }
- // 判斷Text寫入狀態flag,當上一次狀態爲false時,需要將寫入狀態改回true;
- // 同時需要將上一次寫入Text的全局變量value值清空,開始重新接收外部寫入的字符
- if (!writeFlag) {
- value = "";
- writeFlag = true;
- }
- // 循環接收輸入字符,並拼接成新的字符串
- value += str;
- // 外部結束輸入字符時,將最後拼接得到字符串value寫入Text
- text.getDisplay().asyncExec(new Runnable() {
- public void run() {
- if (writeFlag) {
- text.setText("");// 每次向Text寫值前,可以先把原來值清空
- if (value.trim().length() > len) {
- text.setText(value.substring(0, len));
- } else {
- text.setText(value);
- }
- /*
- * 定位光標: 主要解決刷卡器刷卡與手動輸入卡號來回切換引起的問題。
- * 如果不設置光標定位,有時候會在手動輸入一個字符後,光標會 自動移動到字符左邊,
- * 導致正常的輸入順序被打亂,操作速度快的時候極影響輸入字符正確性。
- */
- int l = text.getText().length();
- text.setSelection(l, l);
- writeFlag = false;
- }
- }
- });
- }
- }
- package cn.com.tt.listener;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.events.SelectionListener;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- /**
- * Class Description: 演示某醫院刷卡器示例,某公司醫療系統+某設備商提供刷卡器。<br>
- * <br>
- * <p>
- * 需求:醫院覺得刷卡器刷卡時,將磁卡中不需要的號碼也刷了出來(如:1111111=2222),醫院只需要等號前號碼,現在醫院要求醫療系統進行處理。<br>
- *
- * 這種情況下,醫療系統這邊首先確認刷卡器時如何向可編輯文本框寫字符的。確認刷卡器是一位一位寫字符後,
- * 便可以使用TextModifyListener監聽類的實現方式完成(刷卡器 與手動輸入都滿足)。
- * </P>
- *
- * @author TangKai
- * @version $version 1.0 $ date:2013-02-25
- *
- */
- public class Test {
- public static void main(String[] args) {
- Display disPlay = Display.getDefault();
- Shell shell = new Shell();
- shell.setBounds(300, 300, 360, 150);
- shell.setText("模擬某醫院刷卡器刷卡操作");
- Label l = new Label(shell, SWT.NONE);
- l.setText("磁條卡號:");
- l.setBounds(10, 16, 60, 20);
- // 磁卡卡號
- final Text cardnum = new Text(shell, SWT.BORDER);
- cardnum.setText("1111111111111111111=2222222222222222222");
- cardnum.setBounds(70, 12, 250, 20);
- Label l2 = new Label(shell, SWT.NONE);
- l2.setText("卡號:");
- l2.setBounds(10, 46, 50, 20);
- final Text text = new Text(shell, SWT.BORDER);
- text.setBounds(70, 42, 250, 20);
- text.setFocus();
- text.addModifyListener(new TextModifyListener(text, 19));
- // 通過Button事件模擬刷卡器刷卡操作
- Button btn = new Button(shell, SWT.NORMAL);
- btn.setBounds(70, 85, 60, 20);
- btn.setText("開始刷卡");
- btn.addSelectionListener(new SelectionListener() {
- public void widgetSelected(SelectionEvent e) {
- String s = cardnum.getText();
- s = s == null ? "" : s;
- // 模擬刷卡器,將卡號一位一位寫入Text控件
- for (int i = 0; i < s.trim().length(); i++) {
- text.setText(s.charAt(i) + "");
- }
- }
- public void widgetDefaultSelected(SelectionEvent e) {
- }
- });
- shell.layout();
- shell.open();
- while (!shell.isDisposed()) {
- if (!disPlay.readAndDispatch()) {
- disPlay.sleep();
- }
- }
- disPlay.dispose();
- }
- }