Skip to content

Commit

Permalink
added the formation filter
Browse files Browse the repository at this point in the history
  • Loading branch information
saaya-code committed Mar 22, 2024
1 parent e979d6a commit 16fa81d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
61 changes: 32 additions & 29 deletions src/ExercicesTP/Helpers/TableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Objects;

public class TableModel extends AbstractTableModel {
ArrayList<Object[]> data;
Expand All @@ -22,11 +23,7 @@ public TableModel(ResultSet rs, FormationDAO dao){
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);
}
Expand All @@ -36,7 +33,6 @@ public TableModel(ResultSet rs, FormationDAO dao){
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);
}
Expand All @@ -47,6 +43,27 @@ public TableModel(ResultSet rs, FormationDAO dao){

}

public void updateTableWithNewResultSet(ResultSet rs){
data = new ArrayList<Object[]>();
try {
rsmd = rs.getMetaData();
} 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);
}
data.add(ligne);
}
}catch(SQLException SQLErr){
System.out.println("Error "+SQLErr);
}
fireTableDataChanged();
}


@Override
public int getRowCount() {
Expand Down Expand Up @@ -78,7 +95,7 @@ public String getColumnName(int column) {

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return !getColumnName(columnIndex).equalsIgnoreCase("id");
return false;
}
int colmunNameToIndex(String colmunName){
for (int i = 0; i < getColumnCount(); i++) {
Expand All @@ -88,39 +105,25 @@ int colmunNameToIndex(String colmunName){
}
return -1;
}
Boolean stringToBool(String str){
return str.equalsIgnoreCase("true");
}

@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");
data.add(new Object[]{id, titre, dateF, lieu, certification});
fireTableDataChanged();
JOptionPane.showMessageDialog(null, "done");
}



public void supprimerFromation(int id){
public void supprimerFormation(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é");
Expand Down
65 changes: 56 additions & 9 deletions src/ExercicesTP/IHM/IHMAffichageFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ExercicesTP.CRUD.FormationDAO;
import ExercicesTP.Helpers.TableModel;

import java.awt.*;

public class IHMAffichageFormation extends JInternalFrame {
private JLabel idLabel, titleLabel, dateLabel, lieuLabel, certificationLabel;
private JTextField idField, titleField, dateTextField, lieuTextField;
Expand All @@ -21,7 +23,7 @@ public class IHMAffichageFormation extends JInternalFrame {
formationDAO = dao;
initilizeComponents();
createLayout();
//addEventListeners();
addEventListeners();

}
public void initilizeComponents() {
Expand All @@ -40,7 +42,6 @@ public void initilizeComponents() {
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);

Expand All @@ -52,16 +53,62 @@ public void initilizeComponents() {
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);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel panel2 = new JPanel(new GridBagLayout());

// Add labels and corresponding components
c.fill = GridBagConstraints.HORIZONTAL;
//addComponent(panel, idLabel, c, 0, 0, 1, 1);
//addComponent(panel, idField, c, 1, 0, 2, 1);
addComponent(panel, titleLabel, c, 0, 1, 1, 1);
addComponent(panel, titleField, c, 1, 1, 2, 1);
addComponent(panel, dateLabel, c, 0, 2, 1, 1);
addComponent(panel, dateTextField, c, 1, 2, 2, 1);
addComponent(panel, lieuLabel, c, 0, 3, 1, 1);
addComponent(panel, lieuTextField, c, 1, 3, 2, 1);
addComponent(panel, certificationLabel, c, 0, 4, 1, 1);
addComponent(panel, certificationCheckbox, c, 1, 4, 1, 1);
addComponent(panel2, rechercheButton, c, 5, 1, 1, 1);
addComponent(panel2, modifierButton, c, 5, 2, 1, 1);
addComponent(panel2, supprimerButton, c, 5, 3, 1, 1);
addComponent(panel2, cancelButton, c, 5, 4, 1, 1);

// Add buttons
getContentPane().add(panel);
getContentPane().add(panel2);
getContentPane().setLayout(new FlowLayout());

this.add(panel);
setSize(500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JScrollPane scrollPane = new JScrollPane(jt_Formation);
this.add(scrollPane, BorderLayout.CENTER);


}
private void addComponent(JPanel panel, JComponent component, GridBagConstraints c, int x, int y, int width, int height) {
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
panel.add(component, c);
}
private void addEventListeners(){
rechercheButton.addActionListener((e)->{
String rq = "SELECT * FROM FORMATION WHERE ";
if(!titleField.getText().isEmpty()){
rq += "titre = '"+titleField.getText()+"' AND ";
}
if(!dateTextField.getText().isEmpty()){
rq += "datef = '"+dateTextField.getText()+"' AND ";
}
if(!lieuTextField.getText().isEmpty()){
rq += "lieu = '"+lieuTextField.getText()+"' AND ";
}
rq = rq.substring(0, rq.length()-4);
model.updateTableWithNewResultSet(formationDAO.selection(rq));
});
}

}
1 change: 0 additions & 1 deletion src/TP_Base/GestionEtudiant.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.awt.event.MouseEvent;
import java.sql.ResultSet;

import static java.lang.Integer.parseInt;

public class GestionEtudiant extends JFrame {
JPanel northPannel;
Expand Down

0 comments on commit 16fa81d

Please sign in to comment.