Compoisite Component Permission

I have a composite component that extends a datefield to add “today” and “clear” buttons. These buttons should be disabled when the field is not editable or when it is not enabled. When I call setEditable or setEnabled from a screen controller, the field behaves as expected. However, the buttons do not seem to honor attribute permissions. If I set a date attribute as read-only as part of a role and use my custom component as the component in a form, the value field honors those permissions but I can’t figure out how to get the buttons to also honor them.

Attached are the definitions and controller for the composite component.

Help?

TodayDate.java (4.5 KB)
todaydate-component.xml (539 Bytes)
TodayDateLoader.java (6.2 KB)

Hello!

There is a two options I can suggest:

  1. Extend WebDateField and track if editable is changed. In TodayDate component subscribe to editable change event. For instance:
Exntension of WebDateFied
public class WebTodayDateField<V extends Comparable<V>> extends WebDateField<V> {

    @Override
    protected void setEditableToComponent(boolean editable) {
        super.setEditableToComponent(editable);

        publish(EditableChangeEvent.class, new EditableChangeEvent(this, editable));
    }

    public Subscription addEditableChangeListener(Consumer<EditableChangeEvent> listener) {
        return getEventHub().subscribe(EditableChangeEvent.class, listener);
    }

    public static class EditableChangeEvent extends EventObject {

        protected Boolean editable;

        public EditableChangeEvent(WebTodayDateField source, Boolean editable) {
            super(source);

            this.editable = editable;
        }

        @Override
        public WebTodayDateField getSource() {
            return (WebTodayDateField) super.getSource();
        }

        public Boolean getEditable() {
            return editable;
        }
    }
}
Extension loader
public class WebTodayDateFieldLoader extends DateFieldLoader {

    @Override
    public void createComponent() {
        resultComponent = factory.create(WebTodayDateField.NAME);
        loadId(resultComponent, element);
    }
}
TodayDate
private void onCreate(CreateEvent createEvent) {
    valueField = getInnerComponent("valueField");
    todayBtn = getInnerComponent("todayBtn");
    clearBtn = getInnerComponent("clearBtn");

    todayBtn.addClickListener(clickEvent -> setTodayDate());
    clearBtn.addClickListener(clickEvent -> clearDate());

    ((WebTodayDateField<Date>) valueField).addEditableChangeListener(editableChangeEvent -> {
        todayBtn.setEnabled(editableChangeEvent.getEditable());
        clearBtn.setEnabled(editableChangeEvent.getEditable());
    });
}
  1. Add state change listener to the ValueSource. The state will be active if an item is set to the container, and we can update the availability of buttons. However, it doesn’t work if an item won’t be loaded or we set ValueSource that already has an item to the component.