Online-TicTacToe

Log | Files | Refs | README | LICENSE

register.java (8324B)


      1 package com.groupproject.tictactoeclient.ContentPanes;
      2 
      3 import com.groupproject.tictactoeclient.TicTacToeClient;
      4 import java.awt.Color;
      5 import java.awt.Dimension;
      6 import java.awt.event.ActionEvent;
      7 import java.awt.event.ActionListener;
      8 import javax.swing.BoxLayout;
      9 import java.awt.FlowLayout;
     10 import java.awt.event.ItemEvent;
     11 import java.awt.event.ItemListener;
     12 import javax.swing.JButton;
     13 import javax.swing.JPanel;
     14 import javax.swing.JLabel;
     15 import javax.swing.JTextField;
     16 import javax.swing.Box;
     17 import javax.swing.JCheckBox;
     18 import javax.swing.JPasswordField;
     19 import javax.swing.SpringLayout;
     20 import javax.swing.JOptionPane;
     21 
     22 /**
     23  *
     24  * Author: Luke
     25  */
     26 public class register extends CustomPanel {
     27 
     28     public register(TicTacToeClient client) {
     29 
     30         // Set panel layout to spring layout
     31         SpringLayout layout = new SpringLayout();
     32         setLayout(layout);
     33 
     34         //Create the login Button
     35         JButton registerButton = new JButton("Register");
     36 
     37         //Create the username and password Labels
     38         JLabel usernameLabel = new JLabel("username");
     39         JLabel passwordLabel = new JLabel("password");
     40         JLabel nameLabel = new JLabel("name");
     41         JLabel surnameLabel = new JLabel("surname");
     42 
     43         //Create the username and password text fields
     44         JTextField usernameTextField = new JTextField(20);
     45         JPasswordField passwordTextField = new JPasswordField(20);
     46         JTextField nameTextField = new JTextField(20);
     47         JTextField surnameTextField = new JTextField(20);
     48 
     49         // Back button
     50         JButton backButton = new JButton("Back");
     51 
     52         //Create a checkbox to show the password
     53         JCheckBox showPassword = new JCheckBox("show password");
     54 
     55         //Create error message if register is not correct
     56         JLabel registerErrorLabel = new JLabel("User is already created");
     57         registerErrorLabel.setForeground(Color.red);
     58         registerErrorLabel.setVisible(false);
     59 
     60         // Add components to panel
     61         add(registerButton);
     62         add(usernameLabel);
     63         add(passwordLabel);
     64         add(nameLabel);
     65         add(surnameLabel);
     66         add(usernameTextField);
     67         add(passwordTextField);
     68         add(nameTextField);
     69         add(surnameTextField);
     70 
     71         add(showPassword);
     72         add(backButton);
     73         add(registerErrorLabel);
     74 
     75         // UsernameLabel constraints
     76         layout.putConstraint(SpringLayout.EAST, usernameLabel, -20, SpringLayout.WEST, usernameTextField);
     77         layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameLabel, 0, SpringLayout.VERTICAL_CENTER, usernameTextField);
     78 
     79         //Username textfield Constraint
     80         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, usernameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
     81         layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameTextField, -60, SpringLayout.VERTICAL_CENTER, this);
     82 
     83         //nameLabel constraints
     84         layout.putConstraint(SpringLayout.EAST, nameLabel, -20, SpringLayout.WEST, nameTextField);
     85         layout.putConstraint(SpringLayout.VERTICAL_CENTER, nameLabel, 0, SpringLayout.VERTICAL_CENTER, nameTextField);
     86 
     87         //name textfield constraints
     88         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, nameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
     89         layout.putConstraint(SpringLayout.NORTH, nameTextField, 20, SpringLayout.SOUTH, usernameTextField);
     90 
     91         //surnameLabel constraints
     92         layout.putConstraint(SpringLayout.EAST, surnameLabel, -20, SpringLayout.WEST, surnameTextField);
     93         layout.putConstraint(SpringLayout.VERTICAL_CENTER, surnameLabel, 0, SpringLayout.VERTICAL_CENTER, surnameTextField);
     94 
     95         //surname textfield constraints
     96         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, surnameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
     97         layout.putConstraint(SpringLayout.NORTH, surnameTextField, 20, SpringLayout.SOUTH, nameTextField);
     98 
     99         //PasswordLabel constraints
    100         layout.putConstraint(SpringLayout.EAST, passwordLabel, -20, SpringLayout.WEST, passwordTextField);
    101         layout.putConstraint(SpringLayout.VERTICAL_CENTER, passwordLabel, 0, SpringLayout.VERTICAL_CENTER, passwordTextField);
    102 
    103         //Password textfield constraint
    104         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, passwordTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
    105         layout.putConstraint(SpringLayout.NORTH, passwordTextField, 20, SpringLayout.SOUTH, surnameTextField);
    106 
    107         //Show password checkbox constraint
    108         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, showPassword, 0, SpringLayout.HORIZONTAL_CENTER, this);
    109         layout.putConstraint(SpringLayout.NORTH, showPassword, 20, SpringLayout.SOUTH, passwordTextField);
    110 
    111         //Register Button constraint
    112         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, registerButton, 0, SpringLayout.HORIZONTAL_CENTER, this);
    113         layout.putConstraint(SpringLayout.NORTH, registerButton, 20, SpringLayout.SOUTH, showPassword);
    114 
    115         //Back button constraint
    116         layout.putConstraint(SpringLayout.WEST, backButton, 20, SpringLayout.WEST, this);
    117         layout.putConstraint(SpringLayout.NORTH, backButton, 20, SpringLayout.NORTH, this);
    118 
    119         //Error label constraint
    120         layout.putConstraint(SpringLayout.EAST, registerErrorLabel, -20, SpringLayout.EAST, this);
    121         layout.putConstraint(SpringLayout.NORTH, registerErrorLabel, 20, SpringLayout.NORTH, this);
    122 
    123         // Action listener for register button
    124         registerButton.addActionListener(new ActionListener() {
    125             @Override
    126             public void actionPerformed(ActionEvent e) {
    127 
    128                 //get the text from each textfield
    129                 String username = usernameTextField.getText().trim();
    130                 String name = nameTextField.getText().trim();
    131                 String surname = surnameTextField.getText().trim();
    132                 String password = passwordTextField.getText().trim();
    133 
    134                 //check if any of the fields are empty to prevent a blank user being created
    135                 if (username.isEmpty() || name.isEmpty() || surname.isEmpty() || password.isEmpty()) {
    136                     JOptionPane.showMessageDialog(client.frame, "Please fill in all fields");
    137                     return;
    138                 }
    139 
    140                 // stores the UID in responce and stores it as a global variable
    141                 String response = client.proxy.register(usernameTextField.getText(), passwordTextField.getText(), nameTextField.getText(), surnameTextField.getText());
    142                 System.out.println(response);
    143 
    144                 //check if the username already exists, if not allow user to register
    145                 if ("ERROR-REPEAT".equals(response)) {
    146                     JOptionPane.showMessageDialog(client.frame, "User already exists");
    147                     return;
    148                 } else {
    149 
    150                     //register the user and assign a User id to the client
    151                     client.UID = response;
    152                     //stores the username 
    153                     client.username = usernameTextField.getText();
    154 
    155                     System.out.println("Client.UID = " + client.UID);
    156                     //once registered move to the main panel
    157                     client.showPanel(new MainContentPanel(client));
    158                 }
    159             }
    160 
    161         });
    162 
    163         //Item listener to check if the showPassword checkbox has been selected
    164         //If selected show the password and if not display the password as **** normal
    165         char userPassword = passwordTextField.getEchoChar();
    166         showPassword.addItemListener(new ItemListener() {
    167             public void itemStateChanged(ItemEvent e) {
    168                 if (e.getStateChange() == ItemEvent.SELECTED) {
    169                     passwordTextField.setEchoChar((char) 0);
    170                 } else {
    171                     passwordTextField.setEchoChar(userPassword);
    172                 }
    173             }
    174         });
    175 
    176         // Back button listener - go back to the start panel 
    177         backButton.addActionListener(new ActionListener() {
    178             @Override
    179             public void actionPerformed(ActionEvent e) {
    180                 client.showPanel(new StartPanel(client));
    181             }
    182         });
    183 
    184     }
    185 
    186     @Override
    187     public void refresh() {
    188         // not needed
    189     }
    190 }