public class TestWindow extends ApplicationWindow
{
public TestWindow(Shell parentShell)
{
super(parentShell);
}
@Override
protected Control createContents(Composite parent)
{
Composite clientArea = new Composite(parent, SWT.NONE);
clientArea.setLayout(new RowLayout());
Text text = new Text((Composite)clientArea, SWT.NONE);
text.addModifyListener(new MyModifyListener());
return clientArea;
}
/**
* @param args
*/
public static void main(String[] args)
{
TestWindow testWindow = new TestWindow(Display.getDefault().getActiveShell());
testWindow.setBlockOnOpen(true);
testWindow.open();
}
/**
* 自定义校验类
* ModifyListener是文本被修改后触发和VerifyListener是将要修改文本时触发,可以通过设置VerifyEvent的doit属性决定是否修改文本
*/
private class MyModifyListener implements ModifyListener
{
private UIJob modifyJob = null;
@Override
public void modifyText(ModifyEvent e)
{
if (null != modifyJob)
{
// 取消安排旧修改的校验Job
modifyJob.cancel();
}
modifyJob = new UIJob("Validate Job Title") //$NON-NLS-1$
{
@Override
protected IStatus runInUIThread(IProgressMonitor monitor)
{
if (monitor.isCanceled())
{
return Status.OK_STATUS;
}
// TODO 这儿是校验逻辑
// 注意当逻辑中涉及到界面控件时,要先判断控件是否非空且未被销毁
return Status.OK_STATUS;
}
};
// 安排对最新修改的校验Job
modifyJob.schedule(500);
}
}
}