ModifyListener listener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
String color = text.getText();
if (RGBA_REGEXP.matcher(color).matches()) {
getManagedForm().getMessageManager().removeMessage(text, text);
} else {
getManagedForm().getMessageManager().addMessage(text,
"Accepted color formats are one of #RGB, #ARGB, #RRGGBB or #AARRGGBB.",
null /* data */, IMessageProvider.ERROR, text);
}
}
};
text.addModifyListener(listener);
// Make sure the validator removes its message(s) when the widget is disposed
text.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
getManagedForm().getMessageManager().removeMessage(text, text);
}
});
// Finally call the validator once to make sure the initial value is processed
listener.modifyText(null);