Files
FIprojetIHM2022/java/APIGroupe/src/fr/iutfbleau/projetIHM2022FI2/ETU/Model/Model.java

156 lines
5.2 KiB
Java
Raw Normal View History

package fr.iutfbleau.projetIHM2022FI2.ETU.Model;
2022-11-15 15:08:13 +01:00
import javax.swing.*;
import java.awt.*;
import java.util.Set;
import java.util.HashSet;
2022-11-29 15:09:41 +01:00
import fr.iutfbleau.projetIHM2022FI2.API.AbstractChangementFactory;
2022-11-15 15:08:13 +01:00
import fr.iutfbleau.projetIHM2022FI2.API.AbstractGroupeFactory;
import fr.iutfbleau.projetIHM2022FI2.API.Etudiant;
import fr.iutfbleau.projetIHM2022FI2.API.Groupe;
import fr.iutfbleau.projetIHM2022FI2.API.TypeGroupe;
2022-11-29 15:09:41 +01:00
import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractChangementFactoryNP;
2022-11-15 15:08:13 +01:00
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;
2022-11-15 15:08:13 +01:00
/**
* Le Model de L'IHM
*/
public class Model{
2022-11-17 14:45:42 +01:00
2022-11-23 15:06:55 +01:00
private JPanel panGroupe;
private FenetreGroupe fenGr;
private FenetreEtudiant fenEtu;
private AbstractGroupeFactory promo;
2022-11-29 15:09:41 +01:00
private AbstractChangementFactory changement;
private JFrame fenetre;
private Etudiant Selected;
2022-11-29 15:09:41 +01:00
2022-11-23 15:06:55 +01:00
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;
}
2022-11-23 15:06:55 +01:00
ch.dispose();
this.fenetre.setVisible(true);
this.initEtu(null);
2022-11-23 15:06:55 +01:00
if(this.promo==null){
this.fenGr=new FenetreGroupe(null, this, null);
2022-11-29 15:09:41 +01:00
this.fenEtu=new FenetreEtudiant(null, this.Selected, this);
this.changement=null;
2022-11-23 15:06:55 +01:00
}else{
2022-11-29 15:09:41 +01:00
this.changement=new AbstractChangementFactoryNP(promo, this.fenetre);
this.fenGr=new FenetreGroupe(this.promo.getPromotion(), this,this.promo.getGroupesOfEtudiant(this.Selected));
2022-11-29 15:09:41 +01:00
this.fenEtu=new FenetreEtudiant(this.promo.getPromotion(), this.Selected, this);
2022-11-23 15:06:55 +01:00
}
this.panGroupe=new JPanel(new GridLayout(1,1));
if(this.promo!=null){
this.showGroupe(this.promo.getPromotion());
}else{
this.showGroupe(null);
2022-11-15 15:08:13 +01:00
}
}
2022-11-23 15:06:55 +01:00
2022-11-23 15:06:55 +01:00
/**
* 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);
2022-11-23 15:06:55 +01:00
this.panGroupe.removeAll();
this.fenGr=new FenetreGroupe(g, this, this.promo.getGroupesOfEtudiant(this.Selected));
2022-11-29 15:09:41 +01:00
this.fenEtu=new FenetreEtudiant(g, this.Selected, this);
2022-11-23 15:06:55 +01:00
this.fenetre.getContentPane().removeAll();
this.panGroupe.add(this.fenGr.getPan());
2022-11-23 15:06:55 +01:00
this.panGroupe.revalidate();
this.fenetre.add(this.panGroupe);
JScrollPane scroll=new JScrollPane(this.fenEtu.getPan());
2022-11-23 15:06:55 +01:00
scroll.getVerticalScrollBar().setUnitIncrement(15);
this.fenetre.add(scroll);
this.fenetre.revalidate();
}
2022-11-15 15:08:13 +01:00
/**
* getteur de la fenetre
* @return JFrame la fenetre
*/
public JFrame getFenetre() {
return fenetre;
}
public Set<Etudiant> getEtudiant(){
return this.promo.getPromotion().getEtudiants();
}
private void initEtu(String err){
Set<Etudiant> 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);
2022-11-29 15:09:41 +01:00
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");
}
2022-11-29 15:09:41 +01:00
// **************************
// FONCTION POUR LES CHANGEMENTS
// ******************************
public void changeGroupe(Etudiant e, Groupe b){
if(b==null)
return;
b=this.promo.refreshALL(b);
2022-11-29 15:09:41 +01:00
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<Groupe> getGroupePartition(){
this.promo.refreshALL(this.fenGr.getG().getPointPoint());
2022-11-29 15:09:41 +01:00
Set<Groupe> 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;
}
2022-11-15 15:08:13 +01:00
}