fonctionnalité crud + réagencement des classes

This commit is contained in:
2025-10-23 00:35:41 +02:00
parent f2f054631f
commit 8710600b37
5 changed files with 369 additions and 0 deletions
@@ -0,0 +1,37 @@
import javax.swing.*;
import javax.swing.Icon;
import java.awt.*;
/**
* Renderer simple pour afficher un petit carré coloré dans la liste déroulante.
* J'utilise juste un switch pour déterminer la couleur à partir du nom.
*/
public class CouleurList extends DefaultListCellRenderer {
/** renvoie la couleur correspondant au nom affiché dans le menu */
public static Color couleurDe(String nom) {
switch (nom) {
case "Bleu": return Color.CYAN; // ou Color.BLUE si tu veux un bleu plus foncé
case "Rouge": return Color.RED;
case "Vert": return Color.GREEN;
case "Jaune": return Color.YELLOW;
case "Gris": return Color.LIGHT_GRAY;
default: return Color.WHITE;
}
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) {
JLabel lbl = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
String nom = value.toString();
lbl.setIcon(new ColorIcon(couleurDe(nom), 14, 14));
lbl.setIconTextGap(8); // petit espace entre le carré et le texte
}
return lbl;
}
}
+140
View File
@@ -0,0 +1,140 @@
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 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
JLabel lblTheme = new JLabel("Theme");
theme = new JTextField(20);
c.gridx = 0; c.gridy = 3; c.weightx = 0;
centre.add(lblTheme, c);
c.gridx = 1; c.gridy = 3; c.weightx = 1;
centre.add(theme, 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();
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);
try{
ges.modifierParId(rappel.getId(), rappel);
} catch (Exception ex) {
ex.printStackTrace();
}
Main reParent = new Main();
reParent.setLocation(this.getLocation());
reParent.setVisible(true);
dispose();
}
}
}
+116
View File
@@ -0,0 +1,116 @@
// package fr.iutfbleau.papillon;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PanelRappel extends JPanel implements ActionListener, MouseListener{
private final JLabel lbtitre;
private final JLabel lbcontenu;
private final Rappel r;
private final JPopupMenu menu;
private final JMenuItem itemOuvrir;
private boolean estSelectionne = false;
public PanelRappel(Rappel r){
super(new BorderLayout(2,0));
this.r = r;
lbtitre = new JLabel("["+r.getRang()+"] "+r.getTitre());
lbcontenu = new JLabel(r.getContenu());
this.setPreferredSize(new Dimension(70, 50));
this.setBackground(Color.CYAN);
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 2));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
// --- Position du titre (en haut)
c.gridx = 0;
c.gridy = 0;
c.weightx = 1; // prend l'espace
c.fill = GridBagConstraints.NONE;
add(lbtitre, c);
// --- Position du contenu (en dessous)
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.fill = GridBagConstraints.NONE;
add(lbcontenu, c);
// --- Menu contextuel (clic droit) ---
menu = new JPopupMenu();
itemOuvrir = new JMenuItem("Ouvrir");
itemOuvrir.addActionListener(this);
menu.add(itemOuvrir);
// écouteur de souris ajouté ici
addMouseListener(this);
}
public boolean getSelection(){
return estSelectionne;
}
public int getId(){
return r.getId();
}
public Rappel getRappel(){
return r;
}
// ACTIONS
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == itemOuvrir) {
JOptionPane.showMessageDialog(
this,
"Ouverture du rappel : " + lbtitre.getText(),
"Ouvrir",
JOptionPane.INFORMATION_MESSAGE
);
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(e.getComponent(), e.getX(), e.getY());
}else {
// clic gauche = sélection
estSelectionne = !estSelectionne;
if (estSelectionne) {
setBackground(Color.LIGHT_GRAY);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
} else {
setBackground(Color.CYAN);
setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 2));
}
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
// Les autres méthodes sont obligatoires mais inutilisées ici
@Override public void mouseClicked(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
+67
View File
@@ -0,0 +1,67 @@
public class Rappel {
private int id;
private String titre;
private String contenu;
private String theme;
private int rang;
public Rappel(String titre, String contenu, String theme, int rang) {
this(0, titre, contenu, theme, rang);
}
public Rappel(int id, String titre, String contenu, String theme, int rang) {
this.id = id;
this.titre = titre;
this.contenu = contenu;
this.theme = theme;
this.rang = rang;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getContenu() {
return contenu;
}
public void setContenu(String contenu) {
this.contenu = contenu;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public int getRang() {
return rang;
}
public void setRang(int rang) {
this.rang = rang;
}
// @Override
// public String toString() {
// @Override
// public String toString() {
// String texte = "[" + id + "] " + titre + " | " + theme + " | rang=" + rang + " | ";
// if (contenu != null) {
// texte = texte + contenu;
// }
// return texte;
// }
// }
}
+9
View File
@@ -0,0 +1,9 @@
import javax.swing.*;
import java.awt.*;
public class Start{
public static void main(String[] args) {
Main f = new Main();
f.setVisible(true);
}
}