Untitled

 avatar
unknown
plain_text
2 years ago
6.0 kB
5
Indexable
package com.jwrms.view;

import com.jwrms.controller.loginViewControllers.LoginViewController;
import com.jwrms.domain.Staff;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class LoginView extends JFrame implements MouseListener {


    private JLabel employeeIdLabel;
    private JTextField employeeIdTextField;
    private JPanel employeeIdPanel;
    private JLabel passwordLabel;
    private JPasswordField passwordField;
    private JPanel passwordPanel;
    private Button showPasswordButton;
    private Button loginButton;
    private JPanel loginPanel;
    private JButton continueAsAdminButton;
    private JPanel continueAsAdminPanel;
    private LoginViewController loginViewController;

    private Boolean showPassword = false;


    public LoginView(LoginViewController controller){

        configureComponents();
        addComponentsToFrame();
        configureFrame();
        this.loginViewController = controller;
    }

    private void configureComponents(){

        employeeIdLabel = new JLabel("Employee ID: ");
        employeeIdTextField = new JTextField(20);

        employeeIdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,20,0));
        employeeIdPanel.add(employeeIdLabel);
        employeeIdPanel.add(employeeIdTextField);

        passwordLabel = new JLabel("Password: ");
        passwordField = new JPasswordField(20);


        showPasswordButton = new Button("Show Password");
        showPasswordButton.addMouseListener(this);
        showPasswordButton.setFocusable(false);
        showHidePassword(showPassword);

        passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,20,0));
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        passwordPanel.add(showPasswordButton);

        loginButton = new Button("LOGIN");
        loginButton.addMouseListener(this);
        loginButton.setFocusable(false);



        loginPanel = new JPanel(new GridLayout(1,1));
        loginPanel.setBorder(new EmptyBorder(20,20,10,20));
        loginPanel.add(loginButton);

        continueAsAdminButton = new JButton("CONTINUE AS ADMIN");
        continueAsAdminButton.addMouseListener(this);
        continueAsAdminButton.setFocusable(false);
        continueAsAdminPanel = new JPanel(new GridLayout(1,1));
        continueAsAdminPanel.setBorder(new EmptyBorder(20,20,20,20));
        continueAsAdminPanel.add(continueAsAdminButton);
    }
    private void addComponentsToFrame(){

        this.add(employeeIdPanel);
        this.add(passwordPanel);
        this.add(loginPanel);
        this.add(continueAsAdminPanel);
    }

    private void configureFrame(){

        this.setSize(520,300);
        this.setLocationRelativeTo(null);
        this.setLayout(new GridLayout(4,1));
        this.setTitle("Jan's Wholesale and Retail Management System");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

        if(e.getSource() == loginButton){

            String employeeId = employeeIdTextField.getText().trim();
            String password = passwordField.getText().trim();

            if(!employeeId.isEmpty() && !password.isEmpty()){

                Staff result = loginViewController.attemptLogin(Integer.parseInt(employeeId),password);
                if (result != null){
                    JOptionPane.showMessageDialog(null,"Login Successful\nWelcome " +result.getName(),"Login Status",JOptionPane.INFORMATION_MESSAGE);
                    employeeIdTextField.setText(null);
                    passwordField.setText(null);
                    loginViewController.goToDashBoard();
                }else{
                    JOptionPane.showMessageDialog(null,"Login Failed\nCheck your ID or password and try again.","Login Status",JOptionPane.ERROR_MESSAGE);
                }

            }else{
                JOptionPane.showMessageDialog(null,"Fields cannot be empty.","Error",JOptionPane.ERROR_MESSAGE);
            }

        }


        if(e.getSource() == showPasswordButton){
            showHidePassword(showPassword);
        }

        if(e.getSource() == continueAsAdminButton){

            Boolean result = loginViewController.checkIfAdminExists();
            loginViewController.goToAdminLogin(result);

        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        if(e.getSource() == loginButton){

            loginButton.setForeground(Color.BLUE);

        } else if (e.getSource() == continueAsAdminButton) {
            continueAsAdminButton.setForeground(Color.BLUE);

        }else if (e.getSource() == showPasswordButton) {
            showPasswordButton.setForeground(Color.BLUE);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {

        if(e.getSource() == loginButton){

            loginButton.setForeground(Color.BLACK);

        } else if (e.getSource() == continueAsAdminButton) {
            continueAsAdminButton.setForeground(Color.BLACK);

        } else if (e.getSource() == showPasswordButton) {
            showPasswordButton.setForeground(Color.BLACK);
        }
    }

    private void showHidePassword(Boolean show){
        if (show){
            passwordField.setEchoChar((char)0);
            showPasswordButton.setLabel("Hide Password");
            showPassword = false;
        }else{
            passwordField.setEchoChar('*');
            showPasswordButton.setLabel("Show Password");
            showPassword = true;
        }
    }

}
Editor is loading...