package gui.charge.chargevoiture;
import static gui.MotsCleProjet.CMD_CANCEL;
import static gui.MotsCleProjet.CMD_OK;
import static gui.MotsCleProjet.IsDecimal;
import static gui.MotsCleProjet.IsNumString;
import static gui.util.TaxiGuiUtil.FabricationButton;
import gui.DateCalendar;
import gui.JTextControlKey;
import gui.MainWindow;
import gui.charge.typecharge.TypeChargeAffichable;
import gui.commun.etat.EtatAffichable;
import gui.util.ErreurDeValidation;
import gui.util.TaxiGuiUtil;
import gui.voiture.gestionvoiture.VoitureAffichable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import modele.charge.ChargeVoiture;
import modele.charge.TypeCharge;
import modele.commun.Etat;
import modele.voiture.Voiture;
import dao.GestionnaireDeStockage;
import dao.IDao;
import dao.IVoitureDao;
/**
* class ChargeVoitureEditionFenetre
* Cr�e la fen�tre qui permet d'ajouter ou de modifier un enregistrement
* @author Kasandra
*
*/
class ChargeVoitureEditionFenetre implements ActionListener{
private JDialog aDialog;// R�f�rence � la boite de dialogue.
private JTextControlKey textLibelle;
private JTextControlKey textReference;
private JTextControlKey textMontIndicatif;
private JLabel labRefChrg, labLibelle, labIdTypeChrg, labMontIndicatif, labVoiture, titres, textdateEmissionCharge, textDateEcheanceChrg, labDtEcheanceFacture, labDtEmissionFacture;
private JComboBox cmbTypeCharge, cmbVoiture, cmbEtatCharge;
private JPanel titre;
private static Font titreFont = new Font("Tahoma", 1, 12);
private ChargeVoiture chargeVoiture = new ChargeVoiture();
private boolean okFlag = false;
/**
* Constructeur de la classe : ChargeVoitureEditionFenetre
*
*/
public ChargeVoitureEditionFenetre() {
aDialog = new JDialog(MainWindow.cadrePrincipal(), " Charge Voiture...", true);
textReference = new JTextControlKey("", 20, IsNumString);
textLibelle = new JTextControlKey("", 30, IsNumString);
Calendar calendarEmissionChargeMinimum = Calendar.getInstance();
calendarEmissionChargeMinimum.add(Calendar.DAY_OF_MONTH, -15);
Date dateEmissionChargeMinimum = calendarEmissionChargeMinimum.getTime();
Calendar calendarEmissionChargeMaximum = Calendar.getInstance();
calendarEmissionChargeMaximum.add(Calendar.DAY_OF_MONTH, 1);
Date dateEmissionChargeMaximum = calendarEmissionChargeMaximum.getTime();
textdateEmissionCharge = TaxiGuiUtil.creeDateLabel(gui.MotsCleProjet.DATE_FORMAT, DateCalendar.Mode.DATE_SEULEMENT, dateEmissionChargeMinimum, dateEmissionChargeMaximum);
Calendar calendarEcheanceChrgMinimum = Calendar.getInstance();
calendarEcheanceChrgMinimum.add(Calendar.DAY_OF_MONTH, -14);
Date dateEcheanceChrgMinimum = calendarEcheanceChrgMinimum.getTime();
Calendar calendarEcheanceChrgMaximum = Calendar.getInstance();
calendarEcheanceChrgMaximum.add(Calendar.DAY_OF_MONTH, 15);
Date dateEcheanceChrgMaximum = calendarEcheanceChrgMaximum.getTime();
textDateEcheanceChrg = TaxiGuiUtil.creeDateLabel(gui.MotsCleProjet.DATE_FORMAT, DateCalendar.Mode.DATE_SEULEMENT, dateEcheanceChrgMinimum, dateEcheanceChrgMaximum);
textMontIndicatif = new JTextControlKey("", 10, IsDecimal);
cmbTypeCharge = new JComboBox(getListeTypeCharge());
cmbVoiture = new JComboBox(getListeVoiture());
cmbEtatCharge = new JComboBox(getListeEtatCharge());
okFlag = false;
}
/**
*
* @return okFlag
*/
public boolean OK_Button() {
return okFlag;
}
/**
* initialise les donn�es de la chargeVoiture
* @param chargeVoiture
*/
void initializeDonnee(ChargeVoiture chargeVoiture) {
this.chargeVoiture = chargeVoiture;
textReference.ChampsTxt.setText(chargeVoiture.getReference());
textLibelle.ChampsTxt.setText(chargeVoiture.getLibelle());
textdateEmissionCharge.setText(new SimpleDateFormat(gui.MotsCleProjet.DATE_FORMAT).format(chargeVoiture.getDateEmmissionCharge()));
textDateEcheanceChrg.setText(new SimpleDateFormat(gui.MotsCleProjet.DATE_FORMAT).format(chargeVoiture.getDateEcheanceCharge()));
textMontIndicatif.ChampsTxt.setText(chargeVoiture.getMontantIndicatif()+"");
//textNbreUnite.ChampsTxt.setText(chargeVoiture.getNombreUnite()+"");
selectionneTypeCharge(chargeVoiture.getTypeCharge());
selectionneVoiture(chargeVoiture.getVoiture());
selectionneEtat(chargeVoiture.getEtat());
}
/**
* Selectionne type charge voiture
* @param typeCharge
*/
private void selectionneTypeCharge(TypeCharge typeCharge) {
int nbTypeCharge = cmbTypeCharge.getItemCount();
for (int i = 0; i<nbTypeCharge; i++) {
if (((TypeChargeAffichable)cmbTypeCharge.getItemAt(i)).getTypeCharge().getId().equals(typeCharge.getId())) {
cmbTypeCharge.setSelectedIndex(i);
break;
}
}
}
/**
* Selectionne voiture
* @param voiture
*/
private void selectionneVoiture(Voiture voiture) {
int nbVoiture = cmbVoiture.getItemCount();
for (int i = 0; i<nbVoiture; i++) {
if (((VoitureAffichable)cmbVoiture.getItemAt(i)).getVoiture().getId().equals(voiture.getId())) {
cmbVoiture.setSelectedIndex(i);
break;
}
}
}
/**
* Selectionne Etat
* @param etat
*/
private void selectionneEtat(Etat etat) {
int nbEtats = cmbEtatCharge.getItemCount();
for (int i = 0; i<nbEtats; i++) {
if (((EtatAffichable)cmbEtatCharge.getItemAt(i)).getEtat().getCdeEtat().equals(etat.getCdeEtat())) {
cmbEtatCharge.setSelectedIndex(i);
break;
}
}
}
/**
* Retourne la charge voiture
* @return chargeVoiture
*/
ChargeVoiture renvoiChargeVoiture() {
return chargeVoiture;
}
/**
* Cr�e le panel r�f�rence facture
* @return parameterPane : panel r�f�rence facture
*/
private JPanel creeReferencePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labRefChrg = new JLabel(" R�f�rence facture : *");
labRefChrg.setFont(titreFont);
labRefChrg.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labRefChrg);
parameterPane.add(textReference.ChampsTxt);
return parameterPane;
}
/**
* Cr�e le panel libell� facture
* @return parameterPane: panel libell� facture
*/
private JPanel creeLibelleChargePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labLibelle = new JLabel(" Libell� : *");
labLibelle.setFont(titreFont);
labLibelle.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labLibelle);
parameterPane.add(textLibelle.ChampsTxt);
return parameterPane;
}
/**
* Cr�e le panel date emission facture
* @return parameterPane : panel date �mission
*/
private JPanel creeDateEmissionFacturePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labDtEmissionFacture = new JLabel(" Date �mission facture : *");
labDtEmissionFacture.setFont(titreFont);
labDtEmissionFacture.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labDtEmissionFacture);
parameterPane.add(textdateEmissionCharge);
return parameterPane;
}
/**
* Cr�e panel date �ch�ance facture
* @return parameterPane: panel date �ch�ance fecture
*/
private JPanel creeDateEcheanceFacturePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labDtEcheanceFacture = new JLabel(" Date ech�ance facture : *");
labDtEcheanceFacture.setFont(titreFont);
labDtEcheanceFacture.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labDtEcheanceFacture);
parameterPane.add(textDateEcheanceChrg);
return parameterPane;
}
/**
* Cr�e le panel montant indicatif facture
*
* @return parameterPane : panel montant de base de la fracture
*/
private JPanel creeMontantIndicatifFacturePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labMontIndicatif = new JLabel(" Montant de base facture : *");
labMontIndicatif.setFont(titreFont);
labMontIndicatif.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labMontIndicatif);
parameterPane.add(textMontIndicatif.ChampsTxt);
return parameterPane;
}
/**
* Cr�e le panel code type charge
*
* @return parameterPane : panel de type de charge
*
*/
private JPanel creeCodeTypeChargePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labIdTypeChrg = new JLabel(" Type de charges : *");
labIdTypeChrg.setFont(titreFont);
labIdTypeChrg.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labIdTypeChrg);
parameterPane.add(cmbTypeCharge);
return parameterPane;
}
/**
* Cr�e le panel code �tat charge
*
* @return parameterPane : panel etat charge
*/
private JPanel creeCodeEtatChargePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
JLabel labEtat = new JLabel(" Code �tat :*");
labEtat.setFont(titreFont);
labEtat.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labEtat);
parameterPane.add(cmbEtatCharge);
return parameterPane;
}
/**
* retourne le panel voiture
* @return parameterPane : panel voiture
*/
private JPanel codeVoiturePane() {
JPanel parameterPane = new JPanel(false);
//parameterPane.setBackground(new java.awt.Color(170, 207, 249));
parameterPane.setLayout(new GridLayout());
parameterPane.setPreferredSize(new Dimension(500, 25));
labVoiture = new JLabel(" Voiture : *");
labVoiture.setFont(titreFont);
labVoiture.setForeground(new java.awt.Color(66, 79, 120));
parameterPane.add(labVoiture);
parameterPane.add(cmbVoiture);
return parameterPane;
}
/**
* Retourne le titre de la fen�tre
* @return titre
*/
private JPanel getTitre() {
if (titre == null) {
GridBagConstraints gridBagTitre = new GridBagConstraints();
gridBagTitre.gridx = 0;
gridBagTitre.gridy = 0;
titres = new JLabel();
titres.setText(" Formulaire d�pense");
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;
}
/**
* D�finit la fen�tre et ses composants
*/
public void affiche() {
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(800, 300));
//parameterPane.setBorder(BorderFactory.createTitledBorder(" "));
parameterPane.add(creeReferencePane());
parameterPane.add(creeLibelleChargePane());
parameterPane.add(creeDateEmissionFacturePane());
parameterPane.add(creeDateEcheanceFacturePane());
parameterPane.add(creeMontantIndicatifFacturePane());
//parameterPane.add(creeNombreUniteChargePane());
parameterPane.add(creeCodeTypeChargePane());
parameterPane.add(codeVoiturePane());
parameterPane.add(creeCodeEtatChargePane());
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);
}
/**
* <p>Gestionnaire des �v�nements :</p>
* <p> Contr�le les erreurs de validations</p>
* <p> Rend le champ o� l'erreur de validation est d�tect� en rouge</p>
* <p> restaure la couleur de fond si l'erreur est corrig�e en blanc</p>
*/
public void actionPerformed(ActionEvent e) {
try {
// Restaure la couleur de fonds par d�faut.
for (JComponent composant : Arrays.<JComponent>asList(textReference.ChampsTxt, textLibelle.ChampsTxt,textdateEmissionCharge,
textDateEcheanceChrg,textMontIndicatif.ChampsTxt
)) {
composant.setBackground(Color.WHITE);
composant.setToolTipText("");
}
String cmd = e.getActionCommand();
if (CMD_OK.equals(cmd)) {
Vector<ErreurDeValidation> erreursDeValidation = new Vector<ErreurDeValidation>();
if (textReference.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textReference.ChampsTxt, "le champ 'r�f�rence charge' est obligatoire"));
}
if (textLibelle.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textLibelle.ChampsTxt, "le champ 'description charge' est obligatoire"));
}
Date dateEmission = null;
try {
dateEmission = new SimpleDateFormat(gui.MotsCleProjet.DATE_FORMAT).parse(textdateEmissionCharge.getText());
chargeVoiture.setDateEmmissionCharge(dateEmission);
} catch (ParseException e1) {
//TaxiGuiUtil.MessageBox(MainWindow.desktop(), "Le format de date est invalide : attendu " + gui.MotsCleProjet.DATE_FORMAT, "Erreur");
erreursDeValidation.add(new ErreurDeValidation(textdateEmissionCharge, "Le format de date est invalide : attendu "+ gui.MotsCleProjet.DATE_FORMAT));
}
Date dateEchea = null;
try {
dateEchea = new SimpleDateFormat(gui.MotsCleProjet.DATE_FORMAT).parse(textDateEcheanceChrg.getText());
chargeVoiture.setDateEcheanceCharge(dateEchea);
} catch (ParseException e1) {
//TaxiGuiUtil.MessageBox(MainWindow.desktop(), "Le format de date est invalide : attendu " + gui.MotsCleProjet.DATE_FORMAT, "Erreur");
erreursDeValidation.add(new ErreurDeValidation(textDateEcheanceChrg, "Le format de date est invalide : attendu "+ gui.MotsCleProjet.DATE_FORMAT));
}
if (dateEmission.after( dateEchea)){
erreursDeValidation.add(new ErreurDeValidation(textDateEcheanceChrg, "Date d'�ch�ance de la facture' doit �tre apr�s date de la facture"));
}
if (textMontIndicatif.ChampsTxt.getText().trim().equals("")) {
erreursDeValidation.add(new ErreurDeValidation(textMontIndicatif.ChampsTxt, "le champ 'montant indicatif facture' est obligatoire"));
}
try {
new BigDecimal(textMontIndicatif.ChampsTxt.getText());
} catch (NumberFormatException ex) {
erreursDeValidation.add(new ErreurDeValidation(textMontIndicatif.ChampsTxt, "le champ 'montant indicatif facture' n'est pas un nombre"));
}
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{
chargeVoiture.setReference(textReference.ChampsTxt.getText());
chargeVoiture.setLibelle(textLibelle.ChampsTxt.getText());
//textDateEcheanceChrg.setText(new SimpleDateFormat(gui.MotsCleProjet.DATE_FORMAT).format(chargeVoiture.getDateEcheanceCharge()));
chargeVoiture.setMontantIndicatif(new BigDecimal(textMontIndicatif.ChampsTxt.getText()));
chargeVoiture.setTypeCharge(getTypeChargeSelectionne());
chargeVoiture.setVoiture(getVoitureSelectionne());
chargeVoiture.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");
ex.printStackTrace();
}
}
/**
* Retourne type de charge s�lectionn�
* @return type charge s�lectionn�
*/
private TypeCharge getTypeChargeSelectionne() {
return ((TypeChargeAffichable)cmbTypeCharge.getSelectedItem()).getTypeCharge();
}
/**
* Retourne la voiture s�lectionn�e
* @return la voiture selectionn�e
*/
private Voiture getVoitureSelectionne() {
return ((VoitureAffichable)cmbVoiture.getSelectedItem()).getVoiture();
}
/**
* Retourne l'�tat s�lectionn�
* @return �tat de la voiture s�lectionn�e
*/
private Etat getEtatSelectionne() {
return ((EtatAffichable)cmbEtatCharge.getSelectedItem()).getEtat();
}
/**
* Retourne la liste de type de charge affichable
* @return TypeChargeAffichable
*/
private Vector<TypeChargeAffichable> getListeTypeCharge() {
Vector<TypeChargeAffichable> typeChargeAffichables = new Vector<TypeChargeAffichable>();
ArrayList<TypeCharge> typeCharge = getTypeChargeDao().findAll();
for (TypeCharge etypeCharge : typeCharge) {
typeChargeAffichables.add(new TypeChargeAffichable(etypeCharge));
}
return typeChargeAffichables;
}
/**
* retourne l'instance de la classe MysqlTypeChargeDao pour pouvoir
* utiliser les m�thodes de l'interface IDao
* @return TypeCharge Dao
*/
private IDao<TypeCharge> getTypeChargeDao() {
try {
return GestionnaireDeStockage.getInstance().getTypeChargeDao();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Retourne la liste des voitures affichables
* @return VoitureAffichable
*/
/*
private Vector<VoitureAffichable> getListeVoiture() {
Vector<VoitureAffichable> voitureAffichables = new Vector<VoitureAffichable>();
ArrayList<Voiture> voiture = getVoitureDao().findAll();
for (Voiture evoiture : voiture) {
voitureAffichables.add(new VoitureAffichable(evoiture));
}
return voitureAffichables;
}
*/
/**
* retourne l'instance de la classe MysqlVoitureDao pour pouvoir
* utiliser les m�thodes de l'interface IDao
* @return voiture Dao
*/
/*
private IDao<Voiture> getVoitureDao() {
try {
return GestionnaireDeStockage.getInstance().getVoitureDao();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
*/
/**
* retourne la liste des voitures
* @return la liste des voitures
*/
private Vector<VoitureAffichable> getListeVoiture() {
Vector<VoitureAffichable> voitureAffichables = new Vector<VoitureAffichable>();
ArrayList<Voiture> voitures = getVoitureDao().findAllActif();//.findAll();
for (Voiture evoiture : voitures) {
voitureAffichables.add(new VoitureAffichable(evoiture));
}
return voitureAffichables;
}
/**
* Cr�e l'instance de de la classe dao qui impl�mente les m�thodes de l'interface IDao<Voiture>
* @return ReferenceChauffeur
*
*/
private IVoitureDao getVoitureDao() {
try {
return GestionnaireDeStockage.getInstance().getVoitureDao();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Retourne la liste des �tats affichables
* @return EtatAffichable
*/
private Vector<EtatAffichable> getListeEtatCharge() {
Vector<EtatAffichable> etatAffichables = new Vector<EtatAffichable>();
ArrayList<Etat> etat = getEtatDao().findAll();
for (Etat etats : etat) {
etatAffichables.add(new EtatAffichable(etats));
}
return etatAffichables;
}
/**
* retourne l'instance de la classe MysqlEtatDao pour pouvoir
* utiliser les m�thodes de l'interface IDao
* @return Etat Dao
*/
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);
}
}
}