0
votes

I am trying to make use of Editor in a GWT application, so I have read the official documentation. I have also looked at he question enter link description here and its answers, but I still cannot figure out the ultimate "purpose" of Editors. As an example case, supposing a UiBinder with some fields:

@UiField
TextBox name;

@UiField
TextArea comment;

@UiField
ListBox type;

...

I create additionally a method editCustomer as:

private void editCustomer(CustomerProxy entity) {

MyRequestFactory requestFactory = MyRequest.getRequestFactory();

CustomerRequestContext requestContext = requestFactory.customerRequest();

entity = requestContext.edit(entity);

editorDriver.edit(entity, requestContext);
}

I think the approach with Editor makes for connecting UiBinder fields with the Database. How is this done, based on the common way of sending data in the database through a "Save" Buttton?

@UiHandler("saveButton")
void onSaveButtonClick(ClickEvent event){
????
}
1
It is proper to add a comment explaining what you find oblique or awkward with question when you vote down it.arjacsoh

1 Answers

0
votes

I have been using the MVP pattern for a while and have some more complicated editors. I found that it is good put put your EditorDriver in your view becuase when you initialize it you can bind it to your specific view. My examples require an activity / view interface / view implementation.

This is an abstract activity that can be extended by other activities but I included the relevant content. I have stripped quite a bit of code out but this should give you an idea of a useful way to use editors. My editor is quite complex and has quite a few sub editors. I have only included the name and description. We have found this to be a quite useful design pattern for handling Editors.

public abstract class AbstractTaskBuilderActivity extends <E extends AnalyticsTaskProxy, R extends DaoRequest<E>> implements TaskBuilderView {


/**
 * Create a new task. This will initialize any new lists.
 * @param context The RequestContext to use to create the task.
 * @param clazz The class type to be created. 
 * @return
 */
protected E create(R context, Class<E> clazz) {
            // This is implemented in my inherited classes.
    E editableAnalyticsTask = context.create(clazz);
            // More initialization code expecially initializing arrays to empty so that 
            // any ListEditor sub editors will work.


    return editableAnalyticsTask;
}

/**
 * Call to edit the task and update the dashboards. 
 * @param context
 * @param task
 */
protected void doEdit(R context, E task) {
    RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver();
    E editable = context.edit(task);
    context.save(editable).with(driver.getPaths()).to(new Receiver<Long>() {

        @Override
        public void onFailure(ServerFailure error) {
            display.showError(error.getMessage());
            super.onFailure(error);
        }

        public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
            display.getEditorDriver().setConstraintViolations(violations);
        }

        @Override
        public void onSuccess(Long response) {
            clientFactory.getPlaceController().goTo(getSavePlace());
        }

    });
    driver.edit(editable, context);
}


    /**
     * Save the task.
     */
        @Override
    public void onSaveTask() {

        RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver();

        RequestContext request = driver.flush();
        request.fire();
    }

}

My view interface

public interface TaskBuilderView extends View {

    public interface Presenter {
        void onSaveTask();
    }

    public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getFactoryEditorDriver();

    public void setPresenter(Presenter presenter);
}

My view implementation

public class AnalyticsTaskBuilderViewImpl extends ViewImpl implements AnalyticsTaskBuilderView, Editor<AnalyticsTaskProxy> {

    interface AnalyticsTaskBuilderDriver extends RequestFactoryEditorDriver<AnalyticsTaskProxy, AnalyticsTaskBuilderViewImpl> {
    }

    /**
     * UiBinder implementation.
     * 
     * @author chinshaw
     * 
     */
    @UiTemplate("AnalyticsTaskBuilderView.ui.xml")
    interface Binder extends UiBinder<Widget, AnalyticsTaskBuilderViewImpl> {
    }

    /**
     * Name component for the name of the analytics operation.
     * This also implements {@link HasEditorErrors so it can show 
     * constraint violations when an error occurs.
     */
    @UiField
    ValueBoxEditorDecorator<String> name;

    /**
     * Description component that edits analytics operation description.
     * This also implements {@link HasEditorErrors} so it can show
     * constraint violations when an error occurs
     */
    @UiField
    ValueBoxEditorDecorator<String> description;

    public AnalyticsTaskBuilderViewImpl(Resources resources) {
        super(resources);
        // Must initialize the view before calling driver initialize 
        initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this));
        driver.initialize(this);
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
        bindToPresenter();
    }

    // Save the task
    @UiHandler("saveTask")
    void handleClick(ClickEvent clickEvent) {
        presenter.onSaveTask();
    }

    @Override
    public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getEditorDriver() {
        return driver;
    }
}