analyseWriter/src/fenetres/CustomInputDialogListeEtudi...

270 lines
11 KiB
Java

package fenetres;
import java.awt.Color;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
import MEPTL.commandes;
import MEPTL.meptl;
import cXML.node;
/**
* Class permettant de générer une<br>
* boite de dialogue pour charger une liste d'étudiants<br>
* dans le node commandes.NodeCSV.<br><br>
* Cette méthode fait appel à la méthode,<br>
* meptl.chargementFichierCSV(nameListStudent, textFieldYear.getText())<br>
* <br>
* @author pabr6
*
*/
public class CustomInputDialogListeEtudiant {
/**
* Class permettant de générer une<br>
* boite de dialogue pour charger une liste d'étudiants<br>
* dans le node commandes.NodeCSV.<br><br>
* Cette méthode fait appel à la méthode,<br>
* meptl.chargementFichierCSV(nameListStudent, textFieldYear.getText())<br>
* <br>
* @return
* 1- Le nom de la liste des étudiants (la liste est chargé).<br>
* 2- Code_Annule (ce qui signifie que la liste n'a pas été chargé).<br>
*/
public static String showCustominputDialog() {
String separateur ="";
String encodage ="";
String email ="inconnu";
String name = "inconnu";
String firstname = "inconnu";
String id = "inconnu";
String nameListStudent = "";
if(commandes.fichierAnalyseValide) {
if(commandes.fourniCSV) {
node CSV = commandes.sujet.retourneFirstEnfantsByName("csv");
separateur = CSV.getAttributs().get("separator");
encodage = CSV.getAttributs().get("encoding");
node importCSV = CSV.retourneFirstEnfantsByName("import_moodle");
if(CSV.isHasAttributs()) {
if(importCSV.getAttributs().get("email")!=null) {
email = importCSV.getAttributs().get("email");
}
if(importCSV.getAttributs().get("name")!=null) {
name = importCSV.getAttributs().get("name");
}
if(importCSV.getAttributs().get("firstname")!=null) {
firstname = importCSV.getAttributs().get("firstname");
}
if(importCSV.getAttributs().get("id")!=null) {
id = importCSV.getAttributs().get("id");
}
}
}
if(commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV")!=null) {
if(commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV").getAttributs().get("nameListStudent")!=null) {
nameListStudent = commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV").getAttributs().get("nameListStudent");
}
}
}
JLabel lblTitre = new JLabel("<html><h2>Ajouter une liste d'étudiant à l'évaluation<h2></html>");
lblTitre.setForeground(new Color(50,50,200));
JLabel lblExpliaction = new JLabel("<html><p>Le fichier au format CSV pour importer une liste d'étudiant doit avoir<br>"
+ "les caractéristiques suivantes :</p><br>"
+ "<p>Séparateur : <b>" + separateur +"</b><br>"
+ "Encodage : <b>" + encodage + "</b><br>"
+ "Nom des étudiants, la colonne : <b>" + name + "</b><br>"
+ "Prénom des étudiants, la colonne : <b>" + firstname + "</b><br>"
+ "Numéro des étudiants, la colonne : <b>" + id +"</b><br>"
+ "Courriel des étudiants, la colonne : <b>" + email + "</b></p><br>"
+ "</html>");
lblExpliaction.setFont(new Font("Tahoma", Font.BOLD, 12));
lblExpliaction.setForeground(Color.blue);
JTextField textField = new JTextField(String.valueOf(nameListStudent));
textField.setFont(new Font("Tahoma", Font.BOLD, 14));
textField.setHorizontalAlignment(SwingConstants.LEFT);
ImageIcon icon = new ImageIcon(create.class.getResource("/resources/fichierCSV.png"));
JTextField textFieldYear = new JTextField("2024");
Object[] message = {
lblTitre,
lblExpliaction,
"<html><p><b>Le nom d'une liste d'étudiant doit être unique.</b><br>"
+ "Sinon, les étudiants seront ajoutés à la liste existante.</p><br>"
+ "<p>Quel doit être le nom de cette liste ?</p></html>",
textField,
"<html><p>Quel est l'année ?</p></html>",
textFieldYear
};
String[] options = {"Importer la liste", "Annuler"};
int optionSelected = JOptionPane.showOptionDialog(
null,
message,
"Importer une liste d'étudiant.",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
icon,
options,
options[0]);
if (optionSelected == 0) { // Bouton "Importer la liste" sélectionné
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choisir un fichier CSV");
chooser.setCurrentDirectory(new java.io.File(commandes.path));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Format CSV", "csv");
chooser.setFileFilter(filter);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile().getAbsolutePath());
commandes.nameCSV = chooser.getSelectedFile().getAbsolutePath();
meptl.chargementFichierCSV(nameListStudent, textFieldYear.getText());
if(commandes.nodeCSV.getNbrEnfants()>=1) commandes.fourniCSV=true;
return nameListStudent;
}
return "Code_Annule";
} else if (optionSelected == 1) { // Bouton "Désactivé" sélectionné
return "Code_Annule";
} else { // Si la boîte de dialogue est annulée ou fermée
return "Code_Annule";
}
}
/**
* Class permettant de générer une<br>
* boite de dialogue pour écrire ou ajouter une liste d'étudiants<br>
* dans le node csv pour l'écriture.<br><br>
* Cette méthode fait appel à la méthode,<br>
* meptl.ecritureListeEtudiantsBaseFichierCSV(chemin vers fichier CSV, nameListStudent , year)<br>
* <br>
* @return
* 1- true lorsque le fichier est écrit.<br>
* 2- false lorsque le fichier n'a pas été écrit.<br>
*/
public static boolean showCustomDialogInputStudentBase() {
String separateur ="";
String encodage ="";
String email ="inconnu";
String name = "inconnu";
String firstname = "inconnu";
String id = "inconnu";
String nameListStudent = "";
if(commandes.fichierAnalyseValide) {
if(commandes.fourniCSV) {
node CSV = commandes.sujet.retourneFirstEnfantsByName("csv");
separateur = CSV.getAttributs().get("separator");
encodage = CSV.getAttributs().get("encoding");
node importCSV = CSV.retourneFirstEnfantsByName("import_moodle");
if(CSV.isHasAttributs()) {
if(importCSV.getAttributs().get("email")!=null) {
email = importCSV.getAttributs().get("email");
}
if(importCSV.getAttributs().get("name")!=null) {
name = importCSV.getAttributs().get("name");
}
if(importCSV.getAttributs().get("firstname")!=null) {
firstname = importCSV.getAttributs().get("firstname");
}
if(importCSV.getAttributs().get("id")!=null) {
id = importCSV.getAttributs().get("id");
}
}
}
if(commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV")!=null) {
if(commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV").getAttributs().get("nameListStudent")!=null) {
nameListStudent = commandes.evaluationChargeEnMemoire.retourneFirstEnfantsByName("fileCSV").getAttributs().get("nameListStudent");
}
}
}
JLabel lblTitre = new JLabel("<html><h2>Ajouter une liste d'étudiant à l'évaluation<h2></html>");
lblTitre.setForeground(new Color(50,50,200));
JLabel lblExpliaction = new JLabel("<html><p>Le fichier au format CSV pour importer une liste d'étudiant doit avoir<br>"
+ "les caractéristiques suivantes :</p><br>"
+ "<p>Séparateur : <b>" + separateur +"</b><br>"
+ "Encodage : <b>" + encodage + "</b><br>"
+ "Nom des étudiants, la colonne : <b>" + name + "</b><br>"
+ "Prénom des étudiants, la colonne : <b>" + firstname + "</b><br>"
+ "Numéro des étudiants, la colonne : <b>" + id +"</b><br>"
+ "Courriel des étudiants, la colonne : <b>" + email + "</b></p><br>"
+ "</html>");
lblExpliaction.setFont(new Font("Tahoma", Font.BOLD, 12));
lblExpliaction.setForeground(Color.blue);
JTextField textField = new JTextField(String.valueOf(nameListStudent));
textField.setFont(new Font("Tahoma", Font.BOLD, 14));
textField.setHorizontalAlignment(SwingConstants.LEFT);
ImageIcon icon = new ImageIcon(create.class.getResource("/resources/fichierCSV.png"));
JTextField textFieldYear = new JTextField("2024");
Object[] message = {
lblTitre,
lblExpliaction,
"<html><p><b>Le nom d'une liste d'étudiant doit être unique.</b><br>"
+ "Sinon, les étudiants seront ajoutés à la liste existante du même nom.</p><br>"
+ "<p>Quel doit être le nom de cette liste ?</p></html>",
textField,
"<html><p>Quel est l'année ?</p></html>",
textFieldYear
};
String[] options = {"Importer la liste", "Annuler"};
int optionSelected = JOptionPane.showOptionDialog(
null,
message,
"Importer une liste d'étudiant.",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
icon,
options,
options[0]);
if (optionSelected == 0) { // Bouton "Importer la liste" sélectionné
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choisir un fichier CSV");
chooser.setCurrentDirectory(new java.io.File(commandes.path));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Format CSV", "csv");
chooser.setFileFilter(filter);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
commandes.nameCSV = chooser.getSelectedFile().getAbsolutePath();
nameListStudent = textField.getText().toLowerCase().trim();
String year = textFieldYear.getText().toLowerCase().trim();
return meptl.ecritureListeEtudiantsBaseFichierCSV(chooser.getSelectedFile().getAbsolutePath(), nameListStudent , year);
}
return false;
} else if (optionSelected == 1) { // Bouton "Désactivé" sélectionné
return false;
} else { // Si la boîte de dialogue est annulée ou fermée
return false;
}
}
}