PageRenderTime 85ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/MovieTicketing/src/ipd12/movieticketing/DlgRegister.java

https://bitbucket.org/RobertoDiBi/java3-project
Java | 321 lines | 262 code | 31 blank | 28 comment | 14 complexity | cfec16203a320348ab3464310ae58396 MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package ipd12.movieticketing;
  7. import java.sql.SQLException;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import javax.swing.JOptionPane;
  11. import org.jasypt.util.password.BasicPasswordEncryptor;
  12. /**
  13. *
  14. * @author 1796174
  15. */
  16. public class DlgRegister extends javax.swing.JDialog
  17. {
  18. public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  19. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  20. /**
  21. * Creates new form DlgRegister
  22. */
  23. public DlgRegister(java.awt.Frame parent, boolean modal)
  24. {
  25. super(parent, modal);
  26. initComponents();
  27. }
  28. /**
  29. * 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.
  30. */
  31. @SuppressWarnings("unchecked")
  32. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  33. private void initComponents() {
  34. jLabel5 = new javax.swing.JLabel();
  35. jPanel2 = new javax.swing.JPanel();
  36. dlgReg_btRegister = new javax.swing.JButton();
  37. dlgReg_btCancel = new javax.swing.JButton();
  38. jLabel6 = new javax.swing.JLabel();
  39. dlgReg_tfEmail = new javax.swing.JTextField();
  40. jLabel7 = new javax.swing.JLabel();
  41. jLabel8 = new javax.swing.JLabel();
  42. jLabel2 = new javax.swing.JLabel();
  43. jLabel10 = new javax.swing.JLabel();
  44. dlgReg_tfLastName = new javax.swing.JTextField();
  45. dlgReg_tfFirstName = new javax.swing.JTextField();
  46. dlgReg_pfPass = new javax.swing.JPasswordField();
  47. dlgReg_pfConfirmPass = new javax.swing.JPasswordField();
  48. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  49. jLabel5.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
  50. jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  51. jLabel5.setText("Register");
  52. jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  53. jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  54. dlgReg_btRegister.setText("Register");
  55. dlgReg_btRegister.addActionListener(new java.awt.event.ActionListener() {
  56. public void actionPerformed(java.awt.event.ActionEvent evt) {
  57. dlgReg_btRegisterActionPerformed(evt);
  58. }
  59. });
  60. dlgReg_btCancel.setText("Cancel");
  61. dlgReg_btCancel.addActionListener(new java.awt.event.ActionListener() {
  62. public void actionPerformed(java.awt.event.ActionEvent evt) {
  63. dlgReg_btCancelActionPerformed(evt);
  64. }
  65. });
  66. jLabel6.setText("Username (e-mail):");
  67. jLabel7.setText("Password:");
  68. jLabel8.setText("Confirm password:");
  69. jLabel2.setText("Last name:");
  70. jLabel10.setText("First name:");
  71. javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  72. jPanel2.setLayout(jPanel2Layout);
  73. jPanel2Layout.setHorizontalGroup(
  74. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75. .addGroup(jPanel2Layout.createSequentialGroup()
  76. .addContainerGap()
  77. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78. .addGroup(jPanel2Layout.createSequentialGroup()
  79. .addComponent(dlgReg_btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
  80. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  81. .addComponent(dlgReg_btRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
  82. .addGroup(jPanel2Layout.createSequentialGroup()
  83. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  84. .addGroup(jPanel2Layout.createSequentialGroup()
  85. .addComponent(jLabel8)
  86. .addGap(18, 18, 18)
  87. .addComponent(dlgReg_pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
  88. .addGroup(jPanel2Layout.createSequentialGroup()
  89. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90. .addComponent(jLabel6)
  91. .addComponent(jLabel7)
  92. .addComponent(jLabel10))
  93. .addGap(18, 18, 18)
  94. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95. .addComponent(dlgReg_tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
  96. .addComponent(dlgReg_tfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
  97. .addComponent(dlgReg_tfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
  98. .addComponent(dlgReg_pfPass, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)))
  99. .addComponent(jLabel2))
  100. .addGap(0, 0, Short.MAX_VALUE)))
  101. .addContainerGap())
  102. );
  103. jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dlgReg_pfConfirmPass, dlgReg_pfPass, dlgReg_tfEmail, dlgReg_tfFirstName, dlgReg_tfLastName});
  104. jPanel2Layout.setVerticalGroup(
  105. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  107. .addGap(22, 22, 22)
  108. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  109. .addComponent(jLabel10)
  110. .addComponent(dlgReg_tfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  111. .addGap(18, 18, 18)
  112. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  113. .addComponent(jLabel2)
  114. .addComponent(dlgReg_tfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  115. .addGap(18, 18, 18)
  116. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  117. .addComponent(jLabel6)
  118. .addComponent(dlgReg_tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  119. .addGap(18, 18, 18)
  120. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  121. .addComponent(jLabel7)
  122. .addComponent(dlgReg_pfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  123. .addGap(18, 18, 18)
  124. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  125. .addComponent(jLabel8)
  126. .addComponent(dlgReg_pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  127. .addGap(18, 18, 18)
  128. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  129. .addComponent(dlgReg_btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
  130. .addComponent(dlgReg_btRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
  131. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  132. );
  133. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  134. getContentPane().setLayout(layout);
  135. layout.setHorizontalGroup(
  136. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  137. .addGroup(layout.createSequentialGroup()
  138. .addContainerGap()
  139. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140. .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  141. .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  142. .addContainerGap())
  143. );
  144. layout.setVerticalGroup(
  145. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  146. .addGroup(layout.createSequentialGroup()
  147. .addContainerGap()
  148. .addComponent(jLabel5)
  149. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  150. .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  151. .addContainerGap())
  152. );
  153. pack();
  154. setLocationRelativeTo(null);
  155. }// </editor-fold>//GEN-END:initComponents
  156. private void dlgReg_btRegisterActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_dlgReg_btRegisterActionPerformed
  157. {//GEN-HEADEREND:event_dlgReg_btRegisterActionPerformed
  158. String firstName = dlgReg_tfFirstName.getText();
  159. String lastName = dlgReg_tfLastName.getText();
  160. String email = dlgReg_tfEmail.getText();
  161. String pass = new String(dlgReg_pfPass.getPassword());
  162. String confirmPass = new String(dlgReg_pfConfirmPass.getPassword());
  163. String passRegEx = "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}";
  164. if (firstName.equals(""))
  165. {
  166. JOptionPane.showMessageDialog(this, "Please enter your first name.", "Missing information", JOptionPane.WARNING_MESSAGE);
  167. return;
  168. } else if (lastName.equals(""))
  169. {
  170. JOptionPane.showMessageDialog(this, "Please enter your last name.", "Missing information", JOptionPane.WARNING_MESSAGE);
  171. return;
  172. } else if (email.equals(""))
  173. {
  174. JOptionPane.showMessageDialog(this, "Please enter your e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  175. return;
  176. } else if (!validate(email))
  177. {
  178. JOptionPane.showMessageDialog(this, "Please enter a valid e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  179. return;
  180. }
  181. for (User u : Globals.users)
  182. {
  183. if (u.email.equals(email))
  184. {
  185. JOptionPane.showMessageDialog(this, "This email is already registered.", "Resistration error", JOptionPane.WARNING_MESSAGE);
  186. return;
  187. }
  188. }
  189. if (!pass.matches(passRegEx))
  190. {
  191. JOptionPane.showMessageDialog(this, "Weak password. Must contain:\n"
  192. + "- 8 Characters or more.\n- 1 or more uppercase characters. \n- 1 or more numeric characters.", "Invalid password", JOptionPane.WARNING_MESSAGE);
  193. return;
  194. } else if (!pass.equals(confirmPass))
  195. {
  196. JOptionPane.showMessageDialog(this, "Passwords don't match.", "Invalid password", JOptionPane.WARNING_MESSAGE);
  197. return;
  198. }
  199. BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
  200. String encryptedPassword = passwordEncryptor.encryptPassword(pass);
  201. User user = new User(firstName, lastName, email, encryptedPassword);
  202. try
  203. {
  204. Globals.db.addUser(user);
  205. Globals.users = Globals.db.loadUsers();
  206. } catch (SQLException ex)
  207. {
  208. JOptionPane.showMessageDialog(this, "Could not execute SQL query\n." + ex.getMessage(), "Database error", JOptionPane.ERROR_MESSAGE);
  209. return;
  210. }
  211. JOptionPane.showMessageDialog(this, "Successfully registered!.", "Welcome!", JOptionPane.INFORMATION_MESSAGE);
  212. this.dispose();
  213. }//GEN-LAST:event_dlgReg_btRegisterActionPerformed
  214. private void dlgReg_btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_dlgReg_btCancelActionPerformed
  215. {//GEN-HEADEREND:event_dlgReg_btCancelActionPerformed
  216. this.setVisible(false);
  217. }//GEN-LAST:event_dlgReg_btCancelActionPerformed
  218. /**
  219. * @param args the command line arguments
  220. */
  221. public static void main(String args[])
  222. {
  223. /* Set the Nimbus look and feel */
  224. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  225. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  226. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  227. */
  228. try
  229. {
  230. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  231. {
  232. if ("Nimbus".equals(info.getName()))
  233. {
  234. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  235. break;
  236. }
  237. }
  238. } catch (ClassNotFoundException ex)
  239. {
  240. java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  241. } catch (InstantiationException ex)
  242. {
  243. java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  244. } catch (IllegalAccessException ex)
  245. {
  246. java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  247. } catch (javax.swing.UnsupportedLookAndFeelException ex)
  248. {
  249. java.util.logging.Logger.getLogger(DlgRegister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  250. }
  251. //</editor-fold>
  252. /* Create and display the dialog */
  253. java.awt.EventQueue.invokeLater(new Runnable()
  254. {
  255. public void run()
  256. {
  257. DlgRegister dialog = new DlgRegister(new javax.swing.JFrame(), true);
  258. dialog.addWindowListener(new java.awt.event.WindowAdapter()
  259. {
  260. @Override
  261. public void windowClosing(java.awt.event.WindowEvent e)
  262. {
  263. System.exit(0);
  264. }
  265. });
  266. dialog.setVisible(true);
  267. }
  268. });
  269. }
  270. // Variables declaration - do not modify//GEN-BEGIN:variables
  271. private javax.swing.JButton dlgReg_btCancel;
  272. private javax.swing.JButton dlgReg_btRegister;
  273. private javax.swing.JPasswordField dlgReg_pfConfirmPass;
  274. private javax.swing.JPasswordField dlgReg_pfPass;
  275. private javax.swing.JTextField dlgReg_tfEmail;
  276. private javax.swing.JTextField dlgReg_tfFirstName;
  277. private javax.swing.JTextField dlgReg_tfLastName;
  278. private javax.swing.JLabel jLabel10;
  279. private javax.swing.JLabel jLabel2;
  280. private javax.swing.JLabel jLabel5;
  281. private javax.swing.JLabel jLabel6;
  282. private javax.swing.JLabel jLabel7;
  283. private javax.swing.JLabel jLabel8;
  284. private javax.swing.JPanel jPanel2;
  285. // End of variables declaration//GEN-END:variables
  286. public static boolean validate(String emailStr) {
  287. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  288. return matcher.find();
  289. }
  290. }