简体   繁体   中英

Set the list of acceptable values in a GWT ValueListBox based on an EnumSet?

Given that I have this entity as part of an editor chain:

public class Commission implements Serializable
{   

    private EnumSet<CommissionType> commissionTypes;
    private CommissionType type; // must exist in commissionTypes

    private String value;

    public Commission()
    {

    }
}

and this editor for it:

public class CommissionEditor extends Composite implements Editor<Commission>
{
    private static CommissionEditorUiBinder uiBinder = GWT.create(CommissionEditorUiBinder.class);

    interface CommissionEditorUiBinder extends UiBinder<Widget, CommissionEditor>
    {
    }

    @UiField(provided = true)
    ValueListBox<CommissionType> type = new ValueListBox<CommissionType>(new AbstractRenderer<CommissionType>()
    {
        @Override
        public String render(CommissionType object)
        {
            return object == null ? "" : object.toString();
        }
    });

    @UiField
    TextBox value;

    public CommissionEditor()
    {
        type.setAcceptableValues(Arrays.asList(CommissionType.values()));

        initWidget(uiBinder.createAndBindUi(this));
    }

}

At the moment the ValueListBox renders all possible options for CommissionType, like this:

ValueListBox呈现枚举的所有可能值

The EnumSet could contain between 1 and 4 of the possible options, depending on the particular entity. Is there a way to make the ValueListBox only render the options in the EnumSet and then save the value in commissionType?

Bear in mind that I want to set the value of commissionType as well.

There are two ways to solve it:

1.) If you have a direct access to the CommissionEditor then create a setter in it call it when you edit the entity:

public void setAcceptableValues(List<CommissionType> values) {
    type.setAcceptableValues(values);

}

And call it like this when you call driver.edit(entity); :

commissionEditor.setAcceptableValues(commission.getCommissionTypes());

2.) Instead of extending the Editor interface you can extend the ValueAwareEditor and in the setValue() method call setAcceptableValues with the corresponding values.

Approach 2 is probably the cleaner approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM