javagwttextboxuibinder

How to iterate over TextBox?


I am using MVP pattern. In my View, I am using TextBox named uiTextBox in which I have added my enum class Message Status using following code in the method initStaticContent.

private void initStaticContent() {
    for (MessageStatus status : EnumSet.allOf(MessageStatus.class))
        uiTextStatus.setText(status.name());
    updateUiWithPermissions();
}

My Enum Class is:

public enum MessageStatus {
    SENDING, SENT, FAILED;
}

And in my updateUiWithModel() I am performing following steps :

private void updateUiWithModel() {
    if (model == null)
        throw new IllegalStateException("A model must be defined to update the ui.");

    uiTextCreatedOn.setText(model.createdOn);

    uiTextBusinessName.setText(model.businessName);

    uiTextSenderStaffFirstName.setText(model.senderStaffFirstName);

    selectStatus(model.status);

     case SEND:{
        uiTextRecipientEmail.setVisible(true);
        uiTextRecipientSms.setVisible(false);
        uiTextRecipientVoice.setVisible(false);
     }break;

    case SENDING:{
        uiTextRecipientEmail.setVisible(false);
        uiTextRecipientSms.setVisible(true);
        uiTextRecipientVoice.setVisible(false);
    }break;

    case SENT:{
        uiTextRecipientEmail.setVisible(false);
        uiTextRecipientSms.setVisible(false);
        uiTextRecipientVoice.setVisible(true);  
    }break;
    }

    public void selectStatus(MessageStatus status) {
    if(status == null){
        uiTextStatus.setText("");
        return;
    }
    for(int i=0; i < uiTextStatus.getMaxLength(); i++){
        String currentStatus = (uiTextStatus.getText(i));
        if (currentStatus == status.name()) {
            uiListBoxMethod.setSelectedIndex(i);
            break;
    }
}
}

Firstly, I want to know how to iterate in for each over uiTextBox and count the items in the textbox.

Secondly, if I use Listbox instead of TextBox and I want to have switch case in the Listbox how can I do it in method updateUiWithModel.


Solution

  • Well you can create a TextFieldStatusManipulator class that sets text and visibility state.

    class TextFieldStatusManipulator {
        private final Function<Model,String> extractString;
        private final Predicate<Status> setVisible;
        private final TextBox textBox;
        public TextFieldStatusManipulator(TextBox box, Function<Model,String> ex, Predicate<Status> sv) {
            textBox = box; extractString = ex; setVisible = sv;
        }
        public void statusChange(Model model) {
            textBox.setText(extractString.apply(model));
            textBox.setVisible(setVisible.test(model.status));
        }
    }
    

    Then you create one for each TextField, like so

    TextFieldStatusManipulator createdOnManip = new TextFieldStatusManipulator (uiTextCreatedOn, Model::getCreatedOn, s -> s == Status.SEND);
    

    Then you add them to a list and iterate the list on status changes.

    manipulatorList.forEach(m -> m.statusChange(model));