it.isislab.floasys.gui.dialogs.SignUpDialog.java Source code

Java tutorial

Introduction

Here is the source code for it.isislab.floasys.gui.dialogs.SignUpDialog.java

Source

/*    
 *   Copyright (c) 2012-2014, Dipartimento Informatica, Universit di Salerno (Italy) 
 *   [vitsca@dia.unisa.it, dpirozzi@unisa.it]
 *     
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *      
 *   Contributors:
 *      ISISLab, Dipartimento Informatica, Universit di Salerno (Italy) 
 *      [vitsca@dia.unisa.it, dpirozzi@unisa.it]
 */

package it.isislab.floasys.gui.dialogs;

import it.isislab.floasys.core.security.users.UserAccount;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog to register a new user.
 * @author Donato Pirozzi - dpirozzi@unisa.it
 */
public class SignUpDialog extends TitleAreaDialog {

    private static final long serialVersionUID = 1L;

    private Label lblUsername = null;
    private Label lblEmail = null;
    private Label lblPassword = null;
    private Label lblPassword2 = null;

    private Text txtUsername = null;
    private Text txtEmail = null;
    private Text txtPassword = null;
    private Text txtPassword2 = null;

    private UserAccount userAccount = null;

    public SignUpDialog(Shell parentShell) {
        super(parentShell);
    }//EndConstructor.

    @Override
    public void create() {
        super.create();
        this.setTitle("Register new user");
        setMessage("Enter your data and register a new user account.", IMessageProvider.INFORMATION);
    }//EndMethod.

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);
        Composite container = new Composite(area, SWT.NONE);

        container.setLayout(new GridLayout(2, false));
        container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        showUsername(container);
        showEmail(container);
        showPassword(container);

        return area;
    }//EndMethod.

    private void showUsername(Composite container) {
        lblUsername = new Label(container, SWT.NONE);
        lblUsername.setText("Username: ");
        txtUsername = new Text(container, SWT.BORDER);
        txtUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    }//EndMethod.

    private void showEmail(Composite container) {
        lblEmail = new Label(container, SWT.NONE);
        lblEmail.setText("E-mail: ");
        txtEmail = new Text(container, SWT.BORDER);
        txtEmail.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    }//EndMethod.

    private void showPassword(Composite container) {
        lblPassword = new Label(container, SWT.NONE);
        lblPassword.setText("Password: ");
        txtPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
        txtPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        lblPassword2 = new Label(container, SWT.NONE);
        lblPassword2.setText("Re-insert Password: ");
        txtPassword2 = new Text(container, SWT.BORDER | SWT.PASSWORD);
        txtPassword2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    }//EndMethod.

    @Override
    protected Control createButtonBar(Composite parent) {
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        line.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        final Composite buttonBar = new Composite(parent, SWT.NONE);
        buttonBar.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        final GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = false;
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        buttonBar.setLayout(layout);

        Button signupButton = createButton(buttonBar, LoginDialog.SIGNUP, "Sign up", true);
        signupButton.addSelectionListener(new SelectionAdapter() {
            private static final long serialVersionUID = 1L;

            public void widgetSelected(SelectionEvent e) {
                //Check the fields.
                if (txtUsername.getText().trim().length() == 0) {
                    MessageBox messageBox = new MessageBox(Display.getCurrent().getActiveShell(),
                            SWT.OK | SWT.ICON_WARNING);
                    messageBox.setMessage("Invalid User Name.");
                    messageBox.setText("Warning");
                    messageBox.open();

                    return;
                } else if (txtPassword.getText().trim().length() == 0) {
                    MessageBox messageBox = new MessageBox(Display.getCurrent().getActiveShell(),
                            SWT.OK | SWT.ICON_WARNING);
                    messageBox.setMessage("Invalid Password.");
                    messageBox.setText("Warning");
                    messageBox.open();

                    return;
                } else if (txtPassword.getText().trim().length() == 0) {
                    MessageBox messageBox = new MessageBox(Display.getCurrent().getActiveShell(),
                            SWT.OK | SWT.ICON_WARNING);
                    messageBox.setMessage("Invalid Password.");
                    messageBox.setText("Warning");
                    messageBox.open();

                    return;
                } else if (!txtPassword.getText().equals(txtPassword2.getText())) {
                    MessageBox messageBox = new MessageBox(Display.getCurrent().getActiveShell(),
                            SWT.OK | SWT.ICON_WARNING);
                    messageBox.setMessage("Passwords do not match.");
                    messageBox.setText("Warning");
                    messageBox.open();

                    return;
                }

                userAccount = new UserAccount(txtUsername.getText(), txtPassword.getText());

                setReturnCode(LoginDialog.SIGNUP);
                close();
            }
        });

        createButton(buttonBar, CANCEL, "Cancel", true);

        return buttonBar;
    }//EndMethod.

}//EndClass.