Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Problem with datetime, validation and unit testing
[Databinding] Problem with datetime, validation and unit testing [message #665664] Fri, 15 April 2011 12:30 Go to next message
Mats-Ola Persson is currently offline Mats-Ola PerssonFriend
Messages: 2
Registered: February 2010
Junior Member
I have been experimenting a bit with databindings. I have a dialog whose button i'd like to disable in certain conditions and this property I'd like to have a test for (i.e. some property does not hold -> button is diabled).

I have done one test with a Text-field (that works) , I have also done a very similar test with a DateTime-field (that fails however it works when I run the application). See code below.

I have two questions:
1) Is there someway to programmatically trigger an validation?
2) Have I missed something? If no: is the inconsistency between the behavior for DateTime and Text intended?

public static class Status2BoolConverter implements IConverter {

    @Override
    public Object getFromType() {
      return IStatus.class;
    }



    @Override
    public Object getToType() {
      return Boolean.TYPE;
    }



    @Override
    public Object convert(Object fromObject) {
      return ((IStatus) fromObject).getSeverity() == IStatus.OK;
    }

  }



  @Test
  public void testInvalidStateWithText() throws Exception {
    DataBindingContext context = new DataBindingContext();
    Shell parent = new Shell();

    Text text = new Text(parent, SWT.BORDER);
    final IObservableValue target = WidgetProperties.text(SWT.Modify)
        .observe(text);

    final WritableValue model = new WritableValue("Hello, World", String.class);
    context.bindValue(target, model);

    context.addValidationStatusProvider(new MultiValidator() {

      @Override
      protected IStatus validate() {
        if (model.getValue().equals("Hi!")) {
          return ValidationStatus.error("fail!");
        }

        return ValidationStatus.ok();
      }
    });

    Button b = new Button(parent, SWT.PUSH);
    context.bindValue(WidgetProperties.enabled().observe(b),
                      new AggregateValidationStatus(context,
                                                    AggregateValidationStatus.MAX_SEVERITY),
                      new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER),
                      new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE).setConverter(new Status2BoolConverter()));

    text.setText("Hello!");
    assertTrue(b.isEnabled());

    text.setText("Hi!");

    assertFalse(b.isEnabled());
  }



  @Test
  public void testInvalidStateWithDate() throws Exception {

    DataBindingContext context = new DataBindingContext();
    Shell parent = new Shell();

    DateTime date = new DateTime(parent, SWT.BORDER);
    final IObservableValue target = WidgetProperties.selection().observe(date);

    final WritableValue model = new WritableValue(createDate(1999, 0, 0, 0, 0),
                                                  Date.class);
    context.bindValue(target, model);

    context.addValidationStatusProvider(new MultiValidator() {

      @Override
      protected IStatus validate() {
        Date modelDate = (Date) model.getValue();
        if (modelDate.before(createDate(1980, 0, 0, 0, 0))) {
          return ValidationStatus.error("fail!");
        }

        return ValidationStatus.ok();
      }
    });

    Button b = new Button(parent, SWT.PUSH);
    context.bindValue(WidgetProperties.enabled().observe(b),
                      new AggregateValidationStatus(context,
                                                    AggregateValidationStatus.MAX_SEVERITY),
                      new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER),
                      new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE).setConverter(new Status2BoolConverter()));

    date.setDate(1985, 0, 0);
    assertTrue(b.isEnabled());

    date.setDate(1975, 0, 0);
    assertFalse(b.isEnabled());
  }
Re: [Databinding] Problem with datetime, validation and unit testing [message #665786 is a reply to message #665664] Fri, 15 April 2011 22:48 Go to previous message
Carsten Habicht is currently offline Carsten HabichtFriend
Messages: 14
Registered: January 2011
Junior Member
Hey Mats-Ola,

I just did a little debugging and noticed some "irregularities":

1.: date.setDate(1975, 0, 0) does not work. The setDate() method does nothing if the internal call to DateTime.isValidDate() returns false, which happens when the day field is 0. To get over this, one has to use something like date.setDate(1975, 0, 1).

2.: The setDate() method, when called with a valid date, seems to effect a change in the DateTime's Text control, but I could not find any Selection event being triggered (which I would expect). This only seems to happen if a user actually does some key pressing . I don't know if this misbehaviour is fixed in current versions (I used 3.6.1). Maybe you should consider filing this as a bug.

Hope that helps a little.
Best
Carsten ;o)
Previous Topic:Zest Graph Viewer reference points to null all of a sudden
Next Topic:FileFieldEditor Help
Goto Forum:
  


Current Time: Thu Mar 28 07:55:04 GMT 2024

Powered by FUDForum. Page generated in 0.04600 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top