Online-TicTacToe

Log | Files | Refs | README | LICENSE

login.java (5525B)


      1 /*
      2  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
      3  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
      4  */
      5 package com.groupproject.tictactoeclient.ContentPanes;
      6 
      7 import com.groupproject.tictactoeclient.TicTacToeClient;
      8 import java.awt.Color;
      9 
     10 import java.awt.event.*;
     11 
     12 import javax.swing.*;
     13 
     14 /**
     15  *
     16  * @author adam
     17  */
     18 public class login extends CustomPanel {
     19 
     20     public login(TicTacToeClient client) {
     21 
     22         // Set panel layout to spring layout
     23         SpringLayout layout = new SpringLayout();
     24         setLayout(layout);
     25 
     26         //Create the login Button
     27         JButton loginButton = new JButton("Login");
     28 
     29         //Create the username and password Labels
     30         JLabel usernameLabel = new JLabel("username");
     31         JLabel passwordLabel = new JLabel("password");
     32 
     33         //Create the username and password text fields
     34         JTextField usernameTextField = new JTextField(20);
     35         JPasswordField passwordTextField = new JPasswordField(20);
     36 
     37         // Back button
     38         JButton backButton = new JButton("Back");
     39 
     40         //Create a checkbox to show the password
     41         JCheckBox showPassword = new JCheckBox("show password");
     42 
     43         //Create error message if login is not correct
     44         JLabel loginErrorLabel = new JLabel("Wrong username or password");
     45         loginErrorLabel.setForeground(Color.red);
     46         loginErrorLabel.setVisible(false);
     47 
     48         // Add components to panel
     49         add(loginButton);
     50         add(usernameLabel);
     51         add(passwordLabel);
     52         add(usernameTextField);
     53         add(passwordTextField);
     54         add(showPassword);
     55         add(backButton);
     56         add(loginErrorLabel);
     57 
     58         // UsernameLabel constraints
     59         layout.putConstraint(SpringLayout.EAST, usernameLabel, -20, SpringLayout.WEST, usernameTextField);
     60         layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameLabel, 0, SpringLayout.VERTICAL_CENTER, usernameTextField);
     61 
     62         //Username textfield Constraint
     63         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, usernameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
     64         layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameTextField, 0, SpringLayout.VERTICAL_CENTER, this);
     65 
     66         //PasswordLabel constraints
     67         layout.putConstraint(SpringLayout.EAST, passwordLabel, -20, SpringLayout.WEST, passwordTextField);
     68         layout.putConstraint(SpringLayout.VERTICAL_CENTER, passwordLabel, 0, SpringLayout.VERTICAL_CENTER, passwordTextField);
     69 
     70         //Password textfield constraint
     71         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, passwordTextField, 0, SpringLayout.HORIZONTAL_CENTER, this);
     72         layout.putConstraint(SpringLayout.NORTH, passwordTextField, 20, SpringLayout.SOUTH, usernameTextField);
     73 
     74         //Show password checkbox constraint
     75         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, showPassword, 0, SpringLayout.HORIZONTAL_CENTER, this);
     76         layout.putConstraint(SpringLayout.NORTH, showPassword, 20, SpringLayout.SOUTH, passwordTextField);
     77 
     78         //Login Button constraint
     79         layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, loginButton, 0, SpringLayout.HORIZONTAL_CENTER, this);
     80         layout.putConstraint(SpringLayout.NORTH, loginButton, 20, SpringLayout.SOUTH, showPassword);
     81 
     82         //Back button constraint
     83         layout.putConstraint(SpringLayout.WEST, backButton, 20, SpringLayout.WEST, this);
     84         layout.putConstraint(SpringLayout.NORTH, backButton, 20, SpringLayout.NORTH, this);
     85 
     86         //Error label constraint
     87         layout.putConstraint(SpringLayout.EAST, loginErrorLabel, -20, SpringLayout.EAST, this);
     88         layout.putConstraint(SpringLayout.NORTH, loginErrorLabel, 20, SpringLayout.NORTH, this);
     89 
     90         // Login Button Action - check the entries of the textFields 
     91         loginButton.addActionListener(new ActionListener() {
     92             @Override
     93             public void actionPerformed(ActionEvent e) {
     94                 String username = usernameTextField.getText();  // Get the username
     95                 client.username = username;
     96                 client.UID = String.valueOf(client.proxy.login(usernameTextField.getText(), String.valueOf(passwordTextField.getPassword())));
     97                 System.out.println("UID = " + client.UID);
     98 
     99                 if (Integer.parseInt(client.UID) == -1) {
    100                     loginErrorLabel.setVisible(true);
    101                 } else {
    102                     client.showPanel(new MainContentPanel(client));
    103                 }
    104             }
    105         });
    106 
    107         //Item listener to check if the showPassword checkbox has been selected
    108         //If selected show the password and if not display the password as **** normal
    109         char defaultPassword = passwordTextField.getEchoChar();
    110         showPassword.addItemListener(new ItemListener() {
    111             public void itemStateChanged(ItemEvent e) {
    112                 if (e.getStateChange() == ItemEvent.SELECTED) {
    113                     passwordTextField.setEchoChar((char) 0);
    114                 } else {
    115                     passwordTextField.setEchoChar(defaultPassword);
    116                 }
    117             }
    118         });
    119 
    120         // Back button listener
    121         backButton.addActionListener(new ActionListener() {
    122             @Override
    123             public void actionPerformed(ActionEvent e) {
    124                 client.showPanel(new StartPanel(client));
    125             }
    126         });
    127     }
    128 
    129     @Override
    130     public void refresh() {
    131         // not needed
    132     }
    133 }