FIProjetIHM2022/src/Test/Controller.java

191 lines
6.1 KiB
Java
Raw Normal View History

2022-11-26 19:46:16 +01:00
package Test;
import API.Etudiant;
import API.Groupe;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Objects;
public class Controller implements ActionListener {
private final BDatabase db;
private final ProfView pv;
private final AdminView av;
private final ArrayList<Etudiant> e;
private final ArrayList<Groupe> g;
public Controller(ArrayList<Etudiant> e, ArrayList<Groupe> g) {
this.e = e;
this.g = g;
this.db = new BDatabase();
this.pv = new ProfView(e, g, this);
System.out.println("[+] Demarrage de la vue professeur -> " + pv);
this.av = new AdminView(e, g, this);
System.out.println("[+] Demarrage de la vue Administrateur -> " + av);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
String groupeOption = this.pv.getComboSelection();
int groupeIndex = this.pv.getComboSelectionIndex();
String text = this.pv.getSearchStud();
if (Objects.equals(command, "fi::GetStudList")) {
BFrame frame = new BFrame(
"Liste des eleves",
1,
1,
500,
500,
"GridLayout",
1,
1,
2
);
String[][] data = new String[this.e.size()][2];
String[] titre = {
"Nom",
"Prenom",
"Groupe"
};
for(int i = 0; i <= this.e.size()-1; i++) {
String[] info = {
this.e.get(i).getNom(),
this.e.get(i).getPrenom(),
String.valueOf(this.e.get(i).getGroupe())
};
data[i] = info;
}
Display(frame, data, titre);
}
else if(Objects.equals(command, "fi::GetListFiltered")) {
BFrame frame = new BFrame(
"Liste des eleves du " + groupeOption,
1,
1,
500,
500,
"GridLayout",
1,
1,
2
);
String[][] data = new String[this.e.size()][1];
String[] titre = {
"Nom",
"Prenom"
};
int i, j;
for(i = 0, j = 0; i <= this.e.size()-1; i++) {
if(this.e.get(i).getGroupe() == groupeIndex) {
String[] info = {
this.e.get(i).getNom(),
this.e.get(i).getPrenom()
};
data[j] = info;
j++;
}
}
String[][] data_final = new String[j][1];
for(int x = 0; x <= j-1; x++) {
data_final[x] = data[x];
}
Display(frame, data_final, titre);
}
else if(Objects.equals(command, "fi::SearchStudentPer3Letters")) {
if(text.length() < 3 || text == null) {
JOptionPane.showMessageDialog(
this.pv,
"Veuillez ecrire 3 lettres",
"Erreur.",
JOptionPane.ERROR_MESSAGE
);
} else {
char[] beg = text.toCharArray();
BFrame frame = new BFrame(
"Liste d'eleves d'ou les noms commence par " + beg[0] + beg[1] + beg[2],
1,
1,
500,
500,
"GridLayout",
1,
1,
2
);
ArrayList<String> filtreEleveNom = db.fetchAll("SELECT nom FROM fi_eleves WHERE nom LIKE '" + beg[0] + beg[1] + beg[2] + "%'");
ArrayList<String> filtreElevePrenom = db.fetchAll("SELECT prenom FROM fi_eleves WHERE nom LIKE '" + beg[0] + beg[1] + beg[2] + "%'");
ArrayList<String> filtreEleveGroupe = db.fetchAll("SELECT groupe FROM fi_eleves WHERE nom LIKE '" + beg[0] + beg[1] + beg[2] + "%'");
System.out.println(filtreEleveGroupe.size() + filtreElevePrenom.size() + filtreEleveGroupe.size());
if(filtreEleveGroupe.size() == 0 || filtreEleveNom.size() == 0 || filtreElevePrenom.size() == 0) {
JOptionPane.showMessageDialog(
this.pv,
"Eleve introuvable !",
"Erreur.",
JOptionPane.ERROR_MESSAGE
);
frame.closeBFrame();
} else {
String[] titre = {
"Nom",
"Prenom",
"Groupe"
};
String[][] data = new String[filtreEleveNom.size()][2];
for(int i = 0; i <= filtreEleveNom.size()-1; i++){
data[i] = new String[]{
filtreEleveNom.get(i),
filtreElevePrenom.get(i),
String.valueOf(filtreEleveGroupe.get(i))
};
}
Display(frame, data, titre);
}
}
}
}
private void Display(BFrame frame, String[][] data, String[] titre) {
JTable liste = new JTable(data, titre) {
public boolean editCellAt(int row, int column, java.util.EventObject e) {
return false;
}
};
liste.getTableHeader().setReorderingAllowed(false);
liste.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(liste);
frame.getContentPane().add(scroll);
frame.openBFrame();
}
}