001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019/*
020 * ChangePassword.java
021 *
022 * Created on 18/Abr/2010, 16:33:42
023 */
024package pt.ua.dicoogle.rGUI.client.windows;
025
026import java.awt.Image;
027import java.awt.Toolkit;
028import java.awt.event.KeyEvent;
029import java.rmi.RemoteException;
030import java.util.concurrent.Semaphore;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035import javax.swing.JOptionPane;
036import pt.ua.dicoogle.rGUI.client.ClientCore;
037import pt.ua.dicoogle.rGUI.interfaces.IUser;
038import pt.ua.dicoogle.server.users.HashService;
039
040/**
041 *
042 * @author samuelcampos
043 */
044@Deprecated
045public class ChangePassword extends javax.swing.JFrame {
046
047    private static Semaphore sem = new Semaphore(1, true);
048    private static ChangePassword instance = null;
049
050    private IUser user;
051
052    public static synchronized ChangePassword getInstance() {
053        try {
054            sem.acquire();
055            if (instance == null) {
056                instance = new ChangePassword();
057            }
058            sem.release();
059        } catch (InterruptedException ex) {
060            LoggerFactory.getLogger(ChangePassword.class).error(ex.getMessage(), ex);
061        }
062        return instance;
063    }
064
065    /** Creates new form ChangePassword */
066    private ChangePassword() {
067        initComponents();
068
069        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
070        this.setIconImage(image);
071
072        this.setTitle("Change User Password");
073        
074        user = ClientCore.getInstance().getUser();
075        
076        try {
077            jLabelUsername.setText(user.getUsername());
078        } catch (RemoteException ex) {
079            LoggerFactory.getLogger(ChangePassword.class).error(ex.getMessage(), ex);
080        }
081    }
082
083    /** This method is called from within the constructor to
084     * initialize the form.
085     * WARNING: Do NOT modify this code. The content of this method is
086     * always regenerated by the Form Editor.
087     */
088    @SuppressWarnings("unchecked")
089    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
090    private void initComponents() {
091
092        jLabel1 = new javax.swing.JLabel();
093        jLabel2 = new javax.swing.JLabel();
094        jLabel3 = new javax.swing.JLabel();
095        jLabel4 = new javax.swing.JLabel();
096        jLabelUsername = new javax.swing.JLabel();
097        jPasswordFieldOld = new javax.swing.JPasswordField();
098        jPasswordFieldNew = new javax.swing.JPasswordField();
099        jPasswordFieldConfirm = new javax.swing.JPasswordField();
100        jButtonCancel = new javax.swing.JButton();
101        jButtonChangePass = new javax.swing.JButton();
102
103        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
104        setMinimumSize(new java.awt.Dimension(370, 213));
105        setPreferredSize(new java.awt.Dimension(370, 213));
106        setResizable(false);
107        setSize(new java.awt.Dimension(370, 213));
108
109        jLabel1.setText("Username:");
110
111        jLabel2.setText("Old Password:");
112
113        jLabel3.setText("New Password:");
114
115        jLabel4.setText("Confirm Password:");
116
117        jLabelUsername.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
118        jLabelUsername.setText("Username");
119
120        jPasswordFieldOld.addKeyListener(new java.awt.event.KeyAdapter() {
121            public void keyPressed(java.awt.event.KeyEvent evt) {
122                jPasswordFieldOldKeyPressed(evt);
123            }
124        });
125
126        jPasswordFieldNew.addKeyListener(new java.awt.event.KeyAdapter() {
127            public void keyPressed(java.awt.event.KeyEvent evt) {
128                jPasswordFieldNewKeyPressed(evt);
129            }
130        });
131
132        jPasswordFieldConfirm.addKeyListener(new java.awt.event.KeyAdapter() {
133            public void keyPressed(java.awt.event.KeyEvent evt) {
134                jPasswordFieldConfirmKeyPressed(evt);
135            }
136        });
137
138        jButtonCancel.setText("Cancel");
139        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
140            public void actionPerformed(java.awt.event.ActionEvent evt) {
141                jButtonCancelActionPerformed(evt);
142            }
143        });
144
145        jButtonChangePass.setText("Change Password");
146        jButtonChangePass.addActionListener(new java.awt.event.ActionListener() {
147            public void actionPerformed(java.awt.event.ActionEvent evt) {
148                jButtonChangePassActionPerformed(evt);
149            }
150        });
151
152        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
153        getContentPane().setLayout(layout);
154        layout.setHorizontalGroup(
155            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
156            .add(layout.createSequentialGroup()
157                .add(43, 43, 43)
158                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
159                    .add(jButtonCancel)
160                    .add(layout.createSequentialGroup()
161                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
162                            .add(jLabel3)
163                            .add(jLabel1)
164                            .add(jLabel2)
165                            .add(jLabel4))
166                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
167                            .add(layout.createSequentialGroup()
168                                .add(18, 18, 18)
169                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
170                                    .add(jPasswordFieldNew)
171                                    .add(jPasswordFieldOld)
172                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButtonChangePass, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
173                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPasswordFieldConfirm)))
174                            .add(layout.createSequentialGroup()
175                                .add(26, 26, 26)
176                                .add(jLabelUsername)))))
177                .addContainerGap(40, Short.MAX_VALUE))
178        );
179        layout.setVerticalGroup(
180            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
181            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
182                .addContainerGap(25, Short.MAX_VALUE)
183                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
184                    .add(layout.createSequentialGroup()
185                        .add(jLabelUsername)
186                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
187                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
188                            .add(jPasswordFieldOld, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
189                            .add(jLabel2))
190                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
191                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
192                            .add(jPasswordFieldNew, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
193                            .add(jLabel3))
194                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
195                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
196                            .add(jPasswordFieldConfirm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
197                            .add(jLabel4)))
198                    .add(jLabel1))
199                .add(17, 17, 17)
200                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
201                    .add(jButtonCancel)
202                    .add(jButtonChangePass))
203                .add(12, 12, 12))
204        );
205
206        pack();
207    }// </editor-fold>//GEN-END:initComponents
208
209    private void jButtonChangePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChangePassActionPerformed
210        String oldPass = new String(jPasswordFieldOld.getPassword());
211        String newPass = new String(jPasswordFieldNew.getPassword());
212        String confirmPass = new String(jPasswordFieldConfirm.getPassword());
213
214        if (oldPass.length() == 0 || newPass.length() == 0 || confirmPass.length() == 0) {
215            JOptionPane.showMessageDialog(this, "Password must have characters!",
216                    "Password Empty", JOptionPane.ERROR_MESSAGE);
217
218            return;
219        }
220         if (!newPass.equals(confirmPass)) {
221            JOptionPane.showMessageDialog(this, "Passwords must be equal!",
222                    "Diferent Passwords", JOptionPane.ERROR_MESSAGE);
223
224            return;
225        }
226
227        String oldPassHash = HashService.getSHA1Hash(oldPass);
228        String newPassHash = HashService.getSHA1Hash(newPass);
229
230        try {
231            if (!user.changePassword(oldPassHash, newPassHash)) {
232                JOptionPane.showMessageDialog(this, "Old Password is wrong!",
233                    "Wrong Password", JOptionPane.ERROR_MESSAGE);
234
235            return;
236            }
237            else{
238                JOptionPane.showMessageDialog(this, "Password successfully changed!",
239                    "Password Changed", JOptionPane.INFORMATION_MESSAGE);
240            }
241        } catch (RemoteException ex) {
242            JOptionPane.showMessageDialog(this, "Its impossible to change the password at this time!",
243                    "Connectivity error", JOptionPane.ERROR_MESSAGE);
244        }
245    }//GEN-LAST:event_jButtonChangePassActionPerformed
246
247    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
248        this.dispose();
249    }//GEN-LAST:event_jButtonCancelActionPerformed
250
251    private void jPasswordFieldOldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldOldKeyPressed
252        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
253            jButtonChangePass.doClick();
254    }//GEN-LAST:event_jPasswordFieldOldKeyPressed
255
256    private void jPasswordFieldNewKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldNewKeyPressed
257        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
258            jButtonChangePass.doClick();
259    }//GEN-LAST:event_jPasswordFieldNewKeyPressed
260
261    private void jPasswordFieldConfirmKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldConfirmKeyPressed
262        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
263            jButtonChangePass.doClick();
264    }//GEN-LAST:event_jPasswordFieldConfirmKeyPressed
265
266    // Variables declaration - do not modify//GEN-BEGIN:variables
267    private javax.swing.JButton jButtonCancel;
268    private javax.swing.JButton jButtonChangePass;
269    private javax.swing.JLabel jLabel1;
270    private javax.swing.JLabel jLabel2;
271    private javax.swing.JLabel jLabel3;
272    private javax.swing.JLabel jLabel4;
273    private javax.swing.JLabel jLabelUsername;
274    private javax.swing.JPasswordField jPasswordFieldConfirm;
275    private javax.swing.JPasswordField jPasswordFieldNew;
276    private javax.swing.JPasswordField jPasswordFieldOld;
277    // End of variables declaration//GEN-END:variables
278}