带有 UiBinder 和 UiEditors 框架的 Gwt 平台(GWT 编辑器)

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

我对 gwt 的编辑器框架感到困惑。主要是因为文档是蹩脚的 - 没有伤害感情 - 只是说。

现在我遇到了无法在编辑器上执行按钮事件的问题。这是我的错误:

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

我不确定我做错了什么。我没有找到任何好的例子。希望有人帮忙

这是我的代码:

编辑

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");
    }
}

主持人

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);
    }
}

查看

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 {
}

这是可行的解决方案:

主持人

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;
    }
}

验证器工厂

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编辑视图界面

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();
}

希望对大家有所帮助。