简体   繁体   中英

Enable buttons on the page when user logged in / GWT

I have a page in GWT with several "Edit" buttons. I added a login dialog box. By default the button are disabled.

What I want to do is enable buttons only after user logged in.

Also, as soon as I put several operators in if clause, it stops working. If I put only loggedIn = true , then it works (I mean it goes out of if clause and continues).

I'm using global boolean variable:

Boolean loggedIn = false;

Here is what I do:

public void login(final VerticalPanel vp, final RootPanel rp) {

    final DialogBox db = new DialogBox();
    db.setAnimationEnabled(true);
    db.setGlassEnabled(true);

    final VerticalPanel vp0 = new VerticalPanel();
    vp0.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    vp0.setWidth("100%");

    final HTML loginLabel = new HTML("Login:");
    loginLabel.setStyleName("h1");

    final FlexTable ft0 = new FlexTable();
    final FlexCellFormatter cf = ft0.getFlexCellFormatter();
    ft0.setCellSpacing(10);

    ft0.setHTML(0, 0, "Login:");
    cf.setStyleName(0, 0, "h3");
    cf.setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);

    final TextBox loginBox = new TextBox();
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        public void execute() {
            loginBox.setFocus(true);
        }
    });
    ft0.setWidget(0, 1, loginBox);

    ft0.setHTML(1, 0, "Password:");
    cf.setStyleName(1, 0, "h3");
    cf.setAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);

    final PasswordTextBox passwordBox = new PasswordTextBox();
    ft0.setWidget(1, 1, passwordBox);


    final Button loginButton = new Button(
        "Login", new ClickHandler() {
            public void onClick(ClickEvent event) {
                if ((loginBox.getText() == "1234") && passwordBox.getText() == "1234") {
                    final HTML loginSuccessMsg = new HTML("Logged in. Please wait...");
                    db.add(loginSuccessMsg);
                    loggedIn = true;

                    db.hide();
                    rp.clear();
                    rp.add(vp);
                }
                else {
                    final HTML loginErrorMsg = new HTML("Username/Password incorrect! Try again.");
                    db.add(loginErrorMsg);
                }



            }
        }
    );

    passwordBox.addKeyPressHandler(new KeyPressHandler() { 
        public void onKeyPress(KeyPressEvent event) { 
            if (((int)event.getCharCode()) == 13) { 
                loginButton.click();
            } 
        } 
    });

    ft0.setWidget(2, 0, loginButton);
    cf.setColSpan(2, 0, 2);
    cf.setAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

    vp0.add(ft0);
    db.add(vp0);
    int left = Window.getClientWidth() / 3;
    int top = Window.getClientHeight() / 3;
    db.setPopupPosition(left, top);
    db.show();
    //vp.add(vp0);

}

This is login method.

And here is where I use loggedIn variable:

if (loggedIn) butTotalNumQuick.setEnabled(true); else butTotalNumQuick.setEnabled(false);

Also, some more details - this button is disabled by default, so this works.

I think the problem is in the fact that page (panel) needs to be refreshed after login. This is what I tried to do with

rp.clear();
rp.add(vp);

where rp is RootPanel

There are multiple points which you should focus on:

Boolean loggedIn = false;

Why are you using an boolean object. If there is no special reason for it, which i do not exspect, then use the primitive type.

Also you should not use globals, that contradicts the oo principle. Especially when a variable represents a state. Redesign your application that you can access the variables which you need. If you need help doing this, ask or look at some tutorials.

 if ((loginBox.getText() == "1234") && passwordBox.getText() == "1234")

Third, do not save passwords on the client-side of you application. It is not hard to read them out of the JavaScript and then break into your application.

If your login procedure is just a placeholder for some server communication, which you want to add later, then you got one new problem too. This communication will probably be asynchronous, which you have to keep in mind if you design your application.

Also you last code snippet can be merged to:

butTotalNumQuick.setEnabled(loggedIn)

your mistake is that rp.add(vp); is adding the same vp that you just removed and

if (loggedIn) butTotalNumQuick.setEnabled(true); else butTotalNumQuick.setEnabled(false);

is never called. The easier way would be to access your button from inside the ClickHandler and set a Global variabel for the butTotalNumQuick button. No need to send the vp and rp as parameters in the login() method.

Example:

private Button butTotalNumQuick = new Button();

...

public void login() {

    ...

    if ((loginBox.getText() == "1234") && passwordBox.getText() == "1234") {
                    final HTML loginSuccessMsg = new HTML("Logged in. Please wait...");
                    db.add(loginSuccessMsg);
                    loggedIn = true;

                    butTotalNumQuick.setEnabled(loggedIn)
                }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM