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 it under
13   * the terms of the GNU General Public License as published by the Free Software
14   * Foundation; either version 2 of the License, or (at your option) any later
15   * version.
16   * 
17   * NetPisteur is distributed in the hope that it will be useful, but WITHOUT ANY
18   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with
22   * NetPisteur; if not, write to the Free Software Foundation, Inc., 59 Temple
23   * Place, Suite 330, Boston, MA 02110-1301 USA
24   *  
25   */
26  package com.drazzib.netpisteur.ui;
27  
28  import java.awt.BorderLayout;
29  import java.awt.Font;
30  import java.awt.GridLayout;
31  import java.awt.event.MouseAdapter;
32  import java.awt.event.MouseEvent;
33  
34  import javax.swing.JButton;
35  import javax.swing.JFrame;
36  import javax.swing.JLabel;
37  import javax.swing.JOptionPane;
38  import javax.swing.JPanel;
39  import javax.swing.JPasswordField;
40  import javax.swing.JTextField;
41  
42  import com.drazzib.netpisteur.configuration.ListUtilisateurs;
43  import com.drazzib.netpisteur.configuration.Utilisateur;
44  import com.drazzib.netpisteur.util.Messages;
45  
46  public class CreateTestorFrame extends JFrame {
47  
48  	private static final long serialVersionUID = -1634246603370843049L;
49  
50  	private JPanel panelAll, panelNorth, panelWest, panelEast, panelSouth,
51  			panelCenter, creationPanel, boutonPanel, createPanel;
52  
53  	private JLabel createLabel, createNomLabel, createPrenomLabel,
54  			createPasswdLabel, expliCreateLabel, confirmeMotDePasseSaisieLabel;
55  
56  	private JPasswordField confirmeMotDePasseSaisieText, createPasswdField;
57  
58  	private JTextField createNomTextField, createPrenomTextField;
59  
60  	private JButton okBouton, annulerBouton;
61  
62  	private ConfigFrame myConfig;
63  
64  	/** Creates new form Config */
65  	public CreateTestorFrame(ConfigFrame pConfig) {
66  		this.myConfig = pConfig;
67  		initComponents();
68  	}
69  
70  	// //////////////////////////////
71  	// operations
72  
73  	private void initComponents() {
74  
75  		// Panels de base
76  		this.panelAll = new JPanel(new BorderLayout());
77  		this.panelNorth = new JPanel();
78  		this.panelWest = new JPanel();
79  		this.panelEast = new JPanel();
80  		this.panelSouth = new JPanel();
81  		this.panelCenter = new JPanel(new BorderLayout());
82  
83  		// Panel des boutons de la frame
84  		this.boutonPanel = new JPanel();
85  		// Panel principale
86  		this.creationPanel = new JPanel();
87  
88  		// Panel spcifique  la creation
89  		this.createPanel = new JPanel(new GridLayout(5, 2));
90  
91  		// Creation
92  		this.createLabel = new JLabel();
93  		this.expliCreateLabel = new JLabel();
94  		this.createNomLabel = new JLabel();
95  		this.createPrenomLabel = new JLabel();
96  		this.createPasswdLabel = new JLabel();
97  		this.confirmeMotDePasseSaisieLabel = new JLabel();
98  		this.confirmeMotDePasseSaisieText = new JPasswordField();
99  
100 		this.createNomTextField = new JTextField();
101 		this.createPrenomTextField = new JTextField();
102 		this.createPasswdField = new JPasswordField();
103 
104 		// Cration de la police des titres
105 		Font titre = new Font("Arial", Font.BOLD, 15); //$NON-NLS-1$
106 
107 		// creation d'un testeur avec le nom, le prenom, son mot de passe :
108 
109 		// taile des deux panels utilis
110 		this.creationPanel.setLayout(new GridLayout(7, 2));
111 		this.boutonPanel.setLayout(new GridLayout(1, 2));
112 
113 		// Option Create user :
114 		this.createLabel.setText(Messages
115 				.getString("CreationTesteur.CREATE_TESTER")); //$NON-NLS-1$
116 		this.createLabel.setFont(titre);
117 		// createLabel.setName("creation");
118 		this.creationPanel.add(this.createLabel);
119 
120 		this.creationPanel.add(new JLabel());
121 
122 		this.creationPanel.add(this.expliCreateLabel);
123 		this.creationPanel.add(new JLabel());
124 
125 		this.createNomLabel.setText(Messages.getString("CreationTesteur.NAME")); //$NON-NLS-1$
126 		// createNomTextField.setName("createNomTextField");
127 		this.createPrenomLabel.setText(Messages
128 				.getString("CreationTesteur.FIRST_NAME")); //$NON-NLS-1$
129 		// createPrenomTextField.setName("createPrenomTextField");
130 		this.createPasswdLabel.setText(Messages
131 				.getString("CreationTesteur.PASSWORD")); //$NON-NLS-1$
132 		// createPasswdField.setName("createPasswdTextField");
133 		// confirmation du mot de passe
134 		this.confirmeMotDePasseSaisieLabel.setText(Messages
135 				.getString("CreationTesteur.CONFIRM_PASSWORD")); //$NON-NLS-1$
136 		// confirmeMotDePasseSaisieText.setName("confirmation");
137 
138 		this.creationPanel.add(this.createNomLabel);
139 		this.creationPanel.add(this.createNomTextField);
140 		this.creationPanel.add(this.createPrenomLabel);
141 		this.creationPanel.add(this.createPrenomTextField);
142 		this.creationPanel.add(this.createPasswdLabel);
143 		this.creationPanel.add(this.createPasswdField);
144 		this.creationPanel.add(this.confirmeMotDePasseSaisieLabel);
145 		this.creationPanel.add(this.confirmeMotDePasseSaisieText);
146 
147 		this.creationPanel.add(this.createPanel);
148 		this.creationPanel.add(new JLabel());
149 
150 		// Bouton Ok et Annuler
151 
152 		// Panel des boutons de la frame
153 		this.boutonPanel = new JPanel();
154 		// creation des boutons :
155 		this.okBouton = new JButton();
156 		this.annulerBouton = new JButton();
157 
158 		// Bouton Ok (definition et methode lanc :okBoutonMouseReleased)
159 		this.okBouton.setText(Messages.getString("CreationTesteur.OK")); //$NON-NLS-1$
160 		// okBouton.setName("ok");
161 		this.okBouton.addMouseListener(new MouseAdapter() {
162 			public void mouseReleased(MouseEvent evt) {
163 				okBoutonMouseReleased(evt);
164 			}
165 		});
166 
167 		// Bouton Annuler (definition et methode lanc :
168 		// annulerBoutonMouseReleased)
169 		this.annulerBouton
170 				.setText(Messages.getString("CreationTesteur.CANCEL")); //$NON-NLS-1$
171 		// annulerBouton.setName("annuler");
172 		this.annulerBouton.addMouseListener(new MouseAdapter() {
173 			public void mouseReleased(MouseEvent evt) {
174 				annulerBoutonMouseReleased(evt);
175 
176 			}
177 		});
178 
179 		// ajout des deux boutons  la frame
180 
181 		this.boutonPanel.add(this.okBouton);
182 		this.boutonPanel.add(this.annulerBouton);
183 
184 		// Configurations de Base
185 
186 		// titre de la frame
187 		setTitle(Messages.getString("CreationTesteur.CREATE_TESTER_TITLE")); //$NON-NLS-1$
188 
189 		// Configurations des Panels
190 		this.panelCenter.add(this.creationPanel, BorderLayout.CENTER);
191 		this.panelCenter.add(this.boutonPanel, BorderLayout.SOUTH);
192 		this.panelAll.add(this.panelNorth, BorderLayout.NORTH);
193 		this.panelAll.add(this.panelWest, BorderLayout.WEST);
194 		this.panelAll.add(this.panelEast, BorderLayout.EAST);
195 		this.panelAll.add(this.panelSouth, BorderLayout.SOUTH);
196 		this.panelAll.add(this.panelCenter, BorderLayout.CENTER);
197 		getContentPane().add(this.panelAll);
198 		//this.setLocationRelativeTo(null);
199 		pack();
200 
201 	}
202 
203 	// Methodes utilises par les bouton OK et Annul
204 
205 	// Lorsque l'on appui sur le bouton annuler , la frame se ferme !
206 	void annulerBoutonMouseReleased(java.awt.event.MouseEvent evt) {
207 		this.dispose();
208 
209 	}
210 
211 	/**
212 	 * Lorsque l'on appui sur le bouton ok, la frame se ferme en enregistrant
213 	 * les nouveaux parametres Des vrification sont faites pour valider
214 	 * l'action : vrification que les cases ne sont pas vide vrification que
215 	 * le testeur n'existe pas dj
216 	 * 
217 	 */
218 	void okBoutonMouseReleased(MouseEvent evt) {
219 		Utilisateur lUtilisateur;
220 		String nomUtilisateur;
221 		String prenomUtilisateur;
222 		String motDePasseSaisie;
223 
224 		nomUtilisateur = this.createNomTextField.getText();
225 		prenomUtilisateur = this.createPrenomTextField.getText();
226 		motDePasseSaisie = new String(this.createPasswdField.getPassword());
227 		lUtilisateur = new Utilisateur(nomUtilisateur, prenomUtilisateur,
228 				motDePasseSaisie, " ", " "); //$NON-NLS-1$ //$NON-NLS-2$
229 
230 		if (nomUtilisateur.equals("") || prenomUtilisateur.equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
231 			JOptionPane
232 					.showMessageDialog(
233 							null,
234 							Messages
235 									.getString("CreationTesteur.ENTER_ALL_PARAMETERS"), Messages.getString("CreationTesteur.ERROR"), //$NON-NLS-1$ //$NON-NLS-2$
236 							JOptionPane.ERROR_MESSAGE);
237 
238 		} else {
239 			if (motDePasseSaisie.equals("")) { //$NON-NLS-1$
240 				JOptionPane
241 						.showMessageDialog(
242 								null,
243 								Messages
244 										.getString("CreationTesteur.ENTER_PASSWORD"), //$NON-NLS-1$
245 								Messages.getString("CreationTesteur.ERROR"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$
246 
247 			} else {
248 				if (checkPasswords() == false) {
249 					JOptionPane
250 							.showMessageDialog(
251 									null,
252 									Messages
253 											.getString("CreationTesteur.ENTER_CONFIRM_PASSWORD"), Messages.getString("CreationTesteur.ERROR"), //$NON-NLS-1$ //$NON-NLS-2$
254 									JOptionPane.ERROR_MESSAGE);
255 				} else {
256 					if (checkUserExist() == true) {
257 						JOptionPane
258 								.showMessageDialog(
259 										null,
260 										Messages
261 												.getString("CreationTesteur.SAME_USER"), Messages.getString("CreationTesteur.ERROR"), //$NON-NLS-1$ //$NON-NLS-2$
262 										JOptionPane.ERROR_MESSAGE);
263 					} else {
264 
265 						JOptionPane
266 								.showMessageDialog(
267 										null,
268 										Messages
269 												.getString("CreationTesteur.OK_ADD_USER"), Messages.getString("CreationTesteur.ADD"), //$NON-NLS-1$ //$NON-NLS-2$
270 										JOptionPane.INFORMATION_MESSAGE);
271 						ListUtilisateurs.add(lUtilisateur);
272 
273 						this.myConfig.setFields(this.createNomTextField
274 								.getText(), this.createPrenomTextField
275 								.getText());
276 						dispose();
277 
278 					}
279 				}
280 			}
281 		}
282 	}
283 
284 	/**
285 	 * Methode qui verifie que le mot de passe saisie est le meme dans la zone
286 	 * de saisie de confirmation
287 	 * 
288 	 * @return ret //retourn true si les deux mots de passes sont correctes
289 	 */
290 	private boolean checkPasswords() {
291 
292 		boolean ret = false;
293 		String motDePasse = new String(this.createPasswdField.getPassword());
294 		String confirmeMotDePasseSaisie = new String(
295 				this.confirmeMotDePasseSaisieText.getPassword());
296 
297 		if (confirmeMotDePasseSaisie.equals(motDePasse)) {
298 			ret = true;
299 		} else {
300 			ret = false;
301 		}
302 		return ret;
303 	}
304 
305 	/**
306 	 * Methode qui permet de verifier que le testeur n'existe pas dja
307 	 * 
308 	 * return ret // retourne vrai si le testeur existe deja
309 	 */
310 	private boolean checkUserExist() {
311 		boolean ret;
312 		Utilisateur lUtilisateur;
313 		String nomUtilisateur;
314 		String prenomUtilisateur;
315 		String motDePasseSaisie;
316 
317 		nomUtilisateur = this.createNomTextField.getText();
318 		prenomUtilisateur = this.createPrenomTextField.getText();
319 		motDePasseSaisie = new String(this.createPasswdField.getPassword());
320 		lUtilisateur = new Utilisateur(nomUtilisateur, prenomUtilisateur,
321 				motDePasseSaisie, " ", " "); //$NON-NLS-1$ //$NON-NLS-2$
322 
323 		ListUtilisateurs.readUtilisateurInfoFromDisk();
324 
325 		// verification de l'existence du testeur
326 		ret = ListUtilisateurs.isIn(lUtilisateur);
327 
328 		return ret;
329 
330 	}
331 
332 }