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