2

首先,我正在使用 ldap 进行登录身份验证的时间跟踪页面。对于登录,我创建了一个对话框,将verticalpanel 设置为其小部件。我的垂直面板包含两个用于输入用户名/密码的文本框和一个用于将数据发送到 ldap 服务器的按钮。

在模块加载时弹出对话框并获取登录成功时的时间跟踪内容。

对于我的问题:“LoginWidget”和“LoginClickHandler”具有分离的类,因此我需要在模块加载时获取登录按钮以添加“LoginClickHandler”。

处理这个问题的最佳方法是什么?- 因为我知道我的解决方案不是很好。

LoginWidget 类

public class LoginWidget{
     private Button loginButton;
     private DialogBox dialogBox;
     private TextBox tbxUser;
     private PasswordTextBox tbxPw;
     private Label lblUser;
     private Label lblPW;
     private Label lblError;

public LoginWidget(){
    dialogBox = new DialogBox();
    tbxUser = new TextBox();
    tbxPw = new PasswordTextBox();
    lblUser = new Label();
    lblPW = new Label();
    lblError = new Label();
    dialogBox.setText("Login");
    dialogBox.setAnimationEnabled(true);
    loginButton = new Button("Login");
    // set the id of a widget by accessing its Element
    loginButton.getElement().setId("closeButton");
    final HTML serverResponseLabel = new HTML();
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.add(lblUser);
    lblUser.setText("User:");
    dialogVPanel.add(tbxUser);
    dialogVPanel.add(lblPW);
    lblPW.setText("PW:");
    dialogVPanel.add(tbxPw);
    dialogVPanel.add(lblError);
    dialogVPanel.add(serverResponseLabel);
    dialogVPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    dialogVPanel.add(loginButton);
    dialogBox.setWidget(dialogVPanel);
    dialogBox.show();
    dialogBox.center();
}

public Button getLoginButton(){
    return loginButton;     
}

    public DialogBox getDialogBox(){
    return dialogBox;
}

public TextBox getTbxUser(){
    return tbxUser;
}

public TextBox getTbxPw(){
    return tbxPw;
}

public Label getLblError(){
    return lblError;
}
}

LoginClickHandler 类

public class LoginClickHandler implements ClickHandler {
/**
*/
private LoginWidget lw;
private ServiceImplURL serviceImplURL;
private TimeTracking dtt = new TimeTracking();

public LoginClickHandler(ServiceImplURL sIU, LoginWidget _lw){
    /**
    */
    this.lw = _lw;
    this.serviceImplURL = sIU;
}

public void onClick(ClickEvent event) {
    /**
    */
    serviceImplURL.getRpcLdap().authenticate(lw.getTbxPw().getText(), lw.getTbxUser().getText(), new AsyncCallback<Boolean>() {
        /**
        */
        @Override
        public void onSuccess(Boolean isLdapAuthOk) {   
            /**
            */
            if(isLdapAuthOk){                       
                lw.getDialogBox().hide();
                User user = new User(lw.getTbxUser().getText(), lw.getTbxPw().getText());
                serviceImplURL.getRpcSession().setUsername(user.getUsername(), new AsyncCallback<Void>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onSuccess(Void result) {
                        // TODO Auto-generated method stub

                    }
                });
                serviceImplURL.getRpcDB().insertUser(user.getUsername(), new AsyncCallback<Void>() {
                    /**
                    */
                    @Override
                    public void onFailure(Throwable caught) {
                        /*
                         * connection error to implement
                         */                                     
                    }

                    @Override
                    public void onSuccess(Void result) {
                        /*
                         * do nothing
                         */                                     
                    }
                });
                dtt.loadContent();
            }
            else{
                lw.getLblError().setStyleName("error");
                lw.getLblError().setText("Passwort oder Username falsch!");
            }                           
        }

        @Override
        public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub

        }
    });
}
}

我的入口点

@Override
    public void onModuleLoad() {    
        /**
        */
        loadLoginWidget();

    }

    /**
     * Creates the login pop up.
     */
    public void loadLoginWidget(){
        /**
        */
        LoginWidget lw = new LoginWidget();
        LoginClickHandler lch = new LoginClickHandler(serviceImplURL, lw);
        lw.getLoginButton().addClickHandler(lch);
    }

我已经尝试过扩展 Button 但我不认为它有什么不同。

4

1 回答 1

0

将侦听器类(例如 your LoginClickHandler)传递给视图构造函数:

public LoginWidget( LoginClickHandler listener ) 
{
    ...
    loginButton.addClickHandler( listener )
    ...
}

更一般地,您可能想要插入定义操作的接口并LoginClickHandler实现它:

public interface LoginListener
{
    void onLogin();
    ...
}

public class LoginWidget
{
    public LoginWidget( LoginListener listener ) 
    {
        this.listener = listener;
        Button loginButton = new Button( "Login" );
        loginButton.addClickHandler( new ClickHandler()
        { 
            public void onClick( ClickEvent event ) { listener.onLogin(); }
        } );
    }
    ...
}

最后

public class LoginClickHandler implements LoginListener
{
    @Override public void onLogin()
    {
        serviceImplURL.getRpcLdap()...
        ...
    }
}

public void loadLoginWidget()
{
    LoginWidget lw = new LoginWidget( new LoginClickHandler() );
    ...
}

干杯,

于 2013-03-15T09:11:08.670 回答