net.java.sip.communicator.gui.AuthenticationSplash.java Source code

Java tutorial

Introduction

Here is the source code for net.java.sip.communicator.gui.AuthenticationSplash.java

Source

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * Portions of this software are based upon public domain software
 * originally written at the National Center for Supercomputing Applications,
 * University of Illinois, Urbana-Champaign.
 */

package net.java.sip.communicator.gui;

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.Pattern;

import javax.swing.*;

import org.apache.commons.codec.digest.DigestUtils;

import net.java.sip.communicator.common.*;

//import samples.accessory.StringGridBagLayout;

/**
 * Sample login splash screen
 */
public class AuthenticationSplash extends JDialog {
    String userName = null;
    String password = null;
    String name = null;
    String lastName = null;
    String mail = null;
    String policy = null;
    String parity = "false";

    JTextField userNameTextField = null;
    JTextField nameTextField = null;
    JTextField lastNameTextField = null;
    JTextField mailTextField = null;
    JComboBox policyDropDown = null;
    JLabel realmValueLabel = null;
    JPasswordField passwordTextField = null;

    JLabel userNameLabel = null;
    JLabel passwordLabel = null;
    JLabel nameLabel = null;
    JLabel lastNameLabel = null;
    JLabel mailLabel = null;
    JLabel policyLabel = null;

    /**
     * Resource bundle with default locale
     */
    private ResourceBundle resources = null;

    /**
     * Path to the image resources
     */
    private String imagePath = null;

    /**
     * Command string for a cancel action (e.g., a button).
     * This string is never presented to the user and should
     * not be internationalized.
     */
    private String CMD_CANCEL = "cmd.cancel" /*NOI18N*/;

    /**
     * Command string for a help action (e.g., a button).
     * This string is never presented to the user and should
     * not be internationalized.
     */
    private String CMD_REGISTER = "cmd.register" /*NOI18N*/;

    /**
     * Command string for a login action (e.g., a button).
     * This string is never presented to the user and should
     * not be internationalized.
     */
    private String CMD_LOGIN = "cmd.login" /*NOI18N*/;

    // Components we need to manipulate after creation
    JButton loginButton = null;
    JButton cancelButton = null;
    JButton registerButton = null;

    /**
     * Creates new form AuthenticationSplash
     */
    public AuthenticationSplash(Frame parent, boolean modal) {
        super(parent, modal);
        initResources();
        initComponents(parent);
        pack();
        centerWindow();
    }

    /**
     * Loads locale-specific resources: strings, images, et cetera
     */
    private void initResources() {
        Locale locale = Locale.getDefault();
        imagePath = ".";
    }

    /**
     * Centers the window on the screen.
     */
    private void centerWindow() {
        Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        Point center = new Point((int) screen.getCenterX(), (int) screen.getCenterY());
        Point newLocation = new Point(center.x - this.getWidth() / 2, center.y - this.getHeight() / 2);
        if (screen.contains(newLocation.x, newLocation.y, this.getWidth(), this.getHeight())) {
            this.setLocation(newLocation);
        }
    } // centerWindow()

    /**
     *
     * We use dynamic layout managers, so that layout is dynamic and will
     * adapt properly to user-customized fonts and localized text. The
     * GridBagLayout makes it easy to line up components of varying
     * sizes along invisible vertical and horizontal grid lines. It
     * is important to sketch the layout of the interface and decide
     * on the grid before writing the layout code.
     *
     * Here we actually use
     * our own subclass of GridBagLayout called StringGridBagLayout,
     * which allows us to use strings to specify constraints, rather
     * than having to create GridBagConstraints objects manually.
     *
     *
     * We use the JLabel.setLabelFor() method to connect
     * labels to what they are labeling. This allows mnemonics to work
     * and assistive to technologies used by persons with disabilities
     * to provide much more useful information to the user.
     */

    private void initComponents(final Frame parent) {
        Container contents = getContentPane();
        contents.setLayout(new BorderLayout());

        String title = Utils.getProperty("net.java.sip.communicator.gui.AUTH_WIN_TITLE");

        if (title == null)
            title = "Login Manager";

        setTitle(title);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                dialogDone(CMD_CANCEL, parent);
            }
        });

        // Accessibility -- all frames, dialogs, and applets should
        // have a description
        getAccessibleContext().setAccessibleDescription("Authentication Splash");

        String authPromptLabelValue = Utils.getProperty("net.java.sip.communicator.gui.AUTHENTICATION_PROMPT");

        if (authPromptLabelValue == null)
            authPromptLabelValue = "Please register to the service or enter your credentials to log in:";

        JLabel splashLabel = new JLabel(authPromptLabelValue);
        splashLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        splashLabel.setHorizontalAlignment(SwingConstants.CENTER);
        splashLabel.setHorizontalTextPosition(SwingConstants.CENTER);
        contents.add(splashLabel, BorderLayout.NORTH);

        JPanel centerPane = new JPanel();
        centerPane.setLayout(new GridBagLayout());

        userNameTextField = new JTextField(); // needed below

        // user name label
        JLabel userNameLabel = new JLabel();
        userNameLabel.setDisplayedMnemonic('U');
        // setLabelFor() allows the mnemonic to work
        userNameLabel.setLabelFor(userNameTextField);

        String userNameLabelValue = Utils.getProperty("net.java.sip.communicator.gui.USER_NAME_LABEL");

        if (userNameLabelValue == null)
            userNameLabelValue = "Username";

        int gridy = 0;

        userNameLabel.setText(userNameLabelValue);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(12, 12, 0, 0);
        centerPane.add(userNameLabel, c);

        // user name text
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(12, 7, 0, 11);
        centerPane.add(userNameTextField, c);

        passwordTextField = new JPasswordField(); //needed below

        // password label
        JLabel passwordLabel = new JLabel();
        passwordLabel.setDisplayedMnemonic('P');
        passwordLabel.setLabelFor(passwordTextField);
        String pLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.PASSWORD_LABEL");

        if (pLabelStr == null)
            pLabelStr = "Password";

        passwordLabel.setText(pLabelStr);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);

        centerPane.add(passwordLabel, c);

        // password text
        passwordTextField.setEchoChar('\u2022');
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(passwordTextField, c);

        //Set a relevant realm value
        //Bug report by Steven Lass (sltemp at comcast.net)
        //JLabel realmValueLabel = new JLabel("SipPhone.com"); // needed below

        // realm label

        JLabel realmLabel = new JLabel();
        realmLabel.setDisplayedMnemonic('R');
        realmLabel.setLabelFor(realmValueLabel);
        realmLabel.setText("Realm");
        realmValueLabel = new JLabel();

        // Buttons along bottom of window
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, 0));
        loginButton = new JButton();
        loginButton.setText("Login");
        loginButton.setActionCommand(CMD_LOGIN);
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                dialogDone(event, parent);
            }
        });
        buttonPanel.add(loginButton);

        // space
        buttonPanel.add(Box.createRigidArea(new Dimension(5, 0)));

        registerButton = new JButton();
        registerButton.setMnemonic('G');
        registerButton.setText("Register");
        registerButton.setActionCommand(CMD_REGISTER);
        registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                dialogDone(event, parent);
            }
        });
        buttonPanel.add(registerButton);

        buttonPanel.add(Box.createRigidArea(new Dimension(5, 0)));

        cancelButton = new JButton();
        cancelButton.setText("Cancel");
        cancelButton.setActionCommand(CMD_CANCEL);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                dialogDone(event, parent);
            }
        });
        buttonPanel.add(cancelButton);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        c.insets = new Insets(11, 12, 11, 11);

        centerPane.add(buttonPanel, c);

        contents.add(centerPane, BorderLayout.CENTER);
        getRootPane().setDefaultButton(loginButton);
        equalizeButtonSizes();

        setFocusTraversalPolicy(new FocusTraversalPol());

    } // initComponents()

    /**
     * Sets the buttons along the bottom of the dialog to be the
     * same size. This is done dynamically by setting each button's
     * preferred and maximum sizes after the buttons are created.
     * This way, the layout automatically adjusts to the locale-
     * specific strings.
     */
    private void equalizeButtonSizes() {

        JButton[] buttons = new JButton[] { loginButton, cancelButton, registerButton };

        String[] labels = new String[buttons.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = buttons[i].getText();
        }

        // Get the largest width and height
        int i = 0;
        Dimension maxSize = new Dimension(0, 0);
        Rectangle2D textBounds = null;
        Dimension textSize = null;
        FontMetrics metrics = buttons[0].getFontMetrics(buttons[0].getFont());
        Graphics g = getGraphics();
        for (i = 0; i < labels.length; ++i) {
            textBounds = metrics.getStringBounds(labels[i], g);
            maxSize.width = Math.max(maxSize.width, (int) textBounds.getWidth());
            maxSize.height = Math.max(maxSize.height, (int) textBounds.getHeight());
        }

        Insets insets = buttons[0].getBorder().getBorderInsets(buttons[0]);
        maxSize.width += insets.left + insets.right;
        maxSize.height += insets.top + insets.bottom;

        // reset preferred and maximum size since BoxLayout takes both
        // into account
        for (i = 0; i < buttons.length; ++i) {
            buttons[i].setPreferredSize((Dimension) maxSize.clone());
            buttons[i].setMaximumSize((Dimension) maxSize.clone());
        }
    } // equalizeButtonSizes()

    /**
     * The user has selected an option. Here we close and dispose the dialog.
     * If actionCommand is an ActionEvent, getCommandString() is called,
     * otherwise toString() is used to get the action command.
     *
     * @param actionCommand may be null
     */
    private void registrationComponents() {
        Container contents = getContentPane();
        contents.setLayout(new BorderLayout());

        String title = Utils.getProperty("net.java.sip.communicator.gui.REG_WIN_TITLE");

        if (title == null)
            title = "Registration Manager";

        setTitle(title);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                registrationDialogDone(CMD_CANCEL);
            }
        });

        // Accessibility -- all frames, dialogs, and applets should
        // have a description
        getAccessibleContext().setAccessibleDescription("Registration Splash");

        String authPromptLabelValue = Utils.getProperty("net.java.sip.communicator.gui.REGISTRATION_PROMPT");

        if (authPromptLabelValue == null)
            authPromptLabelValue = "Please fill in the following fields to register:";

        JLabel splashLabel = new JLabel(authPromptLabelValue);
        splashLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        splashLabel.setHorizontalAlignment(SwingConstants.CENTER);
        splashLabel.setHorizontalTextPosition(SwingConstants.CENTER);
        contents.add(splashLabel, BorderLayout.NORTH);

        JPanel centerPane = new JPanel();
        centerPane.setLayout(new GridBagLayout());

        /* My additions */
        nameTextField = new JTextField(); //needed below

        // name label
        nameLabel = new JLabel();
        nameLabel.setLabelFor(nameTextField);
        String nLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.NAME_LABEL");

        if (nLabelStr == null)
            nLabelStr = "Name";

        nameLabel.setText(nLabelStr);

        int gridy = 0;
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);

        centerPane.add(nameLabel, c);

        // name text
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(nameTextField, c);

        lastNameTextField = new JTextField(); //needed below

        // last name label
        lastNameLabel = new JLabel();
        lastNameLabel.setLabelFor(nameTextField);
        String lnLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.LAST_NAME_LABEL");

        if (lnLabelStr == null)
            lnLabelStr = "Last Name";

        lastNameLabel.setText(lnLabelStr);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);

        centerPane.add(lastNameLabel, c);

        // last name text
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(lastNameTextField, c);

        mailTextField = new JTextField(); //needed below

        // mail label
        mailLabel = new JLabel();
        mailLabel.setLabelFor(mailTextField);
        String mLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.MAIL_LABEL");

        if (mLabelStr == null)
            mLabelStr = "Email";

        mailLabel.setText(mLabelStr);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);

        centerPane.add(mailLabel, c);

        // mail text
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(mailTextField, c);
        /* END: MY additions */

        userNameTextField = new JTextField(); // needed below

        // user name label
        userNameLabel = new JLabel();
        userNameLabel.setLabelFor(userNameTextField);
        String userNameLabelValue = Utils.getProperty("net.java.sip.communicator.gui.USER_NAME_LABEL");

        if (userNameLabelValue == null)
            userNameLabelValue = "Username";

        userNameLabel.setText(userNameLabelValue);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(12, 12, 0, 0);
        centerPane.add(userNameLabel, c);

        // user name text
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(12, 7, 0, 11);
        centerPane.add(userNameTextField, c);

        passwordTextField = new JPasswordField(); //needed below

        // password label
        passwordLabel = new JLabel();
        passwordLabel.setLabelFor(passwordTextField);
        String pLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.PASSWORD_LABEL");

        if (pLabelStr == null)
            pLabelStr = "Password";

        passwordLabel.setText(pLabelStr);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);

        centerPane.add(passwordLabel, c);

        // password text
        passwordTextField.setEchoChar('\u2022');
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(passwordTextField, c);

        policyDropDown = new JComboBox();
        policyDropDown.addItem("Basic");
        policyDropDown.addItem("Pro");
        policyDropDown.addItem("Enterprise");

        // policy label
        policyLabel = new JLabel();
        policyLabel.setLabelFor(policyDropDown);
        String plcLabelStr = PropertiesDepot.getProperty("net.java.sip.communicator.gui.POLICY_LABEL");

        if (plcLabelStr == null)
            plcLabelStr = "Policy";

        policyLabel.setText(plcLabelStr);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = gridy;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(11, 12, 0, 0);
        centerPane.add(policyLabel, c);

        // policy menu
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = gridy++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.insets = new Insets(11, 7, 0, 11);
        centerPane.add(policyDropDown, c);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, 0));

        registerButton = new JButton();
        registerButton.setMnemonic('G');
        registerButton.setText("Register");
        registerButton.setActionCommand(CMD_REGISTER);
        registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                registrationDialogDone(event);
            }
        });
        buttonPanel.add(registerButton);

        buttonPanel.add(Box.createRigidArea(new Dimension(5, 0)));

        cancelButton = new JButton();
        cancelButton.setText("Cancel");
        cancelButton.setActionCommand(CMD_CANCEL);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                registrationDialogDone(event);
            }
        });
        buttonPanel.add(cancelButton);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 2;
        c.insets = new Insets(11, 12, 11, 11);

        centerPane.add(buttonPanel, c);

        contents.add(centerPane, BorderLayout.CENTER);
        getRootPane().setDefaultButton(registerButton);
        registrationEqualizeButtonSizes();

        setFocusTraversalPolicy(new FocusTraversalPol());

    } // registrationComponents()

    private void registrationEqualizeButtonSizes() {

        JButton[] buttons = new JButton[] { cancelButton, registerButton };

        String[] labels = new String[buttons.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = buttons[i].getText();
        }

        // Get the largest width and height
        int i = 0;
        Dimension maxSize = new Dimension(0, 0);
        Rectangle2D textBounds = null;
        Dimension textSize = null;
        FontMetrics metrics = buttons[0].getFontMetrics(buttons[0].getFont());
        Graphics g = getGraphics();
        for (i = 0; i < labels.length; ++i) {
            textBounds = metrics.getStringBounds(labels[i], g);
            maxSize.width = Math.max(maxSize.width, (int) textBounds.getWidth());
            maxSize.height = Math.max(maxSize.height, (int) textBounds.getHeight());
        }

        Insets insets = buttons[0].getBorder().getBorderInsets(buttons[0]);
        maxSize.width += insets.left + insets.right;
        maxSize.height += insets.top + insets.bottom;

        // reset preferred and maximum size since BoxLayout takes both
        // into account
        for (i = 0; i < buttons.length; ++i) {
            buttons[i].setPreferredSize((Dimension) maxSize.clone());
            buttons[i].setMaximumSize((Dimension) maxSize.clone());
        }
    } // equalizeButtonSizes()

    private void registrationDialogDone(Object actionCommand) {
        String cmd = null;
        if (actionCommand != null) {
            if (actionCommand instanceof ActionEvent) {
                cmd = ((ActionEvent) actionCommand).getActionCommand();
            } else {
                cmd = actionCommand.toString();
            }
        }
        if (cmd == null) {
            // do nothing
        } else if (cmd.equals(CMD_CANCEL)) {
            userName = null;
            lastName = null;
            name = null;
            mail = null;
            policy = null;
            password = null;
        } else if (cmd.equals(CMD_REGISTER)) {
            nameLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("JLabel.selectionForeground"));
            lastNameLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("JLabel.selectionForeground"));
            mailLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("JLabel.selectionForeground"));
            userNameLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("JLabel.selectionForeground"));
            passwordLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("JLabel.selectionForeground"));

            userName = userNameTextField.getText();
            lastName = lastNameTextField.getText();
            name = nameTextField.getText();
            mail = mailTextField.getText();
            policy = (String) policyDropDown.getSelectedItem();
            password = passwordTextField.getText();

            if (!valid())
                return;
            password = DigestUtils.sha1Hex(password);
            parity = "true";
        }
        setVisible(false);
        dispose();
    } // dialogDone()

    public boolean valid() {
        int idx;
        boolean usern = true, pwd = true, nme = true, lnme = true, ml = true;
        // Username checks

        // Starts with a letter
        if (userName.length() == 0)
            usern = false;
        else if (!Character.isLetter(userName.charAt(0)))
            usern = false;

        // Contains only letters and numbers
        for (idx = 0; idx < userName.length(); idx++) {
            if (!Character.isLetterOrDigit(userName.charAt(idx)))
                usern = false;
        }

        // Is between 4 and 10 characters
        if ((userName.length() < 4) || (userName.length() > 18))
            usern = false;

        // Name and last name checks

        // Both begin with uppercase
        if (name.length() == 0)
            nme = false;
        else if (!Character.isUpperCase(name.charAt(0)))
            nme = false;

        if (lastName.length() == 0)
            lnme = false;
        else if (!Character.isUpperCase(lastName.charAt(0)))
            lnme = false;

        // Both contain only letters
        for (idx = 0; idx < name.length(); idx++) {
            if (!Character.isLetter(name.charAt(idx)))
                nme = false;
        }

        for (idx = 0; idx < lastName.length(); idx++) {
            if (!Character.isLetter(lastName.charAt(idx)))
                lnme = false;
        }

        // Mail chekcs
        Pattern ptr = Pattern.compile(
                "(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\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".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)");
        if (!ptr.matcher(mail).matches())
            ml = false;

        // Password checks

        // Check that we have a variety of different characters
        boolean lower = false, upper = false, number = false, other = false;
        for (idx = 0; idx < password.length(); idx++) {
            if (Character.isUpperCase(password.charAt(idx)))
                upper = true;
            if (Character.isLowerCase(password.charAt(idx)))
                lower = true;
            if (!Character.isLetterOrDigit(password.charAt(idx)))
                other = true;
            if (Character.isDigit(password.charAt(idx)))
                number = true;
        }
        if (!(upper && lower && number && other))
            pwd = false;

        // Verify that the size is acceptable
        if (password.length() < 6 || password.length() > 30)
            pwd = false;

        // Change the label colors
        if (!nme) {
            nameLabel.setForeground(Color.RED);
            nameTextField.setText("");
        }

        if (!lnme) {
            lastNameLabel.setForeground(Color.RED);
            lastNameTextField.setText("");
        }

        if (!ml) {
            mailLabel.setForeground(Color.RED);
            mailTextField.setText("");
        }

        if (!usern) {
            userNameLabel.setForeground(Color.RED);
            userNameTextField.setText("");
        }

        if (!pwd) {
            passwordLabel.setForeground(Color.RED);
            passwordTextField.setText("");
        }

        if (!(nme && lnme && usern && ml && pwd))
            return false;

        return true;
    }

    private void dialogDone(Object actionCommand, Frame parent) {
        String cmd = null;
        if (actionCommand != null) {
            if (actionCommand instanceof ActionEvent) {
                cmd = ((ActionEvent) actionCommand).getActionCommand();
            } else {
                cmd = actionCommand.toString();
            }
        }
        if (cmd == null) {
            // do nothing
        } else if (cmd.equals(CMD_CANCEL)) {
            userName = null;
            password = null;
            setVisible(false);
            dispose();
        } else if (cmd.equals(CMD_REGISTER)) {
            getContentPane().removeAll();
            registrationComponents();
            pack();
            centerWindow();
        } else if (cmd.equals(CMD_LOGIN)) {
            userName = userNameTextField.getText();
            password = passwordTextField.getText();
            password = DigestUtils.sha1Hex(password);
            setVisible(false);
            dispose();
        }
    } // dialogDone()

    private class FocusTraversalPol extends LayoutFocusTraversalPolicy {
        public Component getDefaultComponent(Container cont) {
            if (userNameTextField.getText() == null || userNameTextField.getText().trim().length() == 0)
                return super.getFirstComponent(cont);
            else
                return passwordTextField;
        }
    }
}