From e979d6acdf359b29e8d948194e34b500cdaf4d2c Mon Sep 17 00:00:00 2001 From: saaya-code Date: Thu, 21 Mar 2024 22:46:31 +0100 Subject: [PATCH] added tableModel and the IHMaffichage layout setting --- src/ExercicesTP/CRUD/FormationDAO.java | 9 ++ src/ExercicesTP/Helpers/TableModel.java | 141 ++++++++++++++++++ .../IHM/IHMAffichageFormation.java | 67 +++++++++ src/ExercicesTP/IHM/IHMAjoutFormation.java | 24 +-- .../IHM/IHMRechercheFormation.java | 35 ++++- src/ExercicesTP/IHM/Principale.java | 18 ++- .../interfaces/FormationDaoCRUD.java | 4 + 7 files changed, 278 insertions(+), 20 deletions(-) create mode 100644 src/ExercicesTP/Helpers/TableModel.java create mode 100644 src/ExercicesTP/IHM/IHMAffichageFormation.java diff --git a/src/ExercicesTP/CRUD/FormationDAO.java b/src/ExercicesTP/CRUD/FormationDAO.java index 7a51306..02e746a 100644 --- a/src/ExercicesTP/CRUD/FormationDAO.java +++ b/src/ExercicesTP/CRUD/FormationDAO.java @@ -41,6 +41,15 @@ public void deleteFormation(int id) { } } + public ResultSet selection(String req){ + try{ + PreparedStatement ps = con.prepareStatement(req); + return ps.executeQuery(); + } catch (SQLException e) { + System.out.println("Erreur sql + "+ e.getMessage()); + return null; + } + } public void updateFormation(Formation formation) { PreparedStatement ps = null; try { diff --git a/src/ExercicesTP/Helpers/TableModel.java b/src/ExercicesTP/Helpers/TableModel.java new file mode 100644 index 0000000..0116f37 --- /dev/null +++ b/src/ExercicesTP/Helpers/TableModel.java @@ -0,0 +1,141 @@ +package ExercicesTP.Helpers; + +import ExercicesTP.CRUD.FormationDAO; +import ExercicesTP.Formation; + + +import javax.swing.*; +import javax.swing.table.AbstractTableModel; +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; + +public class TableModel extends AbstractTableModel { + ArrayList data; + ResultSetMetaData rsmd; + FormationDAO dao; + + public TableModel(ResultSet rs, FormationDAO dao){ + data = new ArrayList(); + this.dao = dao; + try { + rsmd = rs.getMetaData(); + Object[] ligne = new Object[rsmd.getColumnCount()]; + for (int i = 0; i < rsmd.getColumnCount(); i++) { + ligne[i] = rsmd.getColumnName(i+1); + } + data.add(ligne); + } catch (SQLException e) { + throw new RuntimeException(e); + } + try { + + while (rs.next()) { + Object[] ligne = new Object[rsmd.getColumnCount()]; + for(int i = 0;i dispose()); } - public static void main(String[] args) { - new IHMAjoutFormation(); - } + } diff --git a/src/ExercicesTP/IHM/IHMRechercheFormation.java b/src/ExercicesTP/IHM/IHMRechercheFormation.java index 0b82b16..e16752f 100644 --- a/src/ExercicesTP/IHM/IHMRechercheFormation.java +++ b/src/ExercicesTP/IHM/IHMRechercheFormation.java @@ -9,7 +9,7 @@ import ExercicesTP.CRUD.FormationDAO; import ExercicesTP.Formation; -public class IHMRechercheFormation extends JFrame { +public class IHMRechercheFormation extends JInternalFrame { private JLabel referenceLabel, titleLabel, dateLabel, lieuLabel, certificationLabel; private JTextField referenceField, titleField; @@ -17,12 +17,12 @@ public class IHMRechercheFormation extends JFrame { private JCheckBox certificationCheckbox; private JButton rechercheButton, cancelButton, modifierButton, supprimerButton; public FormationDAO formationDAO; - public IHMRechercheFormation() { - super("Ajout d'une formation"); + public IHMRechercheFormation(FormationDAO dao) { + super("Recherche d'une formation"); initializeComponents(); createLayout(); addEventListeners(); - formationDAO = new FormationDAO(); + formationDAO = dao; this.setVisible(true); } @@ -74,9 +74,12 @@ private void createLayout() { getContentPane().add(panel); getContentPane().add(panel2); getContentPane().setLayout(new FlowLayout()); + modifierButton.setEnabled(false); + supprimerButton.setEnabled(false); + rechercheButton.setEnabled(false); + setSize(500,250); - setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @@ -95,7 +98,11 @@ private void clearInputs() { dateTextField.setText(""); lieuTextField.setText(""); certificationCheckbox.setSelected(false); + supprimerButton.setEnabled(false); + modifierButton.setEnabled(false); + rechercheButton.setEnabled(false); } + private void addEventListeners() { rechercheButton.addActionListener(e -> { Formation formation = formationDAO.getFormation(Integer.parseInt(referenceField.getText())); @@ -125,6 +132,24 @@ private void addEventListeners() { }); + referenceField.addFocusListener(new java.awt.event.FocusAdapter() { + public void focusLost(java.awt.event.FocusEvent evt) { + if(referenceField.getText().isEmpty()) return; + Formation formation = formationDAO.getFormation(Integer.parseInt(referenceField.getText())); + if (formation != null) { + modifierButton.setEnabled(true); + supprimerButton.setEnabled(true); + } else { + modifierButton.setEnabled(false); + supprimerButton.setEnabled(false); + } + } + }); + referenceField.addKeyListener(new java.awt.event.KeyAdapter() { + public void keyReleased(java.awt.event.KeyEvent evt) { + rechercheButton.setEnabled(!referenceField.getText().isEmpty()); + } + }); } diff --git a/src/ExercicesTP/IHM/Principale.java b/src/ExercicesTP/IHM/Principale.java index 9789c85..b18f52c 100644 --- a/src/ExercicesTP/IHM/Principale.java +++ b/src/ExercicesTP/IHM/Principale.java @@ -1,5 +1,6 @@ package ExercicesTP.IHM; +import ExercicesTP.CRUD.FormationDAO; import TP_Base.Config; import TP_Base.MyConnexion; @@ -27,9 +28,8 @@ public class Principale extends JFrame { JMenuItem menuItemRechercherFormation; JMenuItem menuItemAfficherFormation; JDesktopPane desktop; - Connection con = null; + FormationDAO formationDao; Principale() { - con = MyConnexion.getConnection(Config.URL, Config.USERNAME, Config.PASSWORD); menuBar = new JMenuBar(); this.setTitle("Gestion des formation"); this.setSize(1000, 900); @@ -71,23 +71,31 @@ public class Principale extends JFrame { menuBar.add(menuEnseignant); this.setJMenuBar(menuBar); this.setVisible(true); - + formationDao = new FormationDAO(); // listenners menuItemAjouteurFormation.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - IHMAjoutFormation ihmAjout = new IHMAjoutFormation(); + IHMAjoutFormation ihmAjout = new IHMAjoutFormation(formationDao); desktop.add(ihmAjout); } }); menuItemRechercherFormation.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - IHMRechercheFormation ihmRecherche = new IHMRechercheFormation(); + IHMRechercheFormation ihmRecherche = new IHMRechercheFormation(formationDao); desktop.add(ihmRecherche); } }); + menuItemAfficherFormation.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + IHMAffichageFormation ihmAffichage = new IHMAffichageFormation(formationDao); + desktop.add(ihmAffichage); + } + }); + diff --git a/src/ExercicesTP/interfaces/FormationDaoCRUD.java b/src/ExercicesTP/interfaces/FormationDaoCRUD.java index 379ddd0..b240c20 100644 --- a/src/ExercicesTP/interfaces/FormationDaoCRUD.java +++ b/src/ExercicesTP/interfaces/FormationDaoCRUD.java @@ -1,10 +1,14 @@ package ExercicesTP.interfaces; import ExercicesTP.Formation; +import java.sql.ResultSet; + public interface FormationDaoCRUD { public void addFormation(Formation formation); public void deleteFormation(int id); public void updateFormation(Formation formation); public Formation getFormation(int id); public Formation[] getAllFormations(); + + public ResultSet selection(String rq); }