Search code examples
infinite-loopeclipse-scout

Eclipse Scout callback for entering value in fields


I would like to know if there is a function that fires up when user set value in field but not if program set value in field.

so function :

user click on field 'myField and change value -> method fires up
in program : myField.setValue = SomeValue; -> method doesn't fires up.

problem is with loop detection. If your logic is that you have 4 field and try to detect if any of those fields are changed and then fire method for change some values inside those fields :

@Override
protected void execChangedValue() throws ProcessingException {
  super.execChangedValue();
  valueFieldsChange(this);
}

protected void valueInPriceBoxFieldsChange(AbstractValueField field) {
    ... calculate some values in those fields....
}

and I get :

!MESSAGE org.eclipse.scout.rt.client.ui.form.fields.AbstractValueField.setValue(AbstractValueField.java:338) Loop detection in...

So I know the method execChangedValue() are not what I am looking for. Is there similar method with explained behavior ?

Marko


Solution

  • Let start to say that loop detection is useful in 90% of the cases. The warning is displayed when you are inside an execChangedValue() and you try to update the value of the same field.

    If I understand your example correctly you have inside your field:

    public class FirstField extends AbstractStringField {
    
      @Override
      protected void execChangedValue() throws ProcessingException {
        calculateNewValues();
      }
    }
    

    And the method:

    public void calculateNewValues() {
      //some logic to compute the new values: value1, value2, value3
    
      getFirstField().setValue(value1);
      getSecondField().setValue(value2);
      getThirdField().setValue(value3);
    }
    

    At this point, you really need to be sure that when the user sets the value in the FirstField to something, you might want to change it pragmatically to something else. This might be really confusing for the user.


    If you are sure that you need to update the value, there is a way to set a value without triggering execChangedValue() on the field. I have proposed a new method: setValueWithoutChangedValueTrigger(T value) on the Eclipse Scout Form: Controlling if execChangedValue() is called or not.

    On the forum you will find a snippet that you can add to your field (or in a common template: AbstractMyAppStringField).

    public class FirstField extends AbstractStringField {
    
      @Override
      protected void execChangedValue() throws ProcessingException {
        calculateNewValues();
      }
    
      public void setValueWithoutValueChangeTrigger(String value) {
        try {
          setValueChangeTriggerEnabled(false);
          setValue(value);
        }
        finally {
          setValueChangeTriggerEnabled(true);
        }
      }
    }
    

    And you will be able to use it:

    public void calculateNewValues() {
      //some logic to compute the new values: value1, value2, value3
    
      getFirstField().setValueWithoutChangedValueTrigger(value1);
      getSecondField().setValueWithoutChangedValueTrigger(value2);
      getThirdField().setValueWithoutChangedValueTrigger(value3);
    }
    

    I hope this helps.