Question

i have this issue: I have a PresenterWidget which contains sub-editors. There are "container" elements which should be editable by this widget. These containers can be assigned to groups. To do so, i would like to fetch a list of all available groups from the server. So the widget is set up like this (i use GWTP):

public class ContainerEditorDialogPresenterWidget extends PresenterWidget<ContainerEditorDialogPresenterWidget.MyView> implements
        ContainerEditorDialogUiHandlers {

private final PlaceManager placeManager;
private List<GroupDTO> groupList = new ArrayList<GroupDTO>();
private final DispatchAsync dispatcher;

@Inject
ContainerEditorDialogPresenterWidget(EventBus eventBus,
                           MyView view, PlaceManager placeManager, DispatchAsync dispatcher) {
    super(eventBus, view);
    getView().setUiHandlers(this);
    this.dispatcher = dispatcher;
    fetchGroups();
}
...
public void fetchGroups(){
    FetchGroupsAction action = new FetchGroupsAction();
    dispatcher.execute(action, new AsyncCallbackImpl<FetchGroupsResult>() {
        @Override
        public void onSuccess(FetchGroupsResult result) {
            groupList = result.getGroupDtos();
            eventBus.fireEvent(new GroupListUpdatedEvent(groupList));
        }

    });

}

So i call fetchGroups in the constructor to get it as early as possible. Since it is an AynchCallback, i get the result back "at some time". I then try to pass the values to the sub-editor with a GroupListUpdatedEvent. In there i have a Editor declared like this:

public class GroupListEditor extends Composite implements
        IsEditor<ListEditor<String, GroupItemEditor>> {

    private static StringListEditorUiBinder uiBinder = GWT
            .create(StringListEditorUiBinder.class);

    interface StringListEditorUiBinder extends
            UiBinder<Widget, GroupListEditor> {
    }
    //Gives us access to the event bus. 

    @Inject private EventBus eventBus;
...


    public GroupListEditor() {
         initWidget(uiBinder.createAndBindUi(this));
         eventBus.addHandler(GroupListUpdatedEvent.TYPE, new GroupListUpdatedEvent.GroupListUpdatedHandler() {
            @Override
            public void onGroupListUpdatedEvent(GroupListUpdatedEvent event) { 
                Log.debug("onContainerUpdatedEvent caught");

                allGroups = event.getGroupList();
                if(allGroups != null) {
                    for (GroupDTO g : allGroups) {
                        lbAllGroups.addItem(g.getName(), g.getId().toString());
                    }

                    lbAllGroups.setVisibleItemCount(5);
                    Log.debug("Item list = " + lbAllGroups.getItemCount());
                } else {
                    Log.debug("GROUP LIST is Null!");
                }


            }
        }); 

}

When i try to register the handler, i get an exception. So i expect the eventBus is not injected properly. What do i miss, how can i use events and the event bus if i am not in a Presenter? And: Is this the right way at all to populate Editors with "utility" data? I guess Editor should be related directly to the data they care for. But how do i handle this kind of supplemental data?

Thanks :)

Was it helpful?

Solution

Do you use @UiField in your ContainerEditorDialogPresenterWidgetView for your GroupListEditor ?
If so then Dependency Injection won't work because you basically manually create the GroupListEditor which leads to EventBus being NULL.

I would also use Constructor Injection instead of field injection.

GroupListEditor:

@Inject
public GroupListEditor(EventBus eventBus) {
this.eventBus = eventBus;
}

ContainerEditorDialogPresenterWidgetView:

public class ContainerEditorDialogPresenterWidgetView {

   @UiField(provided=true)
   GroupListEditor groupListEditor;

   @Inject
   public ContainerEditorDialogPresenterWidgetView(GroupListEditor groupListEditor);
      this.groupListEditor = groupListEditor;
      initWidget();
   }
}

Alternatively you could get an instance of your GroupListEditor via the Ginjector directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top