package fr.iutfbleau.projetIHM2022FI2.ETU.Model; import javax.swing.*; import java.awt.*; import java.util.Set; import java.util.HashSet; import fr.iutfbleau.projetIHM2022FI2.API.AbstractChangementFactory; 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.AbstractChangementFactoryNP; import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractGroupeFactoryNP; import fr.iutfbleau.projetIHM2022FI2.Permanent.View.Chargement; import fr.iutfbleau.projetIHM2022FI2.ETU.Controller.ObservateurFenetre; import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreEtudiant; import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreGroupe; /** * Le Model de L'IHM */ public class Model{ private JPanel panGroupe; private FenetreGroupe fenGr; private FenetreEtudiant fenEtu; private AbstractGroupeFactory promo; private AbstractChangementFactory changement; private JFrame fenetre; private Etudiant Selected; 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()); Chargement ch=new Chargement(this.fenetre); this.promo=new AbstractGroupeFactoryNP(this.fenetre); if(this.promo.getPromotion()==null){ this.promo=null; } ch.dispose(); this.fenetre.setVisible(true); this.initEtu(null); if(this.promo==null){ this.fenGr=new FenetreGroupe(null, this, null); this.fenEtu=new FenetreEtudiant(null, this.Selected, this); this.changement=null; }else{ this.changement=new AbstractChangementFactoryNP(promo, this.fenetre); this.fenGr=new FenetreGroupe(this.promo.getPromotion(), this,this.promo.getGroupesOfEtudiant(this.Selected)); this.fenEtu=new FenetreEtudiant(this.promo.getPromotion(), this.Selected, 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.promo .refreshALL(g); this.panGroupe.removeAll(); this.fenGr=new FenetreGroupe(g, this, this.promo.getGroupesOfEtudiant(this.Selected)); this.fenEtu=new FenetreEtudiant(g, this.Selected, 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; } public Set getEtudiant(){ return this.promo.getPromotion().getEtudiants(); } private void initEtu(String err){ Set liste=this.promo.getPromotion().getEtudiants(); JPanel panel = new JPanel(); JPanel myPanel = new JPanel(); JTextField idd = new JTextField(15); myPanel.add(new JLabel("Id:")); myPanel.add(idd); if(err!=null){ myPanel.add(new JLabel(err, SwingConstants.RIGHT)); } panel.add(myPanel); if(JOptionPane.showConfirmDialog(this.fenetre, panel, "login", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION){ this.fenetre.dispose(); System.exit(0); }else{ try{ int id=Integer.parseInt(idd.getText()); for(Etudiant et:liste){ if(et.getId()==id){ this.Selected=et; return; } } }catch(NumberFormatException e){ this.initEtu("Id incomprhéhensible"); } } this.initEtu("Etudiant introuvable"); } // ************************** // FONCTION POUR LES CHANGEMENTS // ****************************** public void changeGroupe(Etudiant e, Groupe b){ if(b==null) return; b=this.promo.refreshALL(b); if(b.getEtudiants()!=null && b.getMax()>b.getEtudiants().size()+1){ this.changement.createChangement(this.fenGr.getG(), e, b); }else{ JOptionPane.showMessageDialog(this.fenetre, "impossible trop d'etudiant dans l'autre Groupe", "erreur", JOptionPane.ERROR_MESSAGE); } this.showGroupe(this.fenGr.getG()); } public Set getGroupePartition(){ this.promo.refreshALL(this.fenGr.getG().getPointPoint()); Set retour=new HashSet<>(); if(this.fenGr.getG().getPointPoint().getType()!=TypeGroupe.PARTITION) throw new IllegalStateException("impossible de changer un étudiant d'un groupe ne provenant pas d'une partition"); for(Groupe sous:this.fenGr.getG().getPointPoint().getSousGroupes()){ if(sous.getId()!=this.fenGr.getG().getId()){ retour.add(sous); } } return retour; } }