- 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();
- }
- }
模拟刷卡器效果
转载于:https://blog.51cto.com/cnctocn/1149737