Files
FIProjetIHM2022/src/ManageStudent.java

230 lines
7.8 KiB
Java
Raw Normal View History

2022-10-07 10:39:13 +02:00
import javax.swing.*;
2022-10-06 11:29:53 +02:00
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2022-10-07 10:39:13 +02:00
import java.sql.Array;
2022-10-06 11:29:53 +02:00
import java.util.ArrayList;
2022-10-07 10:39:13 +02:00
import java.util.Objects;
2022-10-06 11:29:53 +02:00
public class ManageStudent extends BFrame implements ActionListener {
2022-10-07 10:39:13 +02:00
protected BDatabase sharedObject;
protected BFrame tempFrame;
protected BLayout settings;
2022-10-06 11:29:53 +02:00
protected BInput inSearch;
protected BButton moveTo;
2022-10-07 10:39:13 +02:00
protected BButton btnSearch;
2022-10-06 11:29:53 +02:00
protected BButton addTo;
protected BButton seeReq;
2022-10-07 10:39:13 +02:00
protected JList<String> student;
protected JList<String> grupList;
2022-10-06 11:29:53 +02:00
protected String[] data;
2022-10-07 10:39:13 +02:00
protected int tmpForAddGrup;
protected int tmpForAddGrupWho;
2022-10-06 11:29:53 +02:00
public ManageStudent(BDatabase so) {
super("Manageur d'Eleve", 800, 400, JFrame.DISPOSE_ON_CLOSE);
this.settings = new BLayout();
this.sharedObject = so;
this.settings.setAnchor(GridBagConstraints.NORTHWEST);
this.inSearch = new BInput(150, 30);
this.settings.setPositionX(0);
this.settings.setPositionY(0);
this.add(inSearch, this.settings);
this.btnSearch = new BButton("Rechercher");
this.btnSearch.setActionCommand("search");
this.btnSearch.addActionListener(this);
2022-10-07 10:39:13 +02:00
this.settings.setPositionX(1);
this.settings.setPositionY(0);
2022-10-06 11:29:53 +02:00
this.add(btnSearch, this.settings);
this.student = refreshList("");
this.settings.setFill(GridBagConstraints.BOTH);
this.settings.setPositionX(0);
this.settings.setPositionY(1);
this.settings.setSizeX(1.0f);
this.settings.setSizeY(1.0f);
this.settings.setTakeCaseOnY(2);
this.settings.setTakeCaseOnX(2);
this.add(this.student, this.settings);
this.settings.setSizeX(1.0f);
this.settings.setSizeY(1.0f);
this.settings.setTakeCaseOnY(1);
this.settings.setTakeCaseOnX(1);
this.settings.setFill(GridBagConstraints.NONE);
this.settings.setAnchor(GridBagConstraints.CENTER);
2022-10-07 10:39:13 +02:00
this.moveTo = new BButton("Deplacer dans un groupe");
this.moveTo.setActionCommand("move");
this.moveTo.addActionListener(this);
2022-10-06 11:29:53 +02:00
this.settings.setPositionX(2);
this.settings.setPositionY(1);
this.add(moveTo, this.settings);
2022-10-07 10:39:13 +02:00
this.addTo = new BButton("Ajouter dans un groupe");
this.addTo.setActionCommand("add");
this.addTo.addActionListener(this);
2022-10-06 11:29:53 +02:00
this.settings.setPositionX(3);
this.settings.setPositionY(1);
this.add(addTo, this.settings);
this.seeReq = new BButton("Voir ses demandes");
this.settings.setPositionX(4);
this.settings.setPositionY(1);
this.add(seeReq, this.settings);
this.openBFrame();
this.refreshBFrame();
}
protected JList<String> refreshList(String student) {
2022-10-07 10:39:13 +02:00
ArrayList<String> tmp = new ArrayList<>();
ArrayList<String> tmp2 = new ArrayList<>();
ArrayList<String> tmp3 = new ArrayList<>();
2022-10-06 11:29:53 +02:00
2022-10-07 10:39:13 +02:00
tmp = this.sharedObject.fetchAll(
2022-10-06 11:29:53 +02:00
"SELECT nom FROM Membres WHERE nom LIKE '%" + student + "%'"
);
2022-10-07 10:39:13 +02:00
tmp2 = this.sharedObject.fetchAll(
2022-10-06 11:29:53 +02:00
"SELECT prenom FROM Membres WHERE nom LIKE '%" + student + "%'"
);
2022-10-07 10:39:13 +02:00
tmp3 = this.sharedObject.fetchAll(
"SELECT idCompte FROM Membres WHERE nom LIKE '%" + student + "%'"
);
2022-10-06 11:29:53 +02:00
this.data = new String[tmp.size()];
for(int i = 0; i <= tmp.size()-1; i++) {
2022-10-07 10:39:13 +02:00
this.data[i] = "[" + tmp3.get(i) + "] " + tmp.get(i) + " " + tmp2.get(i);
System.out.println(this.data[i]);
2022-10-06 11:29:53 +02:00
}
2022-10-06 15:01:25 +02:00
return new JList<>(this.data);
2022-10-06 11:29:53 +02:00
}
2022-10-07 10:39:13 +02:00
protected void addInGrup(int who) {
this.tmpForAddGrupWho = who;
this.tempFrame = new BFrame("Choisissez un groupe", 300, 200, JFrame.DISPOSE_ON_CLOSE);
BLayout settings = new BLayout();
ArrayList<String> tmp = this.sharedObject.fetchAll(
"SELECT intitule FROM Groupes"
);
String[] grupListTmp = new String[tmp.size()];
for(int i = 0; i <= tmp.size()-1; i++) {
grupListTmp[i] = tmp.get(i);
}
this.grupList = new JList<>(grupListTmp);
settings.setPositionY(0);
this.tempFrame.add(this.grupList, settings);
BButton btn = new BButton("Ajouter");
btn.addActionListener(this);
btn.setActionCommand("addInGrup");
settings.setPositionY(1);
this.tempFrame.add(btn, settings);
this.tempFrame.openBFrame();
}
protected void moveToOtherGrup(int who) {
this.tmpForAddGrupWho = who;
this.tempFrame = new BFrame("Choissez un groupe", 300, 200, JFrame.DISPOSE_ON_CLOSE);
BLayout settings = new BLayout();
String tmp = this.sharedObject.fetchAll(
"SELECT G.intitule FROM Membres M, Groupes G WHERE M.idCompte=" + who
).get(0);
JLabel tmp2 = new JLabel("Il est actuellement dans : [" + tmp + "]");
settings.setPositionY(0);
this.tempFrame.add(tmp2, settings);
ArrayList<String> gr = this.sharedObject.fetchAll(
"SELECT intitule FROM Groupes"
);
String[] grupListTmp = new String[gr.size()];
for(int i = 0; i <= gr.size()-1; i++) {
grupListTmp[i] = gr.get(i);
}
this.grupList = new JList<>(grupListTmp);
settings.setPositionY(1);
this.tempFrame.add(this.grupList, settings);
BButton btn = new BButton("Deplacer");
btn.addActionListener(this);
btn.setActionCommand("moveToOtherGrup");
settings.setPositionY(2);
this.tempFrame.add(btn, settings);
this.tempFrame.openBFrame();
}
2022-10-06 11:29:53 +02:00
@Override
public void actionPerformed(ActionEvent e) {
2022-10-07 10:39:13 +02:00
int idCompte = Integer.parseInt(
this.student.getSelectedValue().charAt(1) + "", 10
);
System.out.println(idCompte);
// ManageStudent's frame
if(Objects.equals(e.getActionCommand(), "search")) {
2022-10-06 15:01:25 +02:00
this.add(refreshList(this.inSearch.getText()), this.settings);
2022-10-07 10:39:13 +02:00
} else if(Objects.equals(e.getActionCommand(), "add")){
this.addInGrup(idCompte);
} else if(Objects.equals(e.getActionCommand(), "move")) {
this.moveToOtherGrup(idCompte);
}
// addInGrup's frame
if(Objects.equals(e.getActionCommand(), "addInGrup")) {
this.tmpForAddGrup = Integer.parseInt(
this.sharedObject.fetchAll(
"SELECT idGroupe FROM Groupes WHERE intitule='" + this.grupList.getSelectedValue() + "'"
).get(0), 10
);
if(this.sharedObject.updateRow(
"UPDATE Membres SET idGroupe=" + this.tmpForAddGrup + " WHERE idCompte=" + this.tmpForAddGrupWho)
) {
JOptionPane.showMessageDialog(this, "L'ajout a bien ete effectue !");
} else {
JOptionPane.showMessageDialog(this, "Erreur lors de l'ajout.");
}
this.tempFrame.closeBFrame();
}
// moveToOtherGrup's frame
if(Objects.equals(e.getActionCommand(), "moveToOtherGrup")) {
this.tmpForAddGrup = Integer.parseInt(
this.sharedObject.fetchAll(
"SELECT idGroupe FROM Groupes WHERE intitule='" + this.grupList.getSelectedValue() + "'"
).get(0), 10
);
if(this.sharedObject.updateRow(
"UPDATE Membres SET idGroupe=" + this.tmpForAddGrup + " WHERE idCompte=" + this.tmpForAddGrupWho)
) {
JOptionPane.showMessageDialog(this, "Le deplacement a bien ete effectue !");
} else {
JOptionPane.showMessageDialog(this, "Erreur lors du deplacement.");
}
this.tempFrame.closeBFrame();
2022-10-06 11:29:53 +02:00
}
}
}