gwtuibindergwt-platformgwt-editors

Gwt-platform with UiBinder and UiEditors Framework (GWT Editors)


I struggle with Editor framework of gwt. Mostly because the documentation is lame- no hurt feelings- just saying.

Now I have problem that I cannot execute button event on editor. This is my error:

Uncaught com.google.gwt.event.shared.UmbrellaException: Exception caught: (TypeError) : Cannot read property 'onLoginButtonClick_3_g$' of undefined

I am not sure what I am doing wrong. I didn't found any good example of that. I hope someone will help.

Here is my code:

Editor

public class LoginEditor extends ViewWithUiHandlers<LoginEditorUiHandlers> implements Editor<LoginModel> {

    private VerticalPanel widget = new VerticalPanel();
    MaterialTextBox email = new MaterialTextBox();
    MaterialTextBox password = new MaterialTextBox();
    MaterialButton btnLogin = new MaterialButton();



    public LoginEditor() {
        initWidget(widget);

        email.setPlaceholder("E-mail");
        password.setPlaceholder("Password");
        btnLogin.setText("Login");
        btnLogin.addClickHandler(new ClickHandler() {
            @Override public void onClick(ClickEvent event) {
                onLoginButtonClick(event);
            }
        });

        widget.add(email);
        widget.add(password);
        widget.add(btnLogin);
    }


    void onLoginButtonClick(ClickEvent e){
        getUiHandlers().onLoginButtonClick();
        Window.alert("TEST");
    }
}

Presenter

public class LoginPresenter extends Presenter<LoginPresenter.MyView, LoginPresenter.MyProxy> implements LoginEditorUiHandlers {
    public interface MyView extends View , HasUiHandlers<LoginEditorUiHandlers> {}
    public static final Type<RevealContentHandler<?>> SLOT_Login = new Type<RevealContentHandler<?>>();

    @ProxyStandard
    @NameToken(NameTokens.login)
    public interface MyProxy extends ProxyPlace<LoginPresenter> {}

    // Editor
    interface Driver extends SimpleBeanEditorDriver<LoginModel, LoginEditor> {}
    private static final LoginService service = GWT.create(LoginService.class);
    Driver editorDriver = GWT.create(Driver.class);
    private LoginModel model = new LoginModel("email","pass");
    private LoginEditor editor = new LoginEditor();

    @Override
    public void onLoginButtonClick() {


        MaterialToast.fireToast("TEST");
        try{
            System.out.println(editorDriver == null);
            System.out.println(editorDriver.isDirty());
            editorDriver.isDirty();
        } catch (NullPointerException e) {
            MaterialToast.fireToast("Null: " + e.getLocalizedMessage());            
        }
        if (editorDriver.isDirty()) {
            model = editorDriver.flush();
            if (editorDriver.hasErrors()) {
                StringBuilder errorBuilder = new StringBuilder();
                for (EditorError error : editorDriver.getErrors()) {
                    errorBuilder.append(error.getMessage() + "\n");
                }
                MaterialToast.fireToast(errorBuilder.toString());
            } else {
                service.login(
                        model, new MethodCallback<Integer>() {
                            @Override
                            public void onSuccess(Method method, Integer response) {
                                MaterialToast.fireToast("Succefully set info. status code: " + response);
                            }
                            @Override
                            public void onFailure(Method method, Throwable exception) {
                                MaterialToast.fireToast("Error setting");
                            }
                    });
            }
        } else {
            MaterialToast.fireToast("Data has not changed");
        }
    }




    @Inject
    LoginPresenter(
            EventBus eventBus,
            MyView view, 
            MyProxy proxy) {
        super(eventBus, view, proxy, RevealType.Root);

        editorDriver.initialize(editor);
        editorDriver.edit(model);

        getView().setUiHandlers(this);
    }
}

View

public class LoginView extends ViewWithUiHandlers<LoginEditorUiHandlers> implements LoginPresenter.MyView {
    interface Binder extends UiBinder<Widget, LoginView> {
    }

    @Inject
    LoginView(Binder uiBinder) {

        initWidget(uiBinder.createAndBindUi(this));

    }
}

View.ui.xml

<m:MaterialRow ui:field="loginWidget">

    <m:MaterialColumn grid="s12 m4 l4" offset="l4" >
            <m:MaterialTitle title="Login" description="Please provide your account credentials."/>
            <m:MaterialPanel padding="5" shadow="1" addStyleNames="{style.panel}">
                <m:MaterialPanel addStyleNames="{style.fieldPanel}">

                    <e:LoginEditor></e:LoginEditor>
                </m:MaterialPanel>
            </m:MaterialPanel>
    </m:MaterialColumn>

</m:MaterialRow>

UiHandlers

interface LoginEditorUiHandlers extends UiHandlers {
    void onLoginButtonClick();
}

interface LoginUiHandlers extends UiHandlers {
}

Solution

  • Here is working solution:

    Presenter

    package pl.korbeldaniel.cms.client.login;
    
    import java.util.ArrayList;
    import java.util.Set;
    
    import javax.validation.ConstraintViolation;
    import javax.validation.Validation;
    import javax.validation.Validator;
    
    import org.fusesource.restygwt.client.Method;
    import org.fusesource.restygwt.client.MethodCallback;
    
    import gwt.material.design.client.ui.MaterialToast;
    
    import com.google.gwt.core.shared.GWT;
    import com.google.gwt.editor.client.EditorError;
    import com.google.gwt.editor.client.SimpleBeanEditorDriver;
    import com.google.gwt.event.shared.GwtEvent.Type;
    import com.google.gwt.user.client.ui.Label;
    import com.google.gwt.user.client.ui.RootPanel;
    import com.google.inject.Inject;
    import com.google.web.bindery.event.shared.EventBus;
    import com.gwtplatform.mvp.client.Presenter;
    import com.gwtplatform.mvp.client.annotations.ProxyStandard;
    import com.gwtplatform.mvp.client.proxy.ProxyPlace;
    import com.gwtplatform.mvp.client.annotations.NameToken;
    import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
    import com.gwtplatform.mvp.client.HasUiHandlers;
    
    import pl.korbeldaniel.cms.client.editor.BeanEditView;
    import pl.korbeldaniel.cms.client.model.LoginModel;
    import pl.korbeldaniel.cms.client.place.NameTokens;
    import pl.korbeldaniel.cms.client.service.LoginService;
    
    public class LoginPresenter extends Presenter<LoginPresenter.MyView, LoginPresenter.MyProxy> implements LoginUiHandlers {
        public interface MyView extends BeanEditView<LoginModel>, HasUiHandlers<LoginUiHandlers> {}
        public static final Type<RevealContentHandler<?>> SLOT_Login = new Type<RevealContentHandler<?>>();
    
        @ProxyStandard
        @NameToken(NameTokens.login)
        public interface MyProxy extends ProxyPlace<LoginPresenter> {}
    
        // Editor
        private SimpleBeanEditorDriver<LoginModel, ?> editorDriver;
        private static final LoginService service = GWT.create(LoginService.class);
        private LoginModel model = new LoginModel("","");
    
    
        @Override
        public void onLoginButtonClick() {
            String msg = "User Pressed a button.";
            GWT.log(msg);
    
            if (editorDriver.isDirty()) {
                model = editorDriver.flush();
                validateModel();
    
                GWT.log(String.valueOf(editorDriver.hasErrors()));
                if (editorDriver.hasErrors()) {
                    MaterialToast.fireToast("Errors occur");
                    StringBuilder errorBuilder = new StringBuilder();
                    for (EditorError error : editorDriver.getErrors()) {
                        GWT.log(error.getMessage());
                        errorBuilder.append(error.getMessage() + "\n");
                    }
                    //MaterialToast.fireToast(errorBuilder.toString());
                    RootPanel.get().add(new Label(errorBuilder.toString()));
                } else {
                    service.login(
                            model, new MethodCallback<Integer>() {
                                @Override
                                public void onSuccess(Method method, Integer response) {
                                    MaterialToast.fireToast("Succefully set info. status code: " + response);
                                }
                                @Override
                                public void onFailure(Method method, Throwable exception) {
                                    MaterialToast.fireToast("Error setting");
                                }
                        });
                }
            } else {
                MaterialToast.fireToast("Data has not changed");
            }
        }
    
    
    
    
        private void validateModel() {
            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            Set<ConstraintViolation<LoginModel>> violations = validator.validate(model);
            GWT.log(String.valueOf(violations.size()));
            if (violations.size() > 0) {
                editorDriver.setConstraintViolations(new ArrayList<ConstraintViolation<?>>(violations));
            }
    
        }
    
    
        @Inject
        LoginPresenter(EventBus eventBus,MyView view, MyProxy proxy) {
            super(eventBus, view, proxy, RevealType.Root);
            getView().setUiHandlers(this);
            editorDriver = getView().createEditorDriver();
            editorDriver.edit(model);
    
        }
    
    }
    

    View/Editor

    package pl.korbeldaniel.cms.client.login;
    
    
    import gwt.material.design.client.ui.MaterialButton;
    import gwt.material.design.client.ui.MaterialCheckBox;
    import gwt.material.design.client.ui.MaterialTextBox;
    
    import javax.inject.Inject;
    
    import pl.korbeldaniel.cms.client.model.LoginModel;
    
    import com.google.gwt.core.client.GWT;
    import com.google.gwt.editor.client.SimpleBeanEditorDriver;
    import com.google.gwt.event.dom.client.ClickEvent;
    import com.google.gwt.event.dom.client.ClickHandler;
    import com.google.gwt.uibinder.client.UiBinder;
    import com.google.gwt.uibinder.client.UiField;
    
    import com.google.gwt.user.client.ui.Widget;
    import com.gwtplatform.mvp.client.ViewWithUiHandlers;
    
    public class LoginView extends ViewWithUiHandlers<LoginUiHandlers> implements LoginPresenter.MyView {
        interface Binder extends UiBinder<Widget, LoginView> {}
    
        /** The driver to link the proxy bean with the view. */
        public interface EditorDriver extends SimpleBeanEditorDriver<LoginModel, LoginView> { }
    
        @UiField MaterialTextBox email;
        @UiField MaterialTextBox password;
        @UiField MaterialButton loginButton;
        @UiField MaterialCheckBox keepMeLoggedInCheckbox;
    
        @Inject
        LoginView(Binder uiBinder) {
            initWidget(uiBinder.createAndBindUi(this));
            addClickHandlerToLoginButton();
        }
    
    
    
        //@UiHandler("loginButton")
        private void onLoginButtonClick(ClickEvent e){
            getUiHandlers().onLoginButtonClick();
        }
        private void addClickHandlerToLoginButton() {
            loginButton.addClickHandler(new ClickHandler() {
                @Override public void onClick(ClickEvent event) {
                    onLoginButtonClick(event);
                }
            });     
        }
        @Override
        public SimpleBeanEditorDriver<LoginModel, ?> createEditorDriver() {
            EditorDriver driver = GWT.create(EditorDriver.class);
            driver.initialize(this);
            return driver;
        }
    }
    

    ValidatorFactory

    package pl.korbeldaniel.cms.client.login;
    
    import javax.validation.Validator;
    
    import pl.korbeldaniel.cms.client.model.LoginModel;
    
    import com.google.gwt.core.client.GWT;
    import com.google.gwt.validation.client.AbstractGwtValidatorFactory;
    import com.google.gwt.validation.client.GwtValidation;
    import com.google.gwt.validation.client.impl.AbstractGwtValidator;
    
    public final class SampleValidatorFactory extends AbstractGwtValidatorFactory {
    
        /**
         * Validator marker for the Validation Sample project. Only the classes and
         * groups listed in the {@link GwtValidation} annotation can be validated.
         */
        @GwtValidation(LoginModel.class)
        public interface GwtValidator extends Validator {
        }
    
        @Override
        public AbstractGwtValidator createValidator() {
            return GWT.create(GwtValidator.class);
        }
    }
    

    UiHandlers

    package pl.korbeldaniel.cms.client.login;
    
    
    import com.gwtplatform.mvp.client.UiHandlers;
    
    interface LoginUiHandlers extends UiHandlers {
        void onLoginButtonClick();
    }
    

    Bean edit view interface

    package pl.korbeldaniel.cms.client.editor;
    
    import com.google.gwt.editor.client.Editor;
    import com.google.gwt.editor.client.SimpleBeanEditorDriver;
    import com.gwtplatform.mvp.client.View;
    
    /**
     * Implemented by views that edit beans.
     *
     * @param <B> the type of the bean
     */
    public interface BeanEditView<B> extends View, Editor<B> {
        /**
         * @return a new {@link SimpleBeanEditorDriver} initialized to run this editor
         */
        SimpleBeanEditorDriver<B, ?> createEditorDriver();
    }
    

    I hope it will help some one.