package fr.iutfbleau.projetIHM2022FI2.ROOT.Model; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; import java.util.Set; import java.sql.Connection; import java.sql.ResultSet; import javax.swing.*; import java.awt.*; import fr.iutfbleau.projetIHM2022FI2.API.AbstractGroupeFactory; import fr.iutfbleau.projetIHM2022FI2.API.Etudiant; import fr.iutfbleau.projetIHM2022FI2.API.Groupe; import fr.iutfbleau.projetIHM2022FI2.API.TypeGroupe; import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractGroupeFactoryNP; import fr.iutfbleau.projetIHM2022FI2.MNP.GroupeNP; import fr.iutfbleau.projetIHM2022FI2.ROOT.Controller.ObservateurFenetre; import fr.iutfbleau.projetIHM2022FI2.ROOT.Util.BD; import fr.iutfbleau.projetIHM2022FI2.ROOT.View.Chargement; import fr.iutfbleau.projetIHM2022FI2.ROOT.View.FenetreEtudiant; import fr.iutfbleau.projetIHM2022FI2.ROOT.View.FenetreGroupe; /** * Le Model de L'IHM */ public class Model{ private JPanel panGroupe; private FenetreGroupe fenGr; private FenetreEtudiant fenEtu; private AbstractGroupeFactory promo; private JFrame fenetre; private BD bd; public Model(){ this.fenetre=new JFrame(); this.fenetre.setSize(1200, 720); this.fenetre.setLocation(100,100); this.fenetre.addWindowListener(new ObservateurFenetre()); this.fenetre.setLayout(new GridLayout(1,2)); this.fenetre.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); this.fenetre.setMinimumSize(this.fenetre.getSize()); this.bd=new BD(this.fenetre); Chargement ch=new Chargement(this.fenetre); this.promo=this.getPromo(ch); ch.dispose(); this.fenetre.setVisible(true); if(this.promo==null){ this.fenGr=new FenetreGroupe(null, this); this.fenEtu=new FenetreEtudiant(null, this); }else{ this.fenGr=new FenetreGroupe(this.promo.getPromotion(), this); this.fenEtu=new FenetreEtudiant(this.promo.getPromotion(), this); } this.panGroupe=new JPanel(new GridLayout(1,1)); if(this.promo!=null){ this.showGroupe(this.promo.getPromotion()); }else{ this.showGroupe(null); } } /** * Fonction pour refresh/changer de groupe d'affichage * @param g le groupe a afficher */ public void showGroupe(Groupe g){ if(g!=null) g=this.bd.refreshALL(g); this.panGroupe.removeAll(); this.fenGr=new FenetreGroupe(g, this); this.fenEtu=new FenetreEtudiant(g, this); this.fenetre.getContentPane().removeAll(); this.panGroupe.add(this.fenGr.getPan()); this.panGroupe.revalidate(); this.fenetre.add(this.panGroupe); JScrollPane scroll=new JScrollPane(this.fenEtu.getPan()); scroll.getVerticalScrollBar().setUnitIncrement(15); this.fenetre.add(scroll); this.fenetre.revalidate(); } /** * getteur de la fenetre * @return JFrame la fenetre */ public JFrame getFenetre() { return fenetre; } /** * Fonction permettant d'initialiser l'AbstractFactory de la promo * avec la Base de donné * @param Chargement: pour indiquer la progression du chargement */ private AbstractGroupeFactory getPromo(Chargement chargement){ AbstractGroupeFactory agf=null; Connection cnx=this.bd.cnx(); //on récupère le Groupe de la BD n'ayant pas de Parent (La promo donc) try{ PreparedStatement pst = cnx.prepareStatement( "SELECT `id`, `nom`, `min`, `max`, `value`, `id-parent` FROM `Groupe` join `TYPE` on Groupe.Type=TYPE.name ORDER BY Groupe.id ASC;"); ResultSet rs = pst.executeQuery(); rs=pst.executeQuery(); rs.last(); int nbgrp=rs.getRow(); rs=pst.executeQuery(); try{ //Si il existe bien une promotion if(rs.next()){ //On créer le groupe de promo Groupe groupe=new GroupeNP(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4), TypeGroupe.getType(rs.getString(5)), null); //On lui ajoute tout ses sous-groupe this.init(groupe, (100/nbgrp), chargement); //On créer la Factory agf=new AbstractGroupeFactoryNP(groupe, this.fenetre); }else{ //Si aucune ligne et donc pas de promo: this.promo=null; } }catch(SQLException e){ } rs.close(); pst.close(); }catch(SQLException e){ } this.bd.close(cnx); return agf; } public Set getEtudiant(){ return this.bd.getEtudiant(); } private void init(Groupe g, int pourcentage, Chargement ch){ this.bd.refreshALL(g); ch.addPourcent(pourcentage+1); for(Groupe gr:g.getSousGroupes()){ this.init(gr, pourcentage, ch); } } // *********************** // FONCTION POUR SUPRIMER UN ETUDIANT // *********************** public boolean deleteEtu(Etudiant e){ if(this.deleteEtutoChildren(e, this.fenGr.getG())){ this.promo.dropFromGroupe(this.fenGr.getG(), e); this.showGroupe(this.fenGr.getG()); return true; }else{ return false; } } private boolean deleteEtutoChildren(Etudiant e, Groupe g){ if(g.getMax() ajout){ Chargement ch=new Chargement(this.fenetre); this.fenetre.setVisible(false); //on creer le groupe this.promo.createGroupe(g, name, min, max); Groupe creer=null; //on le recupere for(Groupe gr:g.getSousGroupes()){ if(gr.getName()==name){ creer=gr; break; } } //on y ajoute les étudiant int n=0; for(Etudiant e:ajout){ if(this.addEtudiant(creer, e)==false) n++; } creer=this.bd.refreshALL(creer); ch.dispose(); this.fenetre.setVisible(true); if(n>0) JOptionPane.showMessageDialog(this.fenetre, "erreur impossible d'ajouter "+n+" etudiants par manque de place dans le groupe de ses parents", "erreur", JOptionPane.ERROR_MESSAGE); //Si finalement avec les etudiants qui n'ont pas pus être ajouter le min n'est pas atteint if(creer.getEtudiants().size()+1<=creer.getMin()){ //On le supprime this.promo.deleteGroupe(creer); JOptionPane.showMessageDialog(this.fenetre, "en raison du manque d'etudiant pour le nombre min le groupe n'a pas pus être créer", "errer", JOptionPane.ERROR_MESSAGE); } this.showGroupe(g); } public void addPromo(int min, int max, String name, Set ajout){ Chargement ch=new Chargement(this.fenetre); this.promo=new AbstractGroupeFactoryNP(name, min, max, this.fenetre); this.fenetre.setVisible(false); ch.addPourcent(20); int pourcent=85/ajout.size(); for(Etudiant e:ajout){ this.addEtudiant(this.promo.getPromotion(), e); ch.addPourcent(pourcent); } ch.dispose(); this.fenetre.setVisible(true); this.showGroupe(this.promo.getPromotion()); } }