Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. package vues;
  2.  
  3. import modele.Personne;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.event.ListSelectionEvent;
  7. import javax.swing.event.ListSelectionListener;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.*;
  11.  
  12. public class noName {
  13.     private JPanel rootPannel;
  14.     private JButton ajouterButton;
  15.     private JButton modifierButton;
  16.     private JButton mailingButton;
  17.     private JTextArea textAreaInfoPersonne;
  18.     private JList<Personne> listPersonne;
  19.  
  20.     private DefaultListModel<Personne> modele = new DefaultListModel<>();
  21.     private AjoutDlg ajoutDlg = null;
  22.     private MailingDlg mailingDLG = null;
  23.  
  24.     private void initModele(){
  25.  
  26.         try {
  27.             JFileChooser fileChooser = new JFileChooser("D:/POP_java/");
  28.             int status = fileChooser.showOpenDialog(null);
  29.             if (status == JFileChooser.APPROVE_OPTION) {
  30.                 File selectedFile = fileChooser.getSelectedFile();
  31.                 FileInputStream f = new FileInputStream(selectedFile);
  32.                 ObjectInputStream out = new ObjectInputStream(f);
  33.  
  34.                 this.modele = (DefaultListModel) out.readObject();
  35.                 out.close();
  36.                 return;
  37.             }
  38.         }
  39.         catch (Exception ex) {
  40.             ex.printStackTrace();
  41.         }
  42.                                         /*this.modele = new DefaultListModel<>();
  43.                                         modele.addElement(new Personne("LEFEBVRE", "MICHAEL", "nord"));
  44.                                         modele.addElement(new Personne("BOUCHEZ", "MARC", "VAR"));
  45.                                         modele.addElement(new Personne("PEPETTE", "lili", "ILE DE FRANCE"));
  46.                                         modele.addElement(new Personne("MINOU", "zizi", "PAS DE CALAIS"));*/
  47.     }
  48.     public noName() {
  49.         //L I E R       LE      M O D E L E
  50.         initModele();
  51.         this.listPersonne.setModel(modele);
  52.        
  53.         //G E S T I O N     E V E N E M E N T
  54.                          // AJOUTER UNE PERSONNE
  55.         ajouterButton.addActionListener(new ActionListener() {
  56.             @Override
  57.             public void actionPerformed(ActionEvent actionEvent) {
  58.                 if(ajoutDlg == null){
  59.                     ajoutDlg = new AjoutDlg();
  60.                     ajoutDlg.pack();
  61.                 }
  62.                 Personne pers = new Personne(null, null, null);
  63.                 if (ajoutDlg.ouvrirDlg(pers, true)){
  64.                     if (! modele.contains(pers)) //refus des doublons
  65.                         modele.addElement(pers);
  66.                 }
  67.                 textAreaInfoPersonne.setText("");
  68.                 modifierButton.setEnabled(false);
  69.             }
  70.         });
  71.  
  72.         //-------------  E V E N E M E N T    S U R    L A     L I S T E  --------------
  73.        
  74.         listPersonne.addListSelectionListener(new ListSelectionListener() {
  75.             @Override
  76.             public void valueChanged(ListSelectionEvent listSelectionEvent) {
  77.                 Personne pers = listPersonne.getSelectedValue();
  78.  
  79.                 //textAreaInfoPersonne.setText("");
  80.                
  81.                 if(pers==null) return;
  82.                 textAreaInfoPersonne.setText("nom : " + pers.getNom());
  83.                 textAreaInfoPersonne.append("\nprenom : " + pers.getPrenom());
  84.                 textAreaInfoPersonne.append("\nregion : " + pers.getRegion());
  85.                 //rend visible le bouton modifier
  86.                 modifierButton.setEnabled(true);
  87.             }
  88.         });
  89.  
  90.     // M O D I F I E R    
  91.         modifierButton.addActionListener(new ActionListener() {
  92.             @Override
  93.             public void actionPerformed(ActionEvent actionEvent) {
  94.                 if(ajoutDlg == null){
  95.                     ajoutDlg = new AjoutDlg();
  96.                     ajoutDlg.pack();
  97.                 }
  98.                 Personne pers = listPersonne.getSelectedValue();
  99.                 Personne nouv = new Personne(pers.getNom(),pers.getPrenom(),pers.getRegion());
  100.                 if (ajoutDlg.ouvrirDlg(nouv, false)){
  101.                     listPersonne.clearSelection();
  102.                     modele.removeElement(pers);
  103.                     modele.addElement(nouv);
  104.                 }
  105.                 textAreaInfoPersonne.setText("");
  106.                 modifierButton.setEnabled(false);
  107.             }
  108.         });
  109. // M A I L
  110.         mailingButton.addActionListener(new ActionListener() {
  111.             @Override
  112.             public void actionPerformed(ActionEvent actionEvent) {
  113.                 if (mailingDLG == null){
  114.                     mailingDLG= new MailingDlg();
  115.                     mailingDLG.pack();
  116.                 }
  117.                 mailingDLG.ouvrir(modele);
  118.             }
  119.         });
  120.     }
  121.  
  122.     //------------------------------------- main ------------------------------
  123.     public static void main(String[] args) {
  124.         JFrame frame = new JFrame("noName");
  125.         frame.setContentPane(new noName().rootPannel);
  126.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  127.         frame.pack();
  128.         frame.setVisible(true);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement