PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/MovieTicketing/src/ipd12/movieticketing/DlgLogin.java

https://bitbucket.org/RobertoDiBi/java3-project
Java | 258 lines | 207 code | 25 blank | 26 comment | 11 complexity | 4b996b7a6ccfe45cf9e6e7955c810200 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.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import javax.swing.JOptionPane;
  10. import org.jasypt.util.password.BasicPasswordEncryptor;
  11. /**
  12. *
  13. * @author 1796174
  14. */
  15. public class DlgLogin extends javax.swing.JDialog
  16. {
  17. public static final Pattern VALID_EMAIL_ADDRESS_REGEX
  18. = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  19. public DlgLogin(java.awt.Frame parent, boolean modal)
  20. {
  21. super(parent, modal);
  22. initComponents();
  23. }
  24. /**
  25. * 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.
  26. */
  27. @SuppressWarnings("unchecked")
  28. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  29. private void initComponents() {
  30. jLabel11 = new javax.swing.JLabel();
  31. jLabel12 = new javax.swing.JLabel();
  32. pfPass = new javax.swing.JPasswordField();
  33. jLabel13 = new javax.swing.JLabel();
  34. tfEmail = new javax.swing.JTextField();
  35. btLogin = new javax.swing.JButton();
  36. btCancel = new javax.swing.JButton();
  37. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  38. jLabel11.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
  39. jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  40. jLabel11.setText("Log in");
  41. jLabel11.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  42. jLabel12.setText("Username (e-mail):");
  43. jLabel13.setText("Password:");
  44. btLogin.setText("Log in");
  45. btLogin.addActionListener(new java.awt.event.ActionListener() {
  46. public void actionPerformed(java.awt.event.ActionEvent evt) {
  47. btLoginActionPerformed(evt);
  48. }
  49. });
  50. btCancel.setText("Cancel");
  51. btCancel.addActionListener(new java.awt.event.ActionListener() {
  52. public void actionPerformed(java.awt.event.ActionEvent evt) {
  53. btCancelActionPerformed(evt);
  54. }
  55. });
  56. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  57. getContentPane().setLayout(layout);
  58. layout.setHorizontalGroup(
  59. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  60. .addGroup(layout.createSequentialGroup()
  61. .addContainerGap()
  62. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  63. .addGroup(layout.createSequentialGroup()
  64. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  65. .addComponent(jLabel12)
  66. .addComponent(jLabel13))
  67. .addGap(18, 18, 18)
  68. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  69. .addComponent(tfEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
  70. .addComponent(pfPass)))
  71. .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
  72. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  73. .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
  74. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
  75. .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
  76. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  77. );
  78. layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCancel, btLogin});
  79. layout.setVerticalGroup(
  80. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81. .addGroup(layout.createSequentialGroup()
  82. .addContainerGap()
  83. .addComponent(jLabel11)
  84. .addGap(26, 26, 26)
  85. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  86. .addComponent(jLabel12)
  87. .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  88. .addGap(18, 18, 18)
  89. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  90. .addComponent(jLabel13)
  91. .addComponent(pfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  92. .addGap(42, 42, 42)
  93. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  94. .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
  95. .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
  96. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  97. );
  98. pack();
  99. setLocationRelativeTo(null);
  100. }// </editor-fold>//GEN-END:initComponents
  101. private void btLoginActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btLoginActionPerformed
  102. {//GEN-HEADEREND:event_btLoginActionPerformed
  103. String email = tfEmail.getText();
  104. String pass = new String(pfPass.getPassword());
  105. boolean userExists = false;
  106. BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
  107. if (email.equals(""))
  108. {
  109. JOptionPane.showMessageDialog(this, "Please enter your e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  110. return;
  111. } else if (!validate(email))
  112. {
  113. JOptionPane.showMessageDialog(this, "Please enter a valid e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  114. return;
  115. }
  116. for (User u : Globals.users)
  117. {
  118. if (u.email.equals(email))
  119. {
  120. userExists = true;
  121. if (passwordEncryptor.checkPassword(pass, u.password))
  122. {
  123. this.dispose();
  124. Globals.movieGUI.lblUserStatus.setText("Logged in as: " + u.getEmail());
  125. Globals.movieGUI.meRegister.setVisible(false);
  126. Globals.movieGUI.meLogin.setVisible(false);
  127. Globals.movieGUI.meUser.setVisible(true);
  128. Globals.movieGUI.meUser.setText(u.firstName);
  129. Globals.currentUser = u;
  130. Globals.userLogged = true;
  131. return;
  132. }
  133. }
  134. }
  135. if (userExists)
  136. {
  137. JOptionPane.showMessageDialog(this, "Wrong password. Please try again.", "Wrong password", JOptionPane.WARNING_MESSAGE);
  138. } else
  139. {
  140. Object[] options
  141. =
  142. {
  143. "Register", "Try again"
  144. };
  145. int decision = JOptionPane.showOptionDialog(null,
  146. "This email is not registered yet. Would you like to register an account?",
  147. "No record found",
  148. JOptionPane.YES_NO_OPTION,
  149. JOptionPane.WARNING_MESSAGE,
  150. null, //do not use a custom Icon
  151. options, //the titles of buttons
  152. options[0]); //default button title
  153. if (decision == JOptionPane.YES_OPTION)
  154. {
  155. this.dispose();
  156. DlgRegister dlgRegister = new DlgRegister(Globals.movieGUI, true);
  157. dlgRegister.pack();
  158. dlgRegister.setVisible(true);
  159. }
  160. }
  161. }//GEN-LAST:event_btLoginActionPerformed
  162. private void btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btCancelActionPerformed
  163. {//GEN-HEADEREND:event_btCancelActionPerformed
  164. this.dispose();
  165. }//GEN-LAST:event_btCancelActionPerformed
  166. public static boolean validate(String emailStr)
  167. {
  168. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  169. return matcher.find();
  170. }
  171. /**
  172. * @param args the command line arguments
  173. */
  174. public static void main(String args[])
  175. {
  176. /* Set the Nimbus look and feel */
  177. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  178. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  179. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  180. */
  181. try
  182. {
  183. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  184. {
  185. if ("Nimbus".equals(info.getName()))
  186. {
  187. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  188. break;
  189. }
  190. }
  191. } catch (ClassNotFoundException ex)
  192. {
  193. java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  194. } catch (InstantiationException ex)
  195. {
  196. java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  197. } catch (IllegalAccessException ex)
  198. {
  199. java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  200. } catch (javax.swing.UnsupportedLookAndFeelException ex)
  201. {
  202. java.util.logging.Logger.getLogger(DlgLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203. }
  204. //</editor-fold>
  205. //</editor-fold>
  206. /* Create and display the dialog */
  207. java.awt.EventQueue.invokeLater(new Runnable()
  208. {
  209. public void run()
  210. {
  211. DlgLogin dialog = new DlgLogin(new javax.swing.JFrame(), true);
  212. dialog.addWindowListener(new java.awt.event.WindowAdapter()
  213. {
  214. @Override
  215. public void windowClosing(java.awt.event.WindowEvent e)
  216. {
  217. System.exit(0);
  218. }
  219. });
  220. dialog.setVisible(true);
  221. }
  222. });
  223. }
  224. // Variables declaration - do not modify//GEN-BEGIN:variables
  225. private javax.swing.JButton btCancel;
  226. private javax.swing.JButton btLogin;
  227. private javax.swing.JLabel jLabel11;
  228. private javax.swing.JLabel jLabel12;
  229. private javax.swing.JLabel jLabel13;
  230. private javax.swing.JPasswordField pfPass;
  231. private javax.swing.JTextField tfEmail;
  232. // End of variables declaration//GEN-END:variables
  233. }