Files
SAE31_2025/src/fr/iutfbleau/papillon/FenetreModif.java
T

149 lines
5.2 KiB
Java
Raw Normal View History

2025-10-23 09:00:30 +02:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FenetreModif extends JFrame implements ActionListener {
private final Main parent; // fenêtre principal (peut être null si tu veux)
private final Rappel rappel; // rappel à modifier (peut être null si tu veux)
private GestionRappel ges = new GestionRappel();
private final JTextField champTitre;
private final JTextArea champContenu;
private Integer[] nombres = {1, 2, 3, 4, 5};
private JComboBox<Integer> rang = new JComboBox<>(nombres);
// private final JTextField theme;
private final String[] nomsCouleurs = {"Bleu", "Rouge", "Vert", "Jaune", "Gris"};
private final JComboBox<String> comboTheme = new JComboBox<>(nomsCouleurs);
private final JButton boutonValider;
private final JButton boutonAnnuler;
/**
* @param parent la fenêtre principale (utilisée pour revenir au même endroit)
* @param rappel le rappel à modifier (peut être null si tu veux juste récupérer des valeurs)
* @param titre titre initial à afficher (utilisé si rappel==null ou si tu préfères passer la valeur)
* @param contenu contenu initial à afficher (idem)
*/
public FenetreModif(Main parent, Rappel rappel, String titre, String contenu) {
super("Modifier un rappel");
this.parent = parent;
this.rappel = rappel;
setSize(350, 250);
setResizable(false);
setLocation(parent.getLocation()); // même position que Main
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ------ UI ------
setLayout(new BorderLayout(10, 10));
JPanel centre = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(4, 4, 4, 4);
c.fill = GridBagConstraints.HORIZONTAL;
// Titre
JLabel lblTitre = new JLabel("Titre :");
champTitre = new JTextField(20);
c.gridx = 0; c.gridy = 0; c.weightx = 0;
centre.add(lblTitre, c);
c.gridx = 1; c.gridy = 0; c.weightx = 1;
centre.add(champTitre, c);
// Contenu
JLabel lblContenu = new JLabel("Contenu :");
champContenu = new JTextArea(4, 20);
champContenu.setLineWrap(true); // active le retour à la ligne
champContenu.setWrapStyleWord(true); // évite de couper un mot en plein milieu
JScrollPane scroll = new JScrollPane(champContenu);
c.gridx = 0; c.gridy = 1; c.weightx = 0;
centre.add(lblContenu, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1; c.gridy = 1; c.weightx = 1; c.weighty = 1.0;
centre.add(scroll, c);
// rang
c.fill = GridBagConstraints.HORIZONTAL;
rang.setSelectedIndex(rappel.getRang()-1);
JLabel lblRang = new JLabel("Rang :");
c.gridx = 0; c.gridy = 2; c.weightx = 0;
centre.add(lblRang, c);
c.gridx = 1; c.gridy = 2; c.weightx = 1;
centre.add(rang, c);
// theme
c.gridx = 0; c.gridy = 4; c.weightx = 0;
centre.add(new JLabel("Theme :"), c);
// liste déroulante de couleurs
c.gridx = 1; c.gridy = 4; c.weightx = 1;
comboTheme.setRenderer(new CouleurList());
comboTheme.setSelectedItem(rappel.getTheme()); // valeur par défaut
centre.add(comboTheme, c);
add(centre, BorderLayout.CENTER);
// Bas : boutons
JPanel bas = new JPanel(new FlowLayout(FlowLayout.RIGHT));
boutonValider = new JButton("Valider");
boutonAnnuler = new JButton("Annuler");
boutonValider.addActionListener(this);
boutonAnnuler.addActionListener(this);
bas.add(boutonValider);
bas.add(boutonAnnuler);
add(bas, BorderLayout.SOUTH);
champTitre.setText(rappel.getTitre());
champContenu.setText(rappel.getContenu());
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == boutonAnnuler) {
parent.setLocation(this.getLocation());
parent.setVisible(true);
this.dispose();
return;
}
if (src == boutonValider) {
String t = champTitre.getText().trim();
String c = champContenu.getText().trim();
int r = (Integer) rang.getSelectedItem();
String th = (String) comboTheme.getSelectedItem();
if (t.isEmpty() || c.isEmpty()) {
JOptionPane.showMessageDialog(this, "Veuillez remplir les deux champs.", "Champs manquants", JOptionPane.WARNING_MESSAGE);
return;
}
// MAJ directe du rappel
rappel.setTitre(t);
rappel.setContenu(c);
rappel.setRang(r);
rappel.setTheme(th);
try{
ges.modifierParId(rappel.getId(), rappel);
} catch (Exception ex) {
ex.printStackTrace();
}
Main reParent = new Main();
reParent.setLocation(this.getLocation());
reParent.setVisible(true);
dispose();
}
}
}