Skip to content

Commit

Permalink
added tableModel and the IHMaffichage layout setting
Browse files Browse the repository at this point in the history
  • Loading branch information
saaya-code committed Mar 21, 2024
1 parent 7636bf1 commit e979d6a
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/ExercicesTP/CRUD/FormationDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
141 changes: 141 additions & 0 deletions src/ExercicesTP/Helpers/TableModel.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> data;
ResultSetMetaData rsmd;
FormationDAO dao;

public TableModel(ResultSet rs, FormationDAO dao){
data = new ArrayList<Object[]>();
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<rsmd.getColumnCount();i++){
ligne[i] = rs.getObject(i+1);
System.out.println(ligne[i]);
}
data.add(ligne);
}
}catch(SQLException SQLErr){
System.out.println("Error "+SQLErr);
}


}


@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
try {
return rsmd.getColumnCount();
} catch (SQLException e) {
return 0;
}
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}

@Override
public String getColumnName(int column) {
try {
return rsmd.getColumnName(column + 1);
} catch (SQLException e) {
return null;
}
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return !getColumnName(columnIndex).equalsIgnoreCase("id");
}
int colmunNameToIndex(String colmunName){
for (int i = 0; i < getColumnCount(); i++) {
if(getColumnName(i).equalsIgnoreCase(colmunName)){
return i;
}
}
return -1;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
int id = (int) data.get(rowIndex)[colmunNameToIndex("id")];
String titre = (String) data.get(rowIndex)[colmunNameToIndex("titre")];
Date dateF = (Date) data.get(rowIndex)[colmunNameToIndex("DateF")];
String lieu = (String) data.get(rowIndex)[colmunNameToIndex("lieu")];
boolean certification = (boolean) data.get(rowIndex)[colmunNameToIndex("certification")];
switch (getColumnName(columnIndex)){
case "titre": titre = (String) aValue;
break;
case "datef": dateF = (Date) aValue;
break;
case "lieu": lieu = (String) aValue;
break;
case "certification": certification = (boolean) aValue;

}
dao.updateFormation(new Formation(id, titre, dateF, lieu, certification));
data.get(rowIndex)[columnIndex] = aValue;

}

public void insertFormation(int id, String titre, Date dateF, String lieu, boolean certification){
this.dao.addFormation(new Formation(id, titre, dateF, lieu,certification));
data.add(new Object[]{id, titre, dateF, lieu, certification});
fireTableDataChanged();
JOptionPane.showMessageDialog(null, "done");
}



public void supprimerFromation(int id){
int option = JOptionPane.showConfirmDialog(null, "est ce que vous etes sure ?");
if(option == JOptionPane.NO_OPTION || option == JOptionPane.CANCEL_OPTION){
JOptionPane.showMessageDialog(null, "annulé");
return;
}

this.dao.deleteFormation(id);
for(int i = 0; i < data.size(); i++){
if((int)data.get(i)[colmunNameToIndex("id")] == id){
data.remove(i);
fireTableDataChanged();
JOptionPane.showMessageDialog(null, "done");
return;
}
}

}
}
67 changes: 67 additions & 0 deletions src/ExercicesTP/IHM/IHMAffichageFormation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ExercicesTP.IHM;

import javax.swing.*;
import ExercicesTP.CRUD.FormationDAO;
import ExercicesTP.Helpers.TableModel;

public class IHMAffichageFormation extends JInternalFrame {
private JLabel idLabel, titleLabel, dateLabel, lieuLabel, certificationLabel;
private JTextField idField, titleField, dateTextField, lieuTextField;
private JCheckBox certificationCheckbox;
private JButton rechercheButton, cancelButton, modifierButton, supprimerButton;
public FormationDAO formationDAO;
public TableModel model;
JTable jt_Formation;


IHMAffichageFormation(FormationDAO dao){
setTitle("Affichage des formations");
setSize(400, 400);
setVisible(true);
formationDAO = dao;
initilizeComponents();
createLayout();
//addEventListeners();

}
public void initilizeComponents() {
idLabel = new JLabel("ID :");
titleLabel = new JLabel("Titre :");
dateLabel = new JLabel("Date :");
lieuLabel = new JLabel("Lieu :");
certificationLabel = new JLabel("Certification :");

jt_Formation = new JTable();
String rq = "SELECT * FROM FORMATION;";
model = new TableModel(formationDAO.selection(rq), formationDAO);
jt_Formation.setModel(model);


idField = new JTextField(15);
titleField = new JTextField(20);

// Sample data for comboboxes (replace with actual data source)
dateTextField = new JTextField(15);
lieuTextField = new JTextField(15);

certificationCheckbox = new JCheckBox();

rechercheButton = new JButton("Rechercher");
modifierButton = new JButton("Modifier");
supprimerButton = new JButton("Supprimer");
cancelButton = new JButton("Annuler");
}
public void createLayout() {
JPanel panel = new JPanel();
panel.add(rechercheButton);
panel.add(modifierButton);
panel.add(supprimerButton);
panel.add(cancelButton);
panel.add(jt_Formation);

this.add(panel);


}

}
24 changes: 14 additions & 10 deletions src/ExercicesTP/IHM/IHMAjoutFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import ExercicesTP.CRUD.FormationDAO;
import ExercicesTP.Formation;

public class IHMAjoutFormation extends JFrame {
public class IHMAjoutFormation extends JInternalFrame {

private JLabel referenceLabel, titleLabel, dateLabel, lieuLabel, certificationLabel;
private JTextField referenceField, titleField;
private JTextField dateTextField, lieuTextField;
private JCheckBox certificationCheckbox;
private JButton addButton, cancelButton;
public FormationDAO formationDAO;
public IHMAjoutFormation() {
public IHMAjoutFormation(FormationDAO dao) {
super("Ajout d'une formation");
initializeComponents();
createLayout();
addEventListeners();
formationDAO = new FormationDAO();
formationDAO = dao;
this.setVisible(true);
}

private void initializeComponents() {
public void initializeComponents() {
referenceLabel = new JLabel("Référence :");
titleLabel = new JLabel("Titre :");
dateLabel = new JLabel("Date :");
Expand All @@ -37,7 +37,7 @@ private void initializeComponents() {
titleField = new JTextField(20);

// Sample data for comboboxes (replace with actual data source)
dateTextField = new JTextField(15);
dateTextField = new JTextField(15);
lieuTextField = new JTextField(15);

certificationCheckbox = new JCheckBox();
Expand Down Expand Up @@ -74,7 +74,6 @@ private void createLayout() {

getContentPane().add(panel);
setSize(500,250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Expand Down Expand Up @@ -106,15 +105,20 @@ private void addEventListeners() {
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
String lieu = lieuTextField.getText();
boolean certification = certificationCheckbox.isSelected();
formationDAO.addFormation(new Formation(reference, title, sqlStartDate, lieu, certification));

try {
formationDAO.addFormation(new Formation(reference, title, sqlStartDate, lieu, certification));
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Erreur lors de l'ajout de la formation");
throw new RuntimeException(ex);
}
JOptionPane.showMessageDialog(this, "Formation ajoutée avec succès");
dispose();
});

cancelButton.addActionListener(e -> dispose());
}

public static void main(String[] args) {
new IHMAjoutFormation();
}


}
35 changes: 30 additions & 5 deletions src/ExercicesTP/IHM/IHMRechercheFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
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;
private JTextField dateTextField, lieuTextField;
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);
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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()));
Expand Down Expand Up @@ -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());
}
});
}


Expand Down
Loading

0 comments on commit e979d6a

Please sign in to comment.