View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25   *
26   */
27  package com.drazzib.netpisteur.ui;
28  
29  import java.awt.BorderLayout;
30  import java.awt.Font;
31  import java.awt.GridLayout;
32  import java.awt.event.MouseAdapter;
33  import java.awt.event.MouseEvent;
34  
35  import javax.swing.JButton;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JOptionPane;
39  import javax.swing.JPanel;
40  import javax.swing.JPasswordField;
41  import javax.swing.SwingConstants;
42  
43  import com.drazzib.netpisteur.configuration.ListUtilisateurs;
44  import com.drazzib.netpisteur.configuration.Utilisateur;
45  import com.drazzib.netpisteur.util.Messages;
46  
47  /**
48   * This class open a frame wich allows to change the password of a specific user
49   */
50  public class ChangePassword extends JFrame {
51  
52  	/**
53  	 * 
54  	 */
55  	private static final long serialVersionUID = -300107571327655316L;
56  
57  	private JPanel panelAll, panelNorth, panelWest, panelEast, panelSouth,
58  			panelCenter, passwordPanel, titrePanel, boutonPanel;
59  
60  	private JLabel gestMotDePassseLabel, gestMotDePassseLabel2,
61  			nouveauPasswordLabel, confirmePasswordLabel;
62  
63  	private JPasswordField nouveauPasswordField, confirmePasswordField;
64  
65  	private JButton passwordOkBouton, passwordAnnulerBouton;
66  
67  	// //////////////////////////////
68  	//
69  	private String name;
70  
71  	private String firstname;
72  
73  	private String password;
74  
75  	private ConfigFrame myConfig;
76  
77  	// //////////////////////////////
78  	// constructor
79  
80  	/**
81  	 * Creates new form Config
82  	 * 
83  	 * @param user
84  	 *            the name of user
85  	 * @param myconf
86  	 *            the firstname of the user
87  	 */
88  	public ChangePassword(Utilisateur user, ConfigFrame myconf) {
89  		this.name = user.getNom();
90  		this.firstname = user.getPrenom();
91  		this.password = user.getMotDePasse();
92  		this.myConfig = myconf;
93  		initComponents();
94  	}
95  
96  	// //////////////////////////////
97  	// operations
98  
99  	private void initComponents() {
100 
101 		this.panelAll = new JPanel(new BorderLayout());
102 		this.panelNorth = new JPanel();
103 		this.panelWest = new JPanel();
104 		this.panelEast = new JPanel();
105 		this.panelSouth = new JPanel();
106 		this.panelCenter = new JPanel(new BorderLayout());
107 		this.passwordPanel = new JPanel();
108 		this.titrePanel = new JPanel();
109 		this.boutonPanel = new JPanel();
110 		this.gestMotDePassseLabel = new JLabel();
111 		this.gestMotDePassseLabel2 = new JLabel();
112 		this.nouveauPasswordLabel = new JLabel(Messages
113 				.getString("ChangeMotDePasse.NEW_PASSWORD")); //$NON-NLS-1$
114 		this.confirmePasswordLabel = new JLabel(Messages
115 				.getString("ChangeMotDePasse.CONFIRM_PASSWORD")); //$NON-NLS-1$
116 		this.nouveauPasswordField = new JPasswordField();
117 		this.confirmePasswordField = new JPasswordField();
118 		this.passwordOkBouton = new JButton();
119 		this.passwordAnnulerBouton = new JButton();
120 
121 		// Creation of the title's font
122 		Font titre = new Font("Arial", Font.BOLD, 15); //$NON-NLS-1$
123 
124 		// Password
125 		this.passwordPanel.setLayout(new GridLayout(6, 1));
126 		this.titrePanel.setLayout(new GridLayout(2, 1));
127 		this.boutonPanel.setLayout(new GridLayout(1, 2));
128 
129 		this.gestMotDePassseLabel.setText(Messages
130 				.getString("ChangeMotDePasse.MOD_PASSWORD")); //$NON-NLS-1$
131 		this.gestMotDePassseLabel2
132 				.setText(Messages.getString("ChangeMotDePasse.OF") + this.firstname + " " + this.name); //$NON-NLS-1$ //$NON-NLS-2$
133 		this.gestMotDePassseLabel.setHorizontalAlignment(SwingConstants.CENTER);
134 		this.gestMotDePassseLabel2
135 				.setHorizontalAlignment(SwingConstants.CENTER);
136 		this.gestMotDePassseLabel.setFont(titre);
137 		this.gestMotDePassseLabel2.setFont(titre);
138 		// gestMotDePassseLabel.setName("motPas1"); //$NON-NLS-1$
139 		// gestMotDePassseLabel2.setName("motPas2"); //$NON-NLS-1$
140 
141 		this.titrePanel.add(this.gestMotDePassseLabel);
142 		this.titrePanel.add(this.gestMotDePassseLabel2);
143 
144 		this.passwordPanel.add(new JLabel());
145 		this.passwordPanel.add(this.nouveauPasswordLabel);
146 		this.passwordPanel.add(this.nouveauPasswordField);
147 		this.passwordPanel.add(this.confirmePasswordLabel);
148 		this.passwordPanel.add(this.confirmePasswordField);
149 		this.passwordPanel.add(new JLabel());
150 
151 		this.passwordOkBouton
152 				.setText(Messages.getString("ChangeMotDePasse.OK")); //$NON-NLS-1$
153 		// passwordOkBouton.setName("ok"); //$NON-NLS-1$
154 		this.passwordOkBouton.addMouseListener(new MouseAdapter() {
155 			public void mouseReleased(MouseEvent evt) {
156 				passwordOkBoutonMouseReleased(evt);
157 			}
158 		});
159 
160 		this.boutonPanel.add(this.passwordOkBouton);
161 
162 		this.passwordAnnulerBouton.setText(Messages
163 				.getString("ChangeMotDePasse.CANCEL")); //$NON-NLS-1$
164 		// passwordAnnulerBouton.setName("annuler"); //$NON-NLS-1$
165 		this.passwordAnnulerBouton.addMouseListener(new MouseAdapter() {
166 			public void mouseReleased(MouseEvent evt) {
167 				passwordAnnulerBoutonMouseReleased(evt);
168 			}
169 		});
170 
171 		this.boutonPanel.add(this.passwordAnnulerBouton);
172 		this.panelCenter.add(this.titrePanel, BorderLayout.NORTH);
173 		this.panelCenter.add(this.passwordPanel, BorderLayout.CENTER);
174 		this.panelCenter.add(this.boutonPanel, BorderLayout.SOUTH);
175 
176 		this.panelAll.add(this.panelNorth, BorderLayout.NORTH);
177 		this.panelAll.add(this.panelWest, BorderLayout.WEST);
178 		this.panelAll.add(this.panelEast, BorderLayout.EAST);
179 		this.panelAll.add(this.panelSouth, BorderLayout.SOUTH);
180 		this.panelAll.add(this.panelCenter, BorderLayout.CENTER);
181 		getContentPane().add(this.panelAll);
182 
183 		this.setLocationRelativeTo(null);
184 		pack();
185 
186 	}
187 
188 	/**
189 	 * Allows to close the frame when the button cancel is pushed
190 	 * 
191 	 * @param MouseEvent
192 	 *            evt
193 	 */
194 	void passwordAnnulerBoutonMouseReleased(java.awt.event.MouseEvent evt) {
195 		this.dispose();
196 	}
197 
198 	/**
199 	 * Allows to close the frame and save the new password when the button ok is
200 	 * pushed
201 	 * 
202 	 * @param MouseEvent
203 	 *            evt
204 	 */
205 	void passwordOkBoutonMouseReleased(java.awt.event.MouseEvent evt) {
206 
207 		Utilisateur lUtilisateur;
208 
209 		String nouveauMotDePasseSaisie;
210 		String confirmeMotDePasseSaisie;
211 
212 		lUtilisateur = new Utilisateur(this.name, this.firstname,
213 				this.password, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
214 		nouveauMotDePasseSaisie = new String(this.nouveauPasswordField
215 				.getPassword());
216 		confirmeMotDePasseSaisie = new String(this.confirmePasswordField
217 				.getPassword());
218 
219 		// chargement des utilisateurs
220 		ListUtilisateurs.readUtilisateurInfoFromDisk();
221 		// verification du mot de passe
222 		boolean nouveaumotOk = (nouveauMotDePasseSaisie
223 				.equals(confirmeMotDePasseSaisie));
224 		boolean nonnull = !(nouveauMotDePasseSaisie.equals("")); //$NON-NLS-1$
225 
226 		if (nouveaumotOk == true && nonnull == true) {
227 
228 			ListUtilisateurs.changePasswd(lUtilisateur, nouveauMotDePasseSaisie);
229 			JOptionPane
230 					.showMessageDialog(
231 							null,
232 							Messages.getString("ChangeMotDePasse.CHANGE_OK"), Messages.getString("ChangeMotDePasse.CHANGE_OK_TITLE"), JOptionPane.INFORMATION_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
233 			this.myConfig.setAllFields(this.name, this.firstname,
234 					nouveauMotDePasseSaisie);
235 			dispose();
236 
237 		} else {
238 			JOptionPane
239 					.showMessageDialog(
240 							null,
241 							Messages.getString("ChangeMotDePasse.CHANGE_NOK"), Messages.getString("ChangeMotDePasse.CHANGE_NOK_TITLE"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
242 
243 		}
244 	}
245 
246 }