SWT:获取即将从文本小部件中删除的字符数
SWT: get number of characters that are about to be deleted from Text widget
我正在验证来自一些 SWT 文本小部件的输入。
假设用户使用鼠标或 Ctrl. + A
等选择他自己插入到小部件中的文本的某些部分,然后按删除;如何获取即将删除的字符数?我正在使用 SWT 验证事件进行输入验证。文档说 event.text 将提供 "the new text that will be inserted.[...]" 并且能够修改将被删除的文本,但我无法找到一种方法来获取将被删除的文本数量。
谷歌也没有给我答案。有人知道如何实现吗?
由于实际代码比较冗长,所以只好删掉不必要的部分贴在这里:
该代码的目的是取 32 位十六进制数字并允许 Copy/Paste。
int len = 0;
// input is a SWT Text widget with initial text set to ""
input.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e){
e.doit = false;
int addit = 0;
// do nothing when there are already 32 characters in the widget
if (len == 32){
return;
}
// allow Ctrl + v to paste
if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'v') && (len + e.text.length()) <= 32){
e.doit = true;
// here it works like a charm
addit = e.text.length();
}
// allow deleting characters: Problem: deleting multiple characters at a time
if (e.keyCode == SWT.BS || e.keyCode == SWT.DEL){
e.doit = true;
addit = ???; // this is the problem: how many characters are deleted, when the key is pressed
}
// isHex is a function that checks, weather e.character is a hexadecimal digit
if (isHex(e.character) || Character.isDigit(e.character) && len+1 <= 32){
e.doit = true;
addit = 1;
} else if (e.keyCode != SWT.BS && e.keyCode != SWT.BS && ((e.stateMask & SWT.CTRL) != SWT.CTRL) && e.keyCode != 'v') {
System.out.println(e.character + " is not valid");
return;
}
len += addit;
if (len == 32){
somemethod();
}
// print how many characters are there the moment the user types or deletes them
System.out.println((32 - len) + "chars");
}});
}
你可以简单的对比一下原文的长度和验证事件后的用户修改。试听SWT.Verify
比较长度:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell();
shell.setText("Whosebug");
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.Verify, new Listener()
{
@Override
public void handleEvent(Event e)
{
final String oldS = text.getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
int difference = oldS.length() - newS.length();
if(difference > 0)
System.out.println("User deleted " + difference + " characters");
else if(difference < 0)
System.out.println("User added " + Math.abs(difference) + " characters");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
我正在验证来自一些 SWT 文本小部件的输入。
假设用户使用鼠标或 Ctrl. + A
等选择他自己插入到小部件中的文本的某些部分,然后按删除;如何获取即将删除的字符数?我正在使用 SWT 验证事件进行输入验证。文档说 event.text 将提供 "the new text that will be inserted.[...]" 并且能够修改将被删除的文本,但我无法找到一种方法来获取将被删除的文本数量。
谷歌也没有给我答案。有人知道如何实现吗?
由于实际代码比较冗长,所以只好删掉不必要的部分贴在这里:
该代码的目的是取 32 位十六进制数字并允许 Copy/Paste。
int len = 0;
// input is a SWT Text widget with initial text set to ""
input.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e){
e.doit = false;
int addit = 0;
// do nothing when there are already 32 characters in the widget
if (len == 32){
return;
}
// allow Ctrl + v to paste
if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'v') && (len + e.text.length()) <= 32){
e.doit = true;
// here it works like a charm
addit = e.text.length();
}
// allow deleting characters: Problem: deleting multiple characters at a time
if (e.keyCode == SWT.BS || e.keyCode == SWT.DEL){
e.doit = true;
addit = ???; // this is the problem: how many characters are deleted, when the key is pressed
}
// isHex is a function that checks, weather e.character is a hexadecimal digit
if (isHex(e.character) || Character.isDigit(e.character) && len+1 <= 32){
e.doit = true;
addit = 1;
} else if (e.keyCode != SWT.BS && e.keyCode != SWT.BS && ((e.stateMask & SWT.CTRL) != SWT.CTRL) && e.keyCode != 'v') {
System.out.println(e.character + " is not valid");
return;
}
len += addit;
if (len == 32){
somemethod();
}
// print how many characters are there the moment the user types or deletes them
System.out.println((32 - len) + "chars");
}});
}
你可以简单的对比一下原文的长度和验证事件后的用户修改。试听SWT.Verify
比较长度:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell();
shell.setText("Whosebug");
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.Verify, new Listener()
{
@Override
public void handleEvent(Event e)
{
final String oldS = text.getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
int difference = oldS.length() - newS.length();
if(difference > 0)
System.out.println("User deleted " + difference + " characters");
else if(difference < 0)
System.out.println("User added " + Math.abs(difference) + " characters");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}