0
votes

i had this task: I have Presenter/View Couple (lets call it ItemListPresenter) showing a Celltable. Now i wanted to edit an item by double-clicking it (or pressing a button, whatever). Then a popup dialog should appear (Lets call it PopupWidget), letting me edit my item properties. i found a solution how to do it, but i am not sure it is the "right" way. Since i am trying to learn the philosophy behind GWT/GWTP, i would appreciate if you could give me hints what i did right and what i did wrong:

In the onbind method of ItemListPresenter, i wire the CellTable with a DoubleClick handler:

getView().getCellTable().setSelectionModel(selectionModel);
getView().getCellTable().addDomHandler(new DoubleClickHandler() {           
    @Override           
    public void onDoubleClick(final DoubleClickEvent event) {
        DeviceDto selectedDeviceDto = selectionModel.getSelectedObject();
        //TODO: Figure out how to best handle editing of DeviceDto
        if (selectedDeviceDto != null) {
            devicesDialog.setCurrentDeviceDTO(selectedDeviceDto);
            addToPopupSlot(devicesDialog);
        }
} }, DoubleClickEvent.getType());

What does not feel right is setting the object i want to edit (selectedDeviceDto) in the Dialog Presenter Widget. Is this the "right" way?

My popup presenter which is defined as

public class DeviceEditDialogPresenterWidget extends PresenterWidget<DeviceEditDialogPresenterWidget.MyView> implements
            DeviceEditDialogUiHandlers {

is still ugly, since i just set every property into a text box and after editing, i recollect the properties and rebuild the object. This is messy and i guess i should GWT Editors for that. However, when i click the "Save" Button in the dialog, an UiHandler is triggered:

@UiHandler("okButton")
void okButtonClicked(ClickEvent event) {
    DeviceDto dev = new DeviceDto(idBox.getText(), deviceIdBox.getText(),    typeBox.getText(), firmwareVersionBox.getText(), userBox.getText(), statusBox.getText());
    getUiHandlers().updateDevice(dev);
    hide();
} 

This triggers my DeviceEditDialogPresenterWidget, which itself fires and event:

@Override
public void updateDevice(DeviceDto device) {
    eventBus.fireEvent(new DeviceUpdatedEvent(device));
}

This event is caught by a handler in the "mother" presenter with the CellTable, which is wired in the onBind method again:

addRegisteredHandler(DeviceUpdatedEvent.TYPE, new DeviceUpdatedEvent.DeviceUpdatedHandler() {
    @Override
    public void onDeviceUpdatedEvent(DeviceUpdatedEvent event) {
        updateDevice(event.getDevice());
    }
}); 

I would really like to avoid going down a road of messy mistakes, so any hints would be appreciated.

Thanks Arthur

1

1 Answers

0
votes

PresenterWidgets are usually designed to have a setter which is used to set a Model or DTO that the PresenterWidget works on (the same way you did it).

Alternatively you can avoid a PresenterWidget and use an Editor (extends Composite) that you manually add to a PopupPanel or DialogBox in your ListItemEditor.

This way you avoid the complexity of a PresenterWidget. But you have to handle the clicks (i.e. save button) from the ListItemPresenter. I would always try to start small (use a composite) and if you realize that you might need the functionality also in other places, create a PresenterWidget.

Also you don't need the updateDevice method because you pass a reference to your DTO. You only need to fresh the CellTable.
But apart from that your approach looks fine.