PageRenderTime 42ms CodeModel.GetById 17ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/ui/dialogs/LoginToInstanceScreen.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 347 lines | 281 code | 46 blank | 20 comment | 13 complexity | c9e4f6e5c2f5b6b866d36703f5b0f745 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.event.WindowAdapter;
  9import java.awt.event.WindowEvent;
 10import java.security.NoSuchAlgorithmException;
 11import java.util.List;
 12import java.util.logging.Level;
 13import java.util.logging.Logger;
 14import mpv5.Main;
 15import mpv5.db.common.NodataFoundException;
 16import mpv5.globals.LocalSettings;
 17import mpv5.globals.Messages;
 18import mpv5.logging.Log;
 19import mpv5.ui.misc.Position;
 20import mpv5.db.objects.User;
 21import mpv5.ui.dialogs.subcomponents.wizard_DBSettings_manage_1;
 22import mpv5.utils.text.MD5HashGenerator;
 23
 24/**
 25 *
 26 *   
 27 */
 28public class LoginToInstanceScreen extends javax.swing.JDialog {
 29
 30    private java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
 31
 32    /**
 33     * Show the instance login screen, modal
 34     */
 35    public static void load() {
 36        LoginToInstanceScreen s = new LoginToInstanceScreen();
 37        s.requestFocus();
 38        s.dispose();
 39    }
 40
 41    private LoginToInstanceScreen() {
 42        super();
 43        initComponents();
 44        setModalityType(ModalityType.APPLICATION_MODAL);
 45
 46        setList();
 47
 48        if (!LocalSettings.getProperty("lastuser").equals("null")) {
 49            jCheckBox1.setSelected(true);
 50            try {
 51                jTextField1.setText(new User(Integer.valueOf(LocalSettings.getProperty("lastuser"))).getName());
 52                jPasswordField1.requestFocus();
 53            } catch (Exception ex) {
 54                Log.Debug(this, ex.getMessage());
 55                jTextField1.requestFocus();
 56            }
 57        }
 58
 59        this.addWindowListener(new WindowAdapter() {
 60
 61            @Override
 62            public void windowClosing(WindowEvent e) {
 63                close();
 64            }
 65
 66            private void close() {
 67                dispose();
 68            }
 69        });
 70        Position position = new Position(this);
 71        this.setVisible(true);
 72    }
 73
 74    private void ddispose() {
 75        this.dispose();
 76    }
 77
 78    /** This method is called from within the constructor to
 79     * initialize the form.
 80     * WARNING: Do NOT modify this code. The content of this method is
 81     * always regenerated by the Form Editor.
 82     */
 83    @SuppressWarnings("unchecked")
 84    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 85    private void initComponents() {
 86
 87        jPanel1 = new javax.swing.JPanel();
 88        jLabel1 = new javax.swing.JLabel();
 89        jTextField1 = new javax.swing.JTextField();
 90        jLabel2 = new javax.swing.JLabel();
 91        jButton1 = new javax.swing.JButton();
 92        jPasswordField1 = new javax.swing.JPasswordField();
 93        jLabel3 = new javax.swing.JLabel();
 94        jCheckBox1 = new javax.swing.JCheckBox();
 95        jCheckBox2 = new javax.swing.JCheckBox();
 96        jLabel4 = new javax.swing.JLabel();
 97        jLabel5 = new javax.swing.JLabel();
 98        mPCombobox1 = new mpv5.ui.beans.MPCombobox();
 99        jButton2 = new javax.swing.JButton();
100        jButton3 = new javax.swing.JButton();
101
102        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
103        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
104        setTitle(bundle.getString("LoginToInstanceScreen.title")); // NOI18N
105        setAlwaysOnTop(true);
106        setFocusTraversalPolicyProvider(true);
107        setResizable(false);
108
109        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("LoginToInstanceScreen.jPanel1.border.title"))); // NOI18N
110
111        jLabel1.setText(bundle.getString("LoginToInstanceScreen.jLabel1.text")); // NOI18N
112
113        jLabel2.setText(bundle.getString("LoginToInstanceScreen.jLabel2.text")); // NOI18N
114
115        jButton1.setText(bundle.getString("LoginToInstanceScreen.jButton1.text")); // NOI18N
116        jButton1.addActionListener(new java.awt.event.ActionListener() {
117            public void actionPerformed(java.awt.event.ActionEvent evt) {
118                jButton1ActionPerformed(evt);
119            }
120        });
121
122        jPasswordField1.setToolTipText(bundle.getString("LoginToInstanceScreen.jPasswordField1.toolTipText")); // NOI18N
123        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
124            public void actionPerformed(java.awt.event.ActionEvent evt) {
125                jPasswordField1ActionPerformed(evt);
126            }
127        });
128
129        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/lock.png"))); // NOI18N
130
131        jCheckBox1.setText(bundle.getString("LoginToInstanceScreen.jCheckBox1.text")); // NOI18N
132
133        jCheckBox2.setText(bundle.getString("LoginToInstanceScreen.jCheckBox2.text")); // NOI18N
134
135        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 11));
136        jLabel4.setText(bundle.getString("LoginToInstanceScreen.jLabel4.text")); // NOI18N
137
138        jLabel5.setText(bundle.getString("LoginToInstanceScreen.jLabel5.text")); // NOI18N
139
140        mPCombobox1.setOpaque(true);
141
142        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/16/add.png"))); // NOI18N
143        jButton2.setText(bundle.getString("LoginToInstanceScreen.jButton2.text")); // NOI18N
144        jButton2.setAlignmentY(0.0F);
145        jButton2.setIconTextGap(0);
146        jButton2.setMargin(new java.awt.Insets(2, 2, 2, 2));
147        jButton2.addActionListener(new java.awt.event.ActionListener() {
148            public void actionPerformed(java.awt.event.ActionEvent evt) {
149                jButton2ActionPerformed(evt);
150            }
151        });
152
153        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/16/remove.png"))); // NOI18N
154        jButton3.setText(bundle.getString("LoginToInstanceScreen.jButton3.text")); // NOI18N
155        jButton3.setAlignmentY(0.0F);
156        jButton3.setIconTextGap(0);
157        jButton3.setMargin(new java.awt.Insets(2, 2, 2, 2));
158        jButton3.addActionListener(new java.awt.event.ActionListener() {
159            public void actionPerformed(java.awt.event.ActionEvent evt) {
160                jButton3ActionPerformed(evt);
161            }
162        });
163
164        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
165        jPanel1.setLayout(jPanel1Layout);
166        jPanel1Layout.setHorizontalGroup(
167            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
168            .addGroup(jPanel1Layout.createSequentialGroup()
169                .addContainerGap()
170                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
171                    .addComponent(jLabel2)
172                    .addComponent(jLabel5)
173                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
174                        .addComponent(jLabel1)
175                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
176                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
177                        .addGap(55, 55, 55))
178                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
179                        .addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING)
180                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
181                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
182                            .addComponent(mPCombobox1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
183                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
184                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
185                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
186                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
187                    .addGroup(jPanel1Layout.createSequentialGroup()
188                        .addComponent(jLabel3)
189                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
190                        .addComponent(jCheckBox1)
191                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
192                        .addComponent(jCheckBox2)
193                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
194                        .addComponent(jButton1)))
195                .addContainerGap())
196        );
197        jPanel1Layout.setVerticalGroup(
198            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199            .addGroup(jPanel1Layout.createSequentialGroup()
200                .addComponent(jLabel5)
201                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
203                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
204                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
205                    .addComponent(mPCombobox1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
206                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
207                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
208                    .addComponent(jLabel1)
209                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
210                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
211                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
212                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
213                .addComponent(jLabel2)
214                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
215                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
216                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
217                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
218                    .addComponent(jCheckBox2)
219                    .addComponent(jCheckBox1)
220                    .addComponent(jButton1)
221                    .addComponent(jLabel3))
222                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
223        );
224
225        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
226        getContentPane().setLayout(layout);
227        layout.setHorizontalGroup(
228            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
230        );
231        layout.setVerticalGroup(
232            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
233            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
234        );
235
236        pack();
237    }// </editor-fold>//GEN-END:initComponents
238
239private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
240
241    login();
242}//GEN-LAST:event_jButton1ActionPerformed
243
244private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
245    login();
246
247}//GEN-LAST:event_jPasswordField1ActionPerformed
248
249private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
250
251    Wizard w = new Wizard(false);
252    w.setModalityType(w.getModalityType().APPLICATION_MODAL);
253    w.addPanel(new wizard_DBSettings_manage_1(w));
254    w.showWiz();
255    setList();
256}//GEN-LAST:event_jButton2ActionPerformed
257
258private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
259    Object idobj = mPCombobox1.getSelectedItemId();
260
261    if (idobj != null) {
262        Integer id = Integer.valueOf(idobj.toString());
263        try {
264            LocalSettings.removeInstance(id);
265        } catch (Exception ex) {
266            Log.Debug(ex);
267        }
268
269        setList();
270    }
271
272}//GEN-LAST:event_jButton3ActionPerformed
273    // Variables declaration - do not modify//GEN-BEGIN:variables
274    private javax.swing.JButton jButton1;
275    private javax.swing.JButton jButton2;
276    private javax.swing.JButton jButton3;
277    private javax.swing.JCheckBox jCheckBox1;
278    private javax.swing.JCheckBox jCheckBox2;
279    private javax.swing.JLabel jLabel1;
280    private javax.swing.JLabel jLabel2;
281    private javax.swing.JLabel jLabel3;
282    private javax.swing.JLabel jLabel4;
283    private javax.swing.JLabel jLabel5;
284    private javax.swing.JPanel jPanel1;
285    private javax.swing.JPasswordField jPasswordField1;
286    private javax.swing.JTextField jTextField1;
287    private mpv5.ui.beans.MPCombobox mPCombobox1;
288    // End of variables declaration//GEN-END:variables
289
290    private void login() {
291        Object idobj = mPCombobox1.getSelectedItemId();
292
293        if (idobj != null) {
294
295            Integer id = Integer.valueOf(idobj.toString());
296            Log.Debug(Main.class, "Switching connection id to: " + id);
297            LocalSettings.setConnectionID(id);
298            try {
299                LocalSettings.read();
300                LocalSettings.apply();
301            } catch (Exception ex) {
302                Log.Debug(Main.class, ex.getMessage());
303                Popup.error(this, "Local settings file not readable: " + LocalSettings.getLocalFile() + "\n" + ex);
304            }
305            User user = mpv5.usermanagement.MPSecurityManager.checkAuth(jTextField1.getText(), new String(jPasswordField1.getPassword()));
306            if (user != null) {
307                user.login();
308                this.setVisible(false);
309
310                if (jCheckBox1.isSelected()) {
311                    LocalSettings.setProperty("lastuser", mpv5.db.objects.User.getCurrentUser().__getIDS().toString());
312                } else {
313                    LocalSettings.setProperty("lastuser", "INSTANCE");
314                }
315
316                if (jCheckBox2.isSelected()) {
317                    try {
318                        LocalSettings.setProperty("lastuserpw", MD5HashGenerator.getInstance().hashData(jPasswordField1.getPassword()));
319                    } catch (NoSuchAlgorithmException ex) {
320                        Log.Debug(ex);
321                    }
322                } else {
323                    LocalSettings.setProperty("lastuserpw", "fdgdfDDRDFGFGFCVGEDgefg45g");
324                }
325                LocalSettings.save();
326                this.dispose();
327            } else {
328                jLabel4.setText(Messages.ACCESS_DENIED.getValue());
329            }
330        }
331    }
332
333    private void setList() {
334        try {
335            List<Integer> list = LocalSettings.getConnectionIDs();
336            Object[][] data = new Object[list.size()][2];
337            for (int i = 0; i < list.size(); i++) {
338                Integer integer = list.get(i);
339                data[i][0] = integer;
340                data[i][1] = "Connection ID " + integer;
341            }
342            mPCombobox1.setModel(data);
343        } catch (Exception ex) {
344            Log.Debug(ex);
345        }
346    }
347}