PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/dialogs/LoginScreen.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 231 lines | 180 code | 31 blank | 20 comment | 8 complexity | 12d086df92669cf9dc717e954dd6c950 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * login.java
  3. *
  4. * Created on 7. August 2008, 21:47
  5. */
  6. package mpv5.ui.dialogs;
  7. import java.awt.Component;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import java.security.NoSuchAlgorithmException;
  11. import mpv5.Main;
  12. import mpv5.db.common.NodataFoundException;
  13. import mpv5.globals.LocalSettings;
  14. import mpv5.globals.Messages;
  15. import mpv5.logging.Log;
  16. import mpv5.ui.frames.MPView;
  17. import mpv5.ui.misc.Position;
  18. import mpv5.db.objects.User;
  19. import mpv5.usermanagement.Lock;
  20. import mpv5.utils.text.MD5HashGenerator;
  21. /**
  22. *
  23. *
  24. */
  25. public class LoginScreen extends javax.swing.JFrame {
  26. /** Creates new form login
  27. * @param toUnlock
  28. */
  29. public LoginScreen(Component toUnlock) {
  30. initComponents();
  31. if (!LocalSettings.getProperty("lastuser").equals("null")) {
  32. jCheckBox1.setSelected(true);
  33. try {
  34. jTextField1.setText(new User(Integer.valueOf(LocalSettings.getProperty("lastuser"))).getName());
  35. } catch (NodataFoundException ex) {
  36. Log.Debug(this, ex);
  37. }
  38. jPasswordField1.requestFocus();
  39. }
  40. this.addWindowListener(new WindowAdapter() {
  41. @Override
  42. public void windowClosing(WindowEvent e) {
  43. close();
  44. }
  45. private void close() {
  46. LocalSettings.setProperty(LocalSettings.LAST_USER, null);
  47. LocalSettings.setProperty(LocalSettings.LAST_USER_PW, null);
  48. LocalSettings.save();
  49. Main.getApplication().exit();
  50. }
  51. });
  52. new Position(this);
  53. this.setVisible(rootPaneCheckingEnabled);
  54. }
  55. /** This method is called from within the constructor to
  56. * initialize the form.
  57. * WARNING: Do NOT modify this code. The content of this method is
  58. * always regenerated by the Form Editor.
  59. */
  60. @SuppressWarnings("unchecked")
  61. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  62. private void initComponents() {
  63. jPanel1 = new javax.swing.JPanel();
  64. jLabel1 = new javax.swing.JLabel();
  65. jTextField1 = new javax.swing.JTextField();
  66. jLabel2 = new javax.swing.JLabel();
  67. jButton1 = new javax.swing.JButton();
  68. jPasswordField1 = new javax.swing.JPasswordField();
  69. jLabel3 = new javax.swing.JLabel();
  70. jCheckBox1 = new javax.swing.JCheckBox();
  71. jCheckBox2 = new javax.swing.JCheckBox();
  72. jLabel4 = new javax.swing.JLabel();
  73. setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  74. java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
  75. setTitle(bundle.getString("LoginScreen.title")); // NOI18N
  76. setAlwaysOnTop(true);
  77. setFocusTraversalPolicyProvider(true);
  78. setResizable(false);
  79. jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("LoginScreen.jPanel1.border.title"))); // NOI18N
  80. jLabel1.setText(bundle.getString("LoginScreen.jLabel1.text")); // NOI18N
  81. jLabel2.setText(bundle.getString("LoginScreen.jLabel2.text")); // NOI18N
  82. jButton1.setText(bundle.getString("LoginScreen.jButton1.text")); // NOI18N
  83. jButton1.addActionListener(new java.awt.event.ActionListener() {
  84. public void actionPerformed(java.awt.event.ActionEvent evt) {
  85. jButton1ActionPerformed(evt);
  86. }
  87. });
  88. jPasswordField1.setToolTipText(bundle.getString("LoginScreen.jPasswordField1.toolTipText")); // NOI18N
  89. jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
  90. public void actionPerformed(java.awt.event.ActionEvent evt) {
  91. jPasswordField1ActionPerformed(evt);
  92. }
  93. });
  94. jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/lock.png"))); // NOI18N
  95. jCheckBox1.setText(bundle.getString("LoginScreen.jCheckBox1.text")); // NOI18N
  96. jCheckBox2.setText(bundle.getString("LoginScreen.jCheckBox2.text")); // NOI18N
  97. jLabel4.setFont(new java.awt.Font("Tahoma", 2, 11));
  98. jLabel4.setText(bundle.getString("LoginScreen.jLabel4.text")); // NOI18N
  99. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  100. jPanel1.setLayout(jPanel1Layout);
  101. jPanel1Layout.setHorizontalGroup(
  102. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103. .addGroup(jPanel1Layout.createSequentialGroup()
  104. .addContainerGap()
  105. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106. .addGroup(jPanel1Layout.createSequentialGroup()
  107. .addComponent(jLabel3)
  108. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  109. .addComponent(jCheckBox1)
  110. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  111. .addComponent(jCheckBox2)
  112. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
  113. .addComponent(jButton1))
  114. .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
  115. .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
  116. .addGroup(jPanel1Layout.createSequentialGroup()
  117. .addComponent(jLabel1)
  118. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  119. .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
  120. .addComponent(jLabel2))
  121. .addContainerGap())
  122. );
  123. jPanel1Layout.setVerticalGroup(
  124. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  125. .addGroup(jPanel1Layout.createSequentialGroup()
  126. .addContainerGap()
  127. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  128. .addComponent(jLabel1)
  129. .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
  130. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  131. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  132. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  133. .addComponent(jLabel2)
  134. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135. .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  136. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138. .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
  139. .addComponent(jCheckBox1, javax.swing.GroupLayout.Alignment.TRAILING)
  140. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  141. .addComponent(jButton1)
  142. .addComponent(jCheckBox2)))
  143. .addContainerGap(13, Short.MAX_VALUE))
  144. );
  145. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  146. getContentPane().setLayout(layout);
  147. layout.setHorizontalGroup(
  148. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  149. .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  150. );
  151. layout.setVerticalGroup(
  152. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  154. );
  155. pack();
  156. }// </editor-fold>//GEN-END:initComponents
  157. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  158. login();
  159. }//GEN-LAST:event_jButton1ActionPerformed
  160. private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
  161. login();
  162. }//GEN-LAST:event_jPasswordField1ActionPerformed
  163. // Variables declaration - do not modify//GEN-BEGIN:variables
  164. private javax.swing.JButton jButton1;
  165. private javax.swing.JCheckBox jCheckBox1;
  166. private javax.swing.JCheckBox jCheckBox2;
  167. private javax.swing.JLabel jLabel1;
  168. private javax.swing.JLabel jLabel2;
  169. private javax.swing.JLabel jLabel3;
  170. private javax.swing.JLabel jLabel4;
  171. private javax.swing.JPanel jPanel1;
  172. private javax.swing.JPasswordField jPasswordField1;
  173. private javax.swing.JTextField jTextField1;
  174. // End of variables declaration//GEN-END:variables
  175. private void login() {
  176. User user = mpv5.usermanagement.MPSecurityManager.checkAuth(jTextField1.getText(), new String(jPasswordField1.getPassword()));
  177. if (user != null) {
  178. user.login();
  179. Lock.unlock(mpv5.YabsViewProxy.instance().getIdentifierFrame());
  180. if (jCheckBox1.isSelected()) {
  181. LocalSettings.setProperty("lastuser", mpv5.db.objects.User.getCurrentUser().__getIDS().toString());
  182. } else {
  183. LocalSettings.setProperty("lastuser", "INSTANCE");
  184. }
  185. if (jCheckBox2.isSelected()) {
  186. try {
  187. LocalSettings.setProperty("lastuserpw", MD5HashGenerator.getInstance().hashData(jPasswordField1.getPassword()));
  188. } catch (NoSuchAlgorithmException ex) {
  189. Log.Debug(ex);
  190. }
  191. } else {
  192. LocalSettings.setProperty("lastuserpw", "ZFIFZIUG876KJIHJu888iouu");
  193. }
  194. LocalSettings.save();
  195. this.dispose();
  196. } else {
  197. jLabel4.setText(Messages.ACCESS_DENIED.getValue());
  198. }
  199. }
  200. }