I'm running Eclipse Indigo 3.7.1 on Windows 7 and use an SWT Text control with a VerifyListener to uppercase any entered text. Undo (CTRL-Z) appears to work incorrectly with entered text. Could this be a bug, am I using the VerifyListener wrongly, or did I overlook something while attempting to uppercase text with Undo working?
How to reproduce the behavior (sample code below):
1) Enter "HelloWorld" => Text displays as "HELLOWORLD"
2) Move cursor to the start of "HELLOWORLD"
3) Enter "XXX" => Text displays as "XXXHELLOWORLD"
4) Type CTRL-Z (Undo) => Text displays as "HELLOWORLD"
5) Type CTRL-Z (Undo) => Text displays as "XXXHELLOWORLD"
6) Type CTRL-Z (Undo) => Text displays as "HELLOWORLD"
7) Move cursor to the start of "HELLOWORLD"
8) Enter "aaa" => Text displays as "AAAHELLOWORLD"
9) Type CTRL-Z (Undo) => Text remains unchanged (should change to "HELLOWORLD")
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TextExample_UndoBug {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(100, 100, 200, 100);
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.SINGLE);
text.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
e.text = e.text.toUpperCase();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}