FIProjetIHM2022/src/Test/ProfView.java
pro.boooooo 2b66bcdf4c $
2022-11-15 18:57:04 +01:00

290 lines
9.5 KiB
Java

package Test;
import API.*;
import MNP.*;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.ArrayList;
public class ProfView extends BFrame implements ActionListener {
private final ArrayList<Etudiant> e;
private final ArrayList<Groupe> g;
private JComboBox groupeOption;
private JTextField text;
private BDatabase db;
public ProfView(ArrayList<Etudiant> e, ArrayList<Groupe> g, BDatabase db) {
super(
"Vue professeur",
1,
1,
500,
500,
3
);
this.e = e;
this.g = g;
this.db = db;
Display();
}
public void Display() {
BLayout settings = new BLayout();
settings.setPositionX(0);
settings.setPositionY(6);
settings.setPositionY(0);
JButton studList = new JButton("Voir la liste des etudiants");
studList.setActionCommand("fi::GetStudList");
studList.addActionListener(this);
this.add(studList, settings);
settings.setPositionY(1);
this.add(new JLabel(" "), settings);
settings.setPositionY(2);
JLabel gs = new JLabel("Afficher les etudiants se trouvant dans le groupe :");
this.add(gs, settings);
settings.setPositionY(3);
settings.setPadding(new Insets(0, 0, 0, 50));
String[] groupeList = new String[this.g.size()];
for(int i = 0; i <= this.g.size()-1; i++) {
groupeList[i] = this.g.get(i).getName();
}
this.groupeOption = new JComboBox(groupeList);
this.groupeOption.setPreferredSize(new Dimension(110, 30));
this.add(groupeOption, settings);
settings.setPositionY(3);
settings.setPadding(new Insets(0, 0, 0, 0));
settings.setAnchor(GridBagConstraints.EAST);
JButton confirm = new JButton("Rechercher");
confirm.setActionCommand("fi::GetListFiltered");
confirm.addActionListener(this);
this.add(confirm, settings);
settings.setAnchor(GridBagConstraints.CENTER);
settings.setPositionY(4);
this.add(new JLabel(" "), settings);
settings.setPositionY(5);
JLabel pf = new JLabel("Rechercher un etudiant : ");
this.add(pf, settings);
settings.setPositionY(6);
settings.setPadding(new Insets(0, 0, 0, 50));
this.text = new JTextField();
this.text.setPreferredSize(new Dimension(110, 30));
text.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (text.getText().length() >= 3 ) // limit textfield to 3 characters
e.consume();
}
});
this.add(this.text, settings);
settings.setPositionY(6);
settings.setPadding(new Insets(0, 0, 0, 0));
settings.setAnchor(GridBagConstraints.EAST);
JButton searchTLetters = new JButton("Rechercher");
searchTLetters.addActionListener(this);
searchTLetters.setActionCommand("fi::SearchStudentPer3Letters");
this.add(searchTLetters, settings);
this.openBFrame();
this.refreshBFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (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;
}
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();
} else if(command == "fi::GetListFiltered") {
BFrame frame = new BFrame(
"Liste des eleves du " + this.groupeOption.getSelectedItem(),
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() == this.groupeOption.getSelectedIndex()) {
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];
}
JTable liste = new JTable(data_final, 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();
} else if(command == "fi::SearchStudentPer3Letters") {
if(this.text.getText().length() < 3 || this.text.getText() == null) {
JOptionPane.showMessageDialog(
this,
"Veuillez ecrire au moins 3 lettres",
"Erreur.",
JOptionPane.ERROR_MESSAGE
);
} else {
char[] beg = this.text.getText().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,
"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))
};
}
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();
}
}
}
}
}