Subversion Repositories SmartDukaan

Rev

Rev 1962 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.catalog.dashboard.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Login screen for the catalog dashboard. 
 * <p>In EntryPoint class CatalogDashboard.java, LoginScreen is initialised and added to the root panel.
 * If authentication is successful, root panel is cleared and dashboard widgets are added to the root panel.
 */
public class LoginScreen extends Composite {
    private Label errLabel = new Label("");
    private Label headerMessage = new Label("Please key in your credentials");
    private Label userLabel = new Label("Username");
    private Label passwordLabel = new Label("Password");
    private TextBox userTextBox = new TextBox();
    private PasswordTextBox passwordTextBox = new PasswordTextBox();
    private Button submitButton = new Button("Login");
    
    interface LoginListener {
        void onLoginPressed(String username, String password);
    }
    private LoginListener loginListener;

    public LoginScreen(){
        
        //errLabel.setVisible(false);
        VerticalPanel vertPanel = new VerticalPanel();
        vertPanel.setSpacing(5);
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        horizontalPanel.setSpacing(5);
        vertPanel.add(headerMessage);
        vertPanel.add(errLabel);
        vertPanel.add(horizontalPanel);
        horizontalPanel.add(userLabel);
        horizontalPanel.add(userTextBox);
        horizontalPanel.setCellWidth(horizontalPanel.getWidget(0), "128px");
        horizontalPanel.setCellWidth(horizontalPanel.getWidget(1), "128px");
        vertPanel.setCellHeight(vertPanel.getWidget(0), "10px");
        vertPanel.setCellHeight(vertPanel.getWidget(1), "10px");
        vertPanel.setCellHeight(vertPanel.getWidget(2), "10px");
        horizontalPanel = new HorizontalPanel();
        horizontalPanel.setSpacing(5);
        vertPanel.add(horizontalPanel);
        horizontalPanel.add(passwordLabel);
        horizontalPanel.add(passwordTextBox);
        horizontalPanel.setCellWidth(horizontalPanel.getWidget(0), "128px");
        horizontalPanel.setCellWidth(horizontalPanel.getWidget(1), "128px");
        vertPanel.setCellHeight(vertPanel.getWidget(3), "10px");
        vertPanel.add(submitButton);
        vertPanel.setStyleName("centerBox");
        
        submitButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                loginListener.onLoginPressed(userTextBox.getText(), passwordTextBox.getText());
            }
        });
        initWidget(vertPanel);
    }
    
    public void setLoginListener(LoginListener loginListener) {
        this.loginListener = loginListener;
    }

    public void setErrorText(String errorMessage) {
        errLabel.setText(errorMessage);
    }
    
    public void clearFields() {
        userTextBox.setText("");
        passwordTextBox.setText("");
    }
}