2022-11-13 19:50:53 +01:00
package fr.iutfbleau.projetIHM2022FI2.Graphic.Controller ;
import java.awt.event.* ;
import java.util.LinkedHashSet ;
import fr.iutfbleau.projetIHM2022FI2.API.* ;
import javax.swing.JOptionPane ;
import fr.iutfbleau.projetIHM2022FI2.API.Groupe ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.Model ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.FenetreSelectionEtu ;
import javax.swing.* ;
import java.util.Set ;
public class ObservateurModifGroupe implements ActionListener {
private Model m ;
private Groupe groupe ;
public ObservateurModifGroupe ( Model m , Groupe g ) {
this . m = m ;
this . groupe = g ;
}
@Override
public void actionPerformed ( ActionEvent e ) {
if ( e . getActionCommand ( ) = = " suprimer ce groupe " ) {
Groupe pointpoint = this . groupe . getPointPoint ( ) ;
this . m . delete ( this . groupe ) ;
this . m . showGroupe ( pointpoint ) ;
}
if ( e . getActionCommand ( ) = = " créer un sous groupe " ) {
String g [ ] = new String [ 2 ] ;
g [ 0 ] = " Partition " ;
g [ 1 ] = " Free " ;
int rep = JOptionPane . showOptionDialog ( m . getFenetre ( ) , " Type du nouveau Groupe " , " Type " , JOptionPane . YES_NO_OPTION , JOptionPane . QUESTION_MESSAGE , null , g , g [ 0 ] ) ;
if ( rep = = JOptionPane . YES_OPTION ) {
try {
JPanel myPanel = new JPanel ( ) ;
JTextField xField = new JTextField ( 5 ) ;
JTextField yField = new JTextField ( 20 ) ;
myPanel . add ( new JLabel ( " Nombre: " ) ) ;
myPanel . add ( xField ) ;
myPanel . add ( Box . createHorizontalStrut ( 15 ) ) ; // a spacer
myPanel . add ( new JLabel ( " Nom: " ) ) ;
myPanel . add ( yField ) ;
int result = JOptionPane . showConfirmDialog ( m . getFenetre ( ) , myPanel ,
" Entrer le nombre et le nom des Partitions " , JOptionPane . OK_CANCEL_OPTION ) ;
if ( result = = JOptionPane . OK_OPTION ) {
int nb = Integer . parseInt ( xField . getText ( ) ) ;
if ( nb > this . groupe . getEtudiants ( ) . size ( ) ) {
JOptionPane . showMessageDialog ( m . getFenetre ( ) , " nombre de partition trop grand " , " erreur " , JOptionPane . ERROR_MESSAGE ) ;
return ;
}
m . partition ( this . groupe , nb , yField . getText ( ) ) ;
m . showGroupe ( this . groupe ) ;
}
} catch ( NumberFormatException er ) {
JOptionPane . showMessageDialog ( m . getFenetre ( ) , " erreur dans le nombre de partition " , " erreur " , JOptionPane . ERROR_MESSAGE ) ;
}
} else {
try {
JPanel myPanel = new JPanel ( ) ;
JTextField xField = new JTextField ( 5 ) ;
JTextField zField = new JTextField ( 5 ) ;
JTextField yField = new JTextField ( 20 ) ;
myPanel . add ( new JLabel ( " Nom: " ) ) ;
myPanel . add ( yField ) ;
myPanel . add ( Box . createHorizontalStrut ( 15 ) ) ; // a spacer
myPanel . add ( new JLabel ( " Min: " ) ) ;
myPanel . add ( xField ) ;
myPanel . add ( new JLabel ( " Max: " ) ) ;
myPanel . add ( zField ) ;
int result = JOptionPane . showConfirmDialog ( m . getFenetre ( ) , myPanel ,
" Entrer le nombre et le nom des Partitions " , JOptionPane . OK_CANCEL_OPTION ) ;
if ( result = = JOptionPane . OK_OPTION ) {
int min = Integer . parseInt ( xField . getText ( ) ) ;
int max = Integer . parseInt ( zField . getText ( ) ) ;
if ( min > this . groupe . getEtudiants ( ) . size ( ) | | max > this . groupe . getEtudiants ( ) . size ( ) ) {
JOptionPane . showMessageDialog ( m . getFenetre ( ) , " nombre de partition trop grand " , " erreur " , JOptionPane . ERROR_MESSAGE ) ;
return ;
}
m . free ( groupe , yField . getText ( ) , min , max ) ;
m . showGroupe ( this . groupe ) ;
}
} catch ( NumberFormatException er ) {
JOptionPane . showMessageDialog ( m . getFenetre ( ) , " erreur dans le nombre de partition " , " erreur " , JOptionPane . ERROR_MESSAGE ) ;
}
}
}
if ( e . getActionCommand ( ) = = " renomer " ) {
String nouveau = JOptionPane . showInputDialog ( m . getFenetre ( ) , " Rentrer le nouveau Nom " ) ;
2022-11-13 19:58:10 +01:00
m . rename ( nouveau , this . groupe ) ;
2022-11-13 19:50:53 +01:00
m . showGroupe ( this . groupe ) ;
}
if ( e . getActionCommand ( ) = = " ajouter " ) {
Set < Etudiant > ajout = new LinkedHashSet < > ( ) ;
JPanel myPanel = new FenetreSelectionEtu ( this . groupe . getPointPoint ( ) , ajout ) ;
if ( JOptionPane . showConfirmDialog ( m . getFenetre ( ) , new JScrollPane ( myPanel ) , " Selectionner les étudiant a ajouter " , JOptionPane . OK_CANCEL_OPTION ) = = JOptionPane . YES_OPTION ) {
if ( this . groupe . getMax ( ) < this . groupe . getEtudiants ( ) . size ( ) + ajout . size ( ) ) {
JOptionPane . showMessageDialog ( m . getFenetre ( ) , " Il y a trop d'etudiant pour le groupe " , " erreur " , JOptionPane . ERROR_MESSAGE ) ;
}
for ( Etudiant et : ajout ) {
2022-11-13 19:58:10 +01:00
m . addEtudiant ( this . groupe , et ) ;
2022-11-13 19:50:53 +01:00
}
m . showGroupe ( this . groupe ) ;
}
}
}
}