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.API.Model; import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractChangementFactoryNP; import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractGroupeFactoryNP; import fr.iutfbleau.projetIHM2022FI2.Permanent.Controller.ObservateurFenetre; import fr.iutfbleau.projetIHM2022FI2.Permanent.View.Chargement; import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreEtudiant; import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreGroupe; /** * Le Model de L'IHM */ public class ModelEtu implements Model{ private JPanel panGroupe; private FenetreGroupe fenGr; private FenetreEtudiant fenEtu; private AbstractGroupeFactory promo; private AbstractChangementFactory changement; private JFrame fenetre; private Etudiant Selected; public ModelEtu(){ 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); } } @Override /** * 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(); } @Override /** * getteur de la fenetre * @return JFrame la fenetre */ public JFrame getFenetre() { return fenetre; } @Override 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 // ****************************** @Override 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){ if(b.getEtudiants().size()>this.fenGr.getG().getEtudiants().size()){ JPanel myPanel= new JPanel(new GridLayout(2,1)); JTextField xField = new JTextField(100); xField.setMinimumSize(new Dimension(100, 100)); xField.setPreferredSize(new Dimension(100,100)); myPanel.add(new JLabel("Ce Groupe est plus grand que le votre raison du changement:")); myPanel.add(xField); if(JOptionPane.showConfirmDialog(this.fenetre, myPanel, "Changer dans quel Groupe ?", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION){ //IL faut que l'etudiant detaile un minimun la raison if(xField.getText().length()>5){ this.changement.createChangement(this.fenGr.getG(), e, b, xField.getText()); }else{ JOptionPane.showMessageDialog(this.fenetre, "veuillez detailler votre raison", "erreur", JOptionPane.ERROR_MESSAGE); } } }else{ 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()); } @Override 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; } @Override public boolean addEtudiant(Groupe g, Etudiant e) { // TODO Auto-generated method stub return false; } @Override public void delete(Groupe g) { // TODO Auto-generated method stub } @Override public void rename(String name, Groupe g) { // TODO Auto-generated method stub } @Override public void partition(Groupe g, int n, String name) { // TODO Auto-generated method stub } @Override public void free(Groupe g, String name, int min, int max, Set ajout) { // TODO Auto-generated method stub } @Override public void addPromo(int min, int max, String name, Set ajout) { // TODO Auto-generated method stub } @Override public boolean deleteEtu(Etudiant e) { // TODO Auto-generated method stub return false; } }