/MovieTicketing/src/ipd12/movieticketing/DlgLogin.java
Java | 258 lines | 207 code | 25 blank | 26 comment | 11 complexity | 4b996b7a6ccfe45cf9e6e7955c810200 MD5 | raw file
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ipd12.movieticketing;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JOptionPane;
- import org.jasypt.util.password.BasicPasswordEncryptor;
- /**
- *
- * @author 1796174
- */
- public class DlgLogin extends javax.swing.JDialog
- {
- public static final Pattern VALID_EMAIL_ADDRESS_REGEX
- = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
- public DlgLogin(java.awt.Frame parent, boolean modal)
- {
- super(parent, modal);
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jLabel11 = new javax.swing.JLabel();
- jLabel12 = new javax.swing.JLabel();
- pfPass = new javax.swing.JPasswordField();
- jLabel13 = new javax.swing.JLabel();
- tfEmail = new javax.swing.JTextField();
- btLogin = new javax.swing.JButton();
- btCancel = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
- jLabel11.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
- jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- jLabel11.setText("Log in");
- jLabel11.setBorder(javax.swing.BorderFactory.createEtchedBorder());
- jLabel12.setText("Username (e-mail):");
- jLabel13.setText("Password:");
- btLogin.setText("Log in");
- btLogin.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btLoginActionPerformed(evt);
- }
- });
- btCancel.setText("Cancel");
- btCancel.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btCancelActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel12)
- .addComponent(jLabel13))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(tfEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
- .addComponent(pfPass)))
- .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
- .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCancel, btLogin});
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel11)
- .addGap(26, 26, 26)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel12)
- .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel13)
- .addComponent(pfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(42, 42, 42)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>//GEN-END:initComponents
- private void btLoginActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btLoginActionPerformed
- {//GEN-HEADEREND:event_btLoginActionPerformed
- String email = tfEmail.getText();
- String pass = new String(pfPass.getPassword());
- boolean userExists = false;
- BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
- if (email.equals(""))
- {
- JOptionPane.showMessageDialog(this, "Please enter your e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
- return;
- } else if (!validate(email))
- {
- JOptionPane.showMessageDialog(this, "Please enter a valid e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
- return;
- }
- for (User u : Globals.users)
- {
- if (u.email.equals(email))
- {
- userExists = true;
- if (passwordEncryptor.checkPassword(pass, u.password))
- {
- this.dispose();
- Globals.movieGUI.lblUserStatus.setText("Logged in as: " + u.getEmail());
- Globals.movieGUI.meRegister.setVisible(false);
- Globals.movieGUI.meLogin.setVisible(false);
- Globals.movieGUI.meUser.setVisible(true);
- Globals.movieGUI.meUser.setText(u.firstName);
- Globals.currentUser = u;
- Globals.userLogged = true;
- return;
- }
- }
- }
- if (userExists)
- {
- JOptionPane.showMessageDialog(this, "Wrong password. Please try again.", "Wrong password", JOptionPane.WARNING_MESSAGE);
- } else
- {
- Object[] options
- =
- {
- "Register", "Try again"
- };
- int decision = JOptionPane.showOptionDialog(null,
- "This email is not registered yet. Would you like to register an account?",
- "No record found",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.WARNING_MESSAGE,
- null, //do not use a custom Icon
- options, //the titles of buttons
- options[0]); //default button title
- if (decision == JOptionPane.YES_OPTION)
- {
- this.dispose();
- DlgRegister dlgRegister = new DlgRegister(Globals.movieGUI, true);
- dlgRegister.pack();
- dlgRegister.setVisible(true);
- }
- }
- }//GEN-LAST:event_btLoginActionPerformed
- private void btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btCancelActionPerformed
- {//GEN-HEADEREND:event_btCancelActionPerformed
- this.dispose();
- }//GEN-LAST:event_btCancelActionPerformed
- public static boolean validate(String emailStr)
- {
- Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
- return matcher.find();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[])
- {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try
- {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
- {
- if ("Nimbus".equals(info.getName()))
- {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex)
- {
- java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex)
- {
- java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex)
- {
- java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex)
- {
- java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- //</editor-fold>
- /* Create and display the dialog */
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- DlgLogin dialog = new DlgLogin(new javax.swing.JFrame(), true);
- dialog.addWindowListener(new java.awt.event.WindowAdapter()
- {
- @Override
- public void windowClosing(java.awt.event.WindowEvent e)
- {
- System.exit(0);
- }
- });
- dialog.setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JButton btCancel;
- private javax.swing.JButton btLogin;
- private javax.swing.JLabel jLabel11;
- private javax.swing.JLabel jLabel12;
- private javax.swing.JLabel jLabel13;
- private javax.swing.JPasswordField pfPass;
- private javax.swing.JTextField tfEmail;
- // End of variables declaration//GEN-END:variables
- }