/MovieTicketing/src/ipd12/movieticketing/DlgRegister.java
Java | 321 lines | 262 code | 31 blank | 28 comment | 14 complexity | cfec16203a320348ab3464310ae58396 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.sql.SQLException;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JOptionPane;
- import org.jasypt.util.password.BasicPasswordEncryptor;
- /**
- *
- * @author 1796174
- */
- public class DlgRegister 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);
- /**
- * Creates new form DlgRegister
- */
- public DlgRegister(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() {
- jLabel5 = new javax.swing.JLabel();
- jPanel2 = new javax.swing.JPanel();
- dlgReg_btRegister = new javax.swing.JButton();
- dlgReg_btCancel = new javax.swing.JButton();
- jLabel6 = new javax.swing.JLabel();
- dlgReg_tfEmail = new javax.swing.JTextField();
- jLabel7 = new javax.swing.JLabel();
- jLabel8 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel10 = new javax.swing.JLabel();
- dlgReg_tfLastName = new javax.swing.JTextField();
- dlgReg_tfFirstName = new javax.swing.JTextField();
- dlgReg_pfPass = new javax.swing.JPasswordField();
- dlgReg_pfConfirmPass = new javax.swing.JPasswordField();
- setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
- jLabel5.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
- jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- jLabel5.setText("Register");
- jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
- jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
- dlgReg_btRegister.setText("Register");
- dlgReg_btRegister.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- dlgReg_btRegisterActionPerformed(evt);
- }
- });
- dlgReg_btCancel.setText("Cancel");
- dlgReg_btCancel.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- dlgReg_btCancelActionPerformed(evt);
- }
- });
- jLabel6.setText("Username (e-mail):");
- jLabel7.setText("Password:");
- jLabel8.setText("Confirm password:");
- jLabel2.setText("Last name:");
- jLabel10.setText("First name:");
- javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
- jPanel2.setLayout(jPanel2Layout);
- jPanel2Layout.setHorizontalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addComponent(dlgReg_btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(dlgReg_btRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addComponent(jLabel8)
- .addGap(18, 18, 18)
- .addComponent(dlgReg_pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel6)
- .addComponent(jLabel7)
- .addComponent(jLabel10))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(dlgReg_tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(dlgReg_tfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(dlgReg_tfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(dlgReg_pfPass, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addComponent(jLabel2))
- .addGap(0, 0, Short.MAX_VALUE)))
- .addContainerGap())
- );
- jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dlgReg_pfConfirmPass, dlgReg_pfPass, dlgReg_tfEmail, dlgReg_tfFirstName, dlgReg_tfLastName});
- jPanel2Layout.setVerticalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
- .addGap(22, 22, 22)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel10)
- .addComponent(dlgReg_tfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(dlgReg_tfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel6)
- .addComponent(dlgReg_tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel7)
- .addComponent(dlgReg_pfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel8)
- .addComponent(dlgReg_pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(dlgReg_btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(dlgReg_btRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- 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)
- .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>//GEN-END:initComponents
- private void dlgReg_btRegisterActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_dlgReg_btRegisterActionPerformed
- {//GEN-HEADEREND:event_dlgReg_btRegisterActionPerformed
- String firstName = dlgReg_tfFirstName.getText();
- String lastName = dlgReg_tfLastName.getText();
- String email = dlgReg_tfEmail.getText();
- String pass = new String(dlgReg_pfPass.getPassword());
- String confirmPass = new String(dlgReg_pfConfirmPass.getPassword());
- String passRegEx = "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}";
- if (firstName.equals(""))
- {
- JOptionPane.showMessageDialog(this, "Please enter your first name.", "Missing information", JOptionPane.WARNING_MESSAGE);
- return;
- } else if (lastName.equals(""))
- {
- JOptionPane.showMessageDialog(this, "Please enter your last name.", "Missing information", JOptionPane.WARNING_MESSAGE);
- return;
- } else 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))
- {
- JOptionPane.showMessageDialog(this, "This email is already registered.", "Resistration error", JOptionPane.WARNING_MESSAGE);
- return;
- }
- }
-
- if (!pass.matches(passRegEx))
- {
- JOptionPane.showMessageDialog(this, "Weak password. Must contain:\n"
- + "- 8 Characters or more.\n- 1 or more uppercase characters. \n- 1 or more numeric characters.", "Invalid password", JOptionPane.WARNING_MESSAGE);
- return;
- } else if (!pass.equals(confirmPass))
- {
- JOptionPane.showMessageDialog(this, "Passwords don't match.", "Invalid password", JOptionPane.WARNING_MESSAGE);
- return;
- }
-
- BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
- String encryptedPassword = passwordEncryptor.encryptPassword(pass);
-
- User user = new User(firstName, lastName, email, encryptedPassword);
- try
- {
- Globals.db.addUser(user);
- Globals.users = Globals.db.loadUsers();
- } catch (SQLException ex)
- {
- JOptionPane.showMessageDialog(this, "Could not execute SQL query\n." + ex.getMessage(), "Database error", JOptionPane.ERROR_MESSAGE);
- return;
- }
- JOptionPane.showMessageDialog(this, "Successfully registered!.", "Welcome!", JOptionPane.INFORMATION_MESSAGE);
- this.dispose();
- }//GEN-LAST:event_dlgReg_btRegisterActionPerformed
- private void dlgReg_btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_dlgReg_btCancelActionPerformed
- {//GEN-HEADEREND:event_dlgReg_btCancelActionPerformed
- this.setVisible(false);
- }//GEN-LAST:event_dlgReg_btCancelActionPerformed
- /**
- * @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(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex)
- {
- java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex)
- {
- java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex)
- {
- java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the dialog */
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- DlgRegister dialog = new DlgRegister(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 dlgReg_btCancel;
- private javax.swing.JButton dlgReg_btRegister;
- private javax.swing.JPasswordField dlgReg_pfConfirmPass;
- private javax.swing.JPasswordField dlgReg_pfPass;
- private javax.swing.JTextField dlgReg_tfEmail;
- private javax.swing.JTextField dlgReg_tfFirstName;
- private javax.swing.JTextField dlgReg_tfLastName;
- private javax.swing.JLabel jLabel10;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JLabel jLabel8;
- private javax.swing.JPanel jPanel2;
- // End of variables declaration//GEN-END:variables
- public static boolean validate(String emailStr) {
- Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
- return matcher.find();
- }
- }