首先,我在一个使用ldap进行登录身份验证的时间跟踪页面上工作。
对于登录,我创建了一个对话框,将verticalpanel设置为其小部件。我的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,但我认为这没有什么不同。
最佳答案
将侦听器类(例如,您的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() );
...
}
干杯,
关于java - GWT-从小部件获取按钮的“addClickHandler”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15428370/