package gui.gestionCompteUser;
import static gui.MotsCleProjet.CMD_CANCEL;
import static gui.MotsCleProjet.CMD_OK;
import static gui.MotsCleProjet.IsNumString;
import static gui.util.TaxiGuiUtil.FabricationButton;
import gui.*;
import gui.commun.etat.EtatAffichable;
import gui.util.ErreurDeValidation;
import gui.util.TaxiGuiUtil;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.*;
import modele.commun.Etat;
import modele.compteUser.CompteUser;
import dao.GestionnaireDeStockage;
import dao.IDao;
/**
* class CompteUserEditionFenetre
* cr�e un interface utilisateur pour ajouter
* ou modifier un enregistrement
* @author Kasandra
*
*/
class CompteUserEditionFenetre implements ActionListener {
private JDialog aDialog;// R�f�rence � la boite de dialogue.
private JTextControlKey textNom;
private JTextControlKey textPrenom;
private JTextControlKey textLogin;
private JPasswordField textPwd;
private JTextControlKey textMail;
private JLabel titres;
private JComboBox cmbEtats;
private JPanel titre;
private CompteUser compteUser = new CompteUser();
protected boolean okFlag;
public boolean OK_Button() {
return okFlag;
}
/**
* Constructeur
*/
public CompteUserEditionFenetre() {
aDialog = new JDialog(MainWindow.cadrePrincipal(), " Compte utilisateur...", true);
textNom = new JTextControlKey("",30,IsNumString);
textPrenom = new JTextControlKey("",30,IsNumString);
textMail = new JTextControlKey("",30,IsNumString);
textLogin = new JTextControlKey("",30,IsNumString);
textPwd = new JPasswordField();
cmbEtats = new JComboBox(getListeEtats());
okFlag = false;
}
/**
* initialise
* @param compteUser
*/
void initializeDonnee(CompteUser compteUser) {
this.compteUser = compteUser;
textNom.ChampsTxt.setText(compteUser.getNom());
textPrenom.ChampsTxt.setText(compteUser.getPrenom());
textMail.ChampsTxt.setText(compteUser.getMail());
textLogin.ChampsTxt.setText(compteUser.getLogin());
textPwd.setText(compteUser.getPassWord());
selectionneEtat(compteUser.getEtat());
}
/**
* Selectionne Etat
* @param etat
*/
private void selectionneEtat(Etat etat) {
int nbEtats = cmbEtats.getItemCount();
for (int i = 0; i<nbEtats; i++) {
if (((EtatAffichable)cmbEtats.getItemAt(i)).getEtat().equals(etat)) {
cmbEtats.setSelectedIndex(i);
break;
}
}
}
/**
* @return compte user
*/
public CompteUser renvoiCompteUser() {
return compteUser;
}
/**
* Cr�e panel login
* @return
*/
private JPanel creeLoginPane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" Login : *"));
parameterPane.add(textLogin.ChampsTxt);
return parameterPane;
}
/**
*
* @return Panel mail
*/
private JPanel creeMailPane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" E-Mail : * "));
parameterPane.add(textMail.ChampsTxt);
return parameterPane;
}
/**
*
* @return panel passe word
*/
private JPanel creePwdPane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" Mot de passe : *"));
parameterPane.add(textPwd);
return parameterPane;
}
/**
*
* @return panel pr�nom
*/
private JPanel creePrenomPane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" Pr�nom : *"));
parameterPane.add(textPrenom.ChampsTxt);
return parameterPane;
}
/**
*
* @return Panel nom
*/
private JPanel creeNomPane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" Nom : *"));
parameterPane.add(textNom.ChampsTxt);
return parameterPane;
}
/**
*
* @return Panel etat compte
*/
private JPanel creeEtatCptePane()
{
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(Color.white);
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(600, 25));
parameterPane.add(new JLabel(" Etat compte : *"));
parameterPane.add(cmbEtats);
return parameterPane;
}
/**
*
* @return titre formulaire
*/
private JPanel getTitre() {
if (titre == null) {
GridBagConstraints gridBagTitre = new GridBagConstraints();
gridBagTitre.gridx = 0;
gridBagTitre.gridy = 0;
titres = new JLabel();
titres.setText(" Fiche compte utilisateur");
titres.setFont(new java.awt.Font("Comic Sans MS", 1, 20));
titres.setForeground(new Color(255, 255, 255));
titre = new JPanel();
titre.setLayout(new GridBagLayout());
titre.setPreferredSize(new Dimension(50, 45));
titre.setBackground(new Color(66, 79, 120));
titre.add(titres, gridBagTitre);
}
return titre;
}
/**
* Affiche la fen�tre et ses composants
*/
public void affiche() throws PrjException {
try {
JToolBar toolBar = new JToolBar("Outils...");
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new Color(170, 207, 249));
parameterPane.setLayout(new FlowLayout());
parameterPane.setPreferredSize(new Dimension(700, 300));
//parameterPane.setBorder(BorderFactory.createTitledBorder(" "));
parameterPane.add(creeNomPane());
parameterPane.add(creePrenomPane());
parameterPane.add(creeMailPane());
parameterPane.add(creeLoginPane());
parameterPane.add(creePwdPane());
parameterPane.add(creeEtatCptePane());
aDialog.getContentPane().add(getTitre(), BorderLayout.NORTH);
aDialog.getContentPane().add(parameterPane, BorderLayout.CENTER);
aDialog.getContentPane().add(toolBar, BorderLayout.PAGE_END);
toolBar.setBackground(new Color(66, 79, 120));
toolBar.add(FabricationButton("OK", CMD_OK, "OK...", this));
toolBar.add(FabricationButton("ANNULER", CMD_CANCEL, "Annuler...", this));
Point p = MainWindow.cadrePrincipal().getLocation();
aDialog.setLocation((p.x + 450), (p.y + 200));
aDialog.pack();
aDialog.setVisible(true);
} catch (NullPointerException exc) {
throw new PrjException(exc.getMessage());
}
}
/**
* Gestionnaire des �v�nements
*/
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
try {
// Restaure la couleur de fonds par d�faut.
for (JComponent composant : Arrays.asList(textNom.ChampsTxt, textPrenom.ChampsTxt,textMail.ChampsTxt, textLogin.ChampsTxt, textPwd)) {
composant.setBackground(Color.WHITE);
composant.setToolTipText("");
}
String cmd = e.getActionCommand();
if (CMD_OK.equals(cmd)) {
Vector<ErreurDeValidation> erreursDeValidation = new Vector<ErreurDeValidation>();
if( textNom.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textNom.ChampsTxt, "le champ 'nom' est obligatoire."));
}
if( textPrenom.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textPrenom.ChampsTxt, "le champ 'Pr�nnom' est obligatoire."));
}
if (textMail.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textMail.ChampsTxt, "le champ 'mail' est obligatoire."));
}
if( textLogin.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textLogin.ChampsTxt, "le champ 'login' est obligatoire."));
}
if( textPwd.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textPwd, "le champ 'marque voiture' est obligatoire."));
}
if (!erreursDeValidation.isEmpty()) {
StringBuffer erreurAffichee = new StringBuffer();
for (ErreurDeValidation erreurDeValidation : erreursDeValidation) {
erreurAffichee.append(erreurDeValidation.getMessageErreur());
erreurAffichee.append("\n");
erreurDeValidation.getComposant().setBackground(Color.RED);
erreurDeValidation.getComposant().setToolTipText(erreurDeValidation.getMessageErreur());
}
TaxiGuiUtil.MessageBox(MainWindow.desktop(), erreurAffichee.toString(), "ERREUR");
return;
}
else{
compteUser.setNom(textNom.ChampsTxt.getText());
compteUser.setPrenom(textPrenom.ChampsTxt.getText());
compteUser.setMail(textMail.ChampsTxt.getText());
compteUser.setLogin(textLogin.ChampsTxt.getText());
char[] password = textPwd.getPassword();
compteUser.setPassWord(new String(password));
for (int i=0; i < password.length; i++) {
password[i] = 0;
}
compteUser.setEtat(getEtatSelectionne());
okFlag = true;
aDialog.setVisible(false);
}
}else if (CMD_CANCEL.equals(cmd)) {
okFlag = false;
aDialog.setVisible(false);
}
}catch(Exception ex) {
TaxiGuiUtil.MessageBox(MainWindow.desktop(), "Erreur syst�me : " + ex.getMessage(), "Erreur syst�me");
}
}
/**
*
* @return Etat compte selectionn�
*/
private Etat getEtatSelectionne() {
return ((EtatAffichable)cmbEtats.getSelectedItem()).getEtat();
}
/**
*
* @return Vector<EtatAffichable>
*/
private Vector<EtatAffichable> getListeEtats() {
Vector<EtatAffichable> etatsAffichables = new Vector<EtatAffichable>();
ArrayList<Etat> etats = getEtatDao().findAll();
for (Etat etat : etats) {
etatsAffichables.add(new EtatAffichable(etat));
}
return etatsAffichables;
}
/**
* retourne l'instance de la classe MysqlEtatDao pour pouvoir
* utiliser les m�thodes de l'interface IDao
* @return IDao<Etat>
*/
private IDao<Etat> getEtatDao() {
try {
return GestionnaireDeStockage.getInstance().getEtatDao();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}