Files
FIProjetIHM2022/src/ManageStudent.java
pro.boooooo 42ad496166 %
2022-10-07 10:39:13 +02:00

230 lines
7.8 KiB
Java

import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Objects;
public class ManageStudent extends BFrame implements ActionListener {
protected BDatabase sharedObject;
protected BFrame tempFrame;
protected BLayout settings;
protected BInput inSearch;
protected BButton moveTo;
protected BButton btnSearch;
protected BButton addTo;
protected BButton seeReq;
protected JList<String> student;
protected JList<String> grupList;
protected String[] data;
protected int tmpForAddGrup;
protected int tmpForAddGrupWho;
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);
this.settings.setPositionX(1);
this.settings.setPositionY(0);
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);
this.moveTo = new BButton("Deplacer dans un groupe");
this.moveTo.setActionCommand("move");
this.moveTo.addActionListener(this);
this.settings.setPositionX(2);
this.settings.setPositionY(1);
this.add(moveTo, this.settings);
this.addTo = new BButton("Ajouter dans un groupe");
this.addTo.setActionCommand("add");
this.addTo.addActionListener(this);
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) {
ArrayList<String> tmp = new ArrayList<>();
ArrayList<String> tmp2 = new ArrayList<>();
ArrayList<String> tmp3 = new ArrayList<>();
tmp = this.sharedObject.fetchAll(
"SELECT nom FROM Membres WHERE nom LIKE '%" + student + "%'"
);
tmp2 = this.sharedObject.fetchAll(
"SELECT prenom FROM Membres WHERE nom LIKE '%" + student + "%'"
);
tmp3 = this.sharedObject.fetchAll(
"SELECT idCompte FROM Membres WHERE nom LIKE '%" + student + "%'"
);
this.data = new String[tmp.size()];
for(int i = 0; i <= tmp.size()-1; i++) {
this.data[i] = "[" + tmp3.get(i) + "] " + tmp.get(i) + " " + tmp2.get(i);
System.out.println(this.data[i]);
}
return new JList<>(this.data);
}
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();
}
@Override
public void actionPerformed(ActionEvent e) {
int idCompte = Integer.parseInt(
this.student.getSelectedValue().charAt(1) + "", 10
);
System.out.println(idCompte);
// ManageStudent's frame
if(Objects.equals(e.getActionCommand(), "search")) {
this.add(refreshList(this.inSearch.getText()), this.settings);
} 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();
}
}
}