Monthly Archive for: ‘December, 2008’

Eclipse Databinding + Validation + Decoration


When it comes to databinding, a very common use case is to decorate form fields with little markers that indicate the state of the control. The following example explains how to do this with Eclipse databinding and JFace. Here is a screenshot:

Validation Decoration

The code to create an error decoration for a control is pretty straight forward:

ControlDecoration controlDecoration = new ControlDecoration(
    control, SWT.LEFT | SWT.TOP);
FieldDecoration fieldDecoration = FieldDecorationRegistry

After creating the error decoration, you can implement a databindig validator that checks the condition and hides or shows the decoration accordingly:

class StringRequiredValidator implements IValidator {

    private final String errorText;
    private final ControlDecoration controlDecoration;

    public StringRequiredValidator(String errorText,
        ControlDecoration controlDecoration) {
        this.errorText = errorText;
        this.controlDecoration = controlDecoration;

    public IStatus validate(Object value) {
        if (value instanceof String) {
            String text = (String) value;
            if (text.trim().length() == 0) {
                return ValidationStatus
        return Status.OK_STATUS;

Now you can bind a model property to a text field with an UpdateStrategy using your validator:

    SWTObservables.observeText(firstNameText, SWT.Modify),
    PojoObservables.observeValue(person, "firstName"),
    new UpdateValueStrategy()
        .setAfterConvertValidator(new StringRequiredValidator(
             "Please enter first name",

If you want to use (existing) validators without handing the control decoration over, you could create a map for controls and their corresponding decorators, and then use an AggregateValidationStatus to control the decorators:

AggregateValidationStatus aggregateValidationStatus
 = new AggregateValidationStatus(

   .addChangeListener(new IChangeListener() {
    public void handleChange(ChangeEvent event) {
      for (Object o : dataBindingContext.getBindings()) {
        Binding binding = (Binding) o;
        IStatus status = (IStatus) binding
        Control control = null;
        if (binding.getTarget() instanceof ISWTObservable) {
          ISWTObservable swtObservable =
             (ISWTObservable) binding.getTarget();
          control = (Control) swtObservable.getWidget();
        ControlDecoration decoration
          = decoratorMap.get(control);
        if (decoration != null) {
          if (status.isOK()) {
          } else {

You can download an Eclipse project with the running examples here: Binding Decoration Examples. I also implemented this in my RCP demo project MP3 Manager.

Have Fun!


I am speaking at EclipseCon

I am very happy that my “Advanced Eclipse Rich Client Platform Tutorial” (together with Ben Pasero), my little “e4 RCP CSS Styling Demo“, and also the tutorial “RCP Mail 2.0: Commands, Common Navigator, and Data Binding” (together with Boris Bokowski, Frank Gerhardt, Francis Upton IV, and Michael Scharf) were accepted at EclipseCon 2009.

I am looking forward to seeing you in Santa Clara!

Have Fun