FieldGroup ignores Label during binding with bindMemberFields

Hello!

I use the FieldGroup class to bind fields to item properties with bindMemberFields. But I cannot use label because the Label class is not Field. It would be usefull to enchance the FieldGroup class to support the Label class.

Thanks in advance,
Anatoly.

Would it be possible for you to use e.g. a TextField in read-only mode instead?

Thank you, Anna. But your suggestion will not work because of the following FieldGroup method:

protected void configureField(Field<?> field) {
        field.setBuffered(isBuffered());

        field.setEnabled(isEnabled());

        if (field.getPropertyDataSource().isReadOnly()) {
            field.setReadOnly(true);
        } else {
            field.setReadOnly(isReadOnly()); // here is problem, because my FieldGroup is not read only
        }
    }

As the result my read only text field becomes editable for the none read only item property.

Yes, the read-only status must be set after that call, not before. It’s a bit of a hassle, but usually doable.

In my case I have used the following helper class:

public class LabelField extends Label implements Field<String> {

  private static final long serialVersionUID = 1L;

  @Override
  public void focus() {
    super.focus();
  }
  
  @Override
  public boolean isInvalidCommitted() {
    return false;
  }

  @Override
  public void setInvalidCommitted(boolean isCommitted) {
  }

  @Override
  public void commit() throws SourceException, InvalidValueException {
  }

  @Override
  public void discard() throws SourceException {
  }

  @Override
  public void setBuffered(boolean buffered) {
  }

  @Override
  public boolean isBuffered() {
    return false;
  }

  @Override
  public boolean isModified() {
    return false;
  }

  @Override
  public void addValidator(Validator validator) {
  }

  @Override
  public void removeValidator(Validator validator) {
  }

  @Override
  public void removeAllValidators() {
  }

  @Override
  public Collection<Validator> getValidators() {
    return null;
  }

  @Override
  public boolean isValid() {
    return true;
  }

  @Override
  public void validate() throws InvalidValueException {
  }

  @Override
  public boolean isInvalidAllowed() {
    return true;
  }

  @Override
  public void setInvalidAllowed(boolean invalidValueAllowed) throws UnsupportedOperationException {
  }

  @Override
  public int getTabIndex() {
    return 0;
  }

  @Override
  public void setTabIndex(int tabIndex) {
  }

  @Override
  public boolean isRequired() {
    return false;
  }

  @Override
  public void setRequired(boolean required) {
  }

  @Override
  public void setRequiredError(String requiredMessage) {
  }

  @Override
  public String getRequiredError() {
    return null;
  }

  @Override
  public boolean isEmpty() {
    return false;
  }

  @Override
  public void clear() {
  }

}

That’s certainly one way to do it, although not something that could be included in the framework, I think.

Anna, as for me Label is read only Field, so FieldGroup can process such element correctly.

In most use cases Label is simply an element that contains text and doesn’t have any field functionality, so having it implement Field interface by default would add lots of unnecessary code and processing. Setting the read-only status after the FieldGroup has done the default configuring is in my mind the more correct solution on framework level, because TextField pretty much
is
a field version of Label. I might change the FieldGroup handling to keep the default read only status of fields, though – it would have to be changed temporarily to set the default value, but it shouldn’t be much of an issue to return the original state after that is done.

Anna, as for me FieldGroup can be changed to support Property.Viewer as well as Field interface. That’s more correctly from binding point of view.