Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package GUI; import dao.UserDao; import entite.User; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.time.LocalDate; import java.util.ArrayList; import static java.util.Collections.list; import java.util.List; import javax.swing.JCheckBox; import GUI.UserModel; import java.awt.Color; import java.util.Locale; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SingleSelectionModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import utils.Statestique; import utils.*; /** * * @author maroo */ public class Framedashboard extends javax.swing.JFrame { /** * Creates new form UserForm */ public Framedashboard() { initComponents(); stat(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); tfusername = new javax.swing.JTextField(); tfemail = new javax.swing.JTextField(); tflastname = new javax.swing.JTextField(); tffirstname = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); chbenabled = new javax.swing.JCheckBox(); cboxgender = new javax.swing.JComboBox(); jLabel8 = new javax.swing.JLabel(); tfphone = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); cbrole = new javax.swing.JComboBox(); jToggleButton4 = new javax.swing.JToggleButton(); tfbonus = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); btnSup = new javax.swing.JButton(); btnupdate = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); taadress = new javax.swing.JTextArea(); jPanel3 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); jPanel5 = new javax.swing.JPanel(); jPanel6 = new javax.swing.JPanel(); jPanel7 = new javax.swing.JPanel(); jLabel11 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tfusername.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfusernameActionPerformed(evt); } }); jLabel2.setText("Nom D'utilisateur :"); jLabel3.setText("Email :"); jLabel5.setText("Genre :"); jLabel6.setText("Prenom :"); chbenabled.setText("Activer"); chbenabled.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { chbenabledStateChanged(evt); } }); chbenabled.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { chbenabledActionPerformed(evt); } }); cboxgender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Homme", "Femme" })); cboxgender.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cboxgenderActionPerformed(evt); } }); jLabel8.setText("Nom :"); jLabel9.setText("N Tel"); jLabel7.setText("Role"); cbrole.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "ROLE_ADMIN", "ROLE_USER", "ROLE_FOURNISSEUR", " " })); jToggleButton4.setText("Annuler"); jToggleButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton4ActionPerformed(evt); } }); tfbonus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfbonusActionPerformed(evt); } }); jLabel10.setText("Bonus :"); jTable1.setModel(new UserModel()); jTable1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTable1MouseClicked(evt); } }); jScrollPane2.setViewportView(jTable1); btnSup.setText("Supprimer"); btnSup.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSupActionPerformed(evt); } }); btnupdate.setText("Modifier"); btnupdate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnupdateActionPerformed(evt); } }); jLabel1.setText("Adresse"); taadress.setColumns(20); taadress.setRows(5); taadress.setPreferredSize(new java.awt.Dimension(130, 94)); jScrollPane1.setViewportView(taadress); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(21, 21, 21).addComponent(btnupdate) .addGap(18, 18, 18).addComponent(btnSup).addGap(18, 18, 18) .addComponent(jToggleButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(888, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup().addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel3).addComponent(jLabel5) .addComponent(jLabel8).addComponent(jLabel6).addComponent(jLabel9) .addComponent(jLabel7).addComponent(jLabel10).addComponent(jLabel1)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(chbenabled) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jScrollPane1).addComponent(tfemail) .addComponent(tfusername) .addComponent(cboxgender, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(tflastname).addComponent(tfbonus) .addComponent(cbrole, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(tfphone).addComponent(tffirstname)).addGap(83, 83, 83) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 649, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE))))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tfusername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfemail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(36, 36, 36) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cboxgender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tflastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tffirstname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfphone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel7)) .addComponent(cbrole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tfbonus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29).addComponent(chbenabled)))) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jToggleButton4).addComponent(btnSup).addComponent(btnupdate)) .addContainerGap())); jTabbedPane1.addTab("Gestion Utilisateur ", jPanel1); jPanel4.setLayout(null); jPanel5.setLayout(new java.awt.BorderLayout()); jPanel4.add(jPanel5); jPanel5.setBounds(10, 0, 540, 280); jPanel6.setLayout(new java.awt.BorderLayout()); jPanel4.add(jPanel6); jPanel6.setBounds(600, 0, 570, 280); jPanel7.setLayout(new java.awt.BorderLayout()); jPanel4.add(jPanel7); jPanel7.setBounds(10, 290, 540, 220); jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bg.jpg"))); // NOI18N jPanel4.add(jLabel11); jLabel11.setBounds(0, 0, 1180, 520); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jTabbedPane1.addTab("Statestique", jPanel3); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void chbenabledActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbenabledActionPerformed // TODO add your handling code here: }//GEN-LAST:event_chbenabledActionPerformed private void cboxgenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxgenderActionPerformed // TODO add your handling code here: }//GEN-LAST:event_cboxgenderActionPerformed private void tfusernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfusernameActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfusernameActionPerformed private void chbenabledStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chbenabledStateChanged // TODO add your handling code here: }//GEN-LAST:event_chbenabledStateChanged private void tfbonusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfbonusActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfbonusActionPerformed private void btnSupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSupActionPerformed UserDao udao = new UserDao(); try { int i = jTable1.getSelectedRow(); int s = (int) jTable1.getModel().getValueAt(i, 0); System.out.println(s); udao.removeById(s); } catch (ArrayIndexOutOfBoundsException exp) { JOptionPane.showMessageDialog(null, "Veuillez selectionner un Utilisateur supprimer"); } }//GEN-LAST:event_btnSupActionPerformed private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnupdateActionPerformed User u = new User(); UserDao udao = new UserDao(); int i = jTable1.getSelectedRow(); int s = (int) jTable1.getModel().getValueAt(i, 0); u.setId(s); u.setUsername(tfusername.getText()); u.setUsernameCanonical(tfusername.getText()); u.setEmail(tfemail.getText()); u.setEmailCanonical(tfemail.getText()); // u.setPassword(tfmdp.getText()); if (chbenabled.isSelected()) u.setEnabled(1); else { u.setEnabled(0); } if (cboxgender.getSelectedIndex() == 0) { u.setGender("m"); } else { u.setGender("f"); } u.setPhone(tfphone.getText()); u.setLastname(tflastname.getText()); u.setFirstname(tffirstname.getText()); if (cbrole.getSelectedIndex() == 0) { u.setRoles("ROLE_ADMIN"); } if (cbrole.getSelectedIndex() == 1) { u.setRoles("ROLE_USER"); } else { u.setRoles("ROLE_FOURNISSEUR"); } try { u.setBonus(Integer.parseInt(tfbonus.getText())); } catch (NumberFormatException ex) { System.out.println("erreur" + ex); } java.sql.Date d = java.sql.Date.valueOf(LocalDate.now()); u.setUpdated_at(d); u.setAdress(taadress.getText()); udao.update(u); jTable1.setModel(new UserModel()); // TODO add your handling code here: }//GEN-LAST:event_btnupdateActionPerformed private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked UserDao udao = new UserDao(); User u = new User(); int i = jTable1.getSelectedRow(); int s = (int) jTable1.getModel().getValueAt(i, 0); u = udao.findById(s); tfusername.setText(u.getUsername()); tfemail.setText(u.getEmail()); //tfmdp.setText(u.getPassword()); //tfemail.setText(u.getEmail()); tffirstname.setText(u.getFirstname()); tflastname.setText(u.getLastname()); tfphone.setText(u.getPhone()); //tfemail.setText(u.getEmail()); tfbonus.setText("" + (u.getBonus()));// TODO add your handling code here: }//GEN-LAST:event_jTable1MouseClicked private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton4ActionPerformed tfusername.setText(""); tfemail.setText(""); //tfmdp.setText(""); //tfemail.setText(u.getEmail()); tffirstname.setText(""); tflastname.setText(""); tfphone.setText(""); //tfemail.setText(u.getEmail()); tfbonus.setText(""); // TODO add your handling code here: }//GEN-LAST:event_jToggleButton4ActionPerformed public void stat() { Statestique st = new Statestique(); int u = st.nbUser(); int f = st.nbfemme(); int h = u - f; int p = st.nbProduit(); int s = st.nbService(); int c = st.nbComment(); int pp = st.nbPauP(); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(s, "", "Service"); dataset.setValue(p, "", "Produit"); JFreeChart chart = ChartFactory.createBarChart("Service et Produit", null, null, dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot cp = chart.getCategoryPlot(); // cp.setRangeGridlinePaint(Color.BLACK); ChartPanel chp = new ChartPanel(chart); jPanel5.removeAll(); jPanel5.add(chp); jPanel5.validate(); DefaultCategoryDataset ds = new DefaultCategoryDataset(); ds.setValue(pp, "", "Commande"); ds.setValue(c, "", "Commentaire"); JFreeChart cht = ChartFactory.createBarChart("Graphe de Commentaire & Commande", null, null, ds, PlotOrientation.VERTICAL, false, false, false); CategoryPlot catp = chart.getCategoryPlot(); // catp.setRangeGridlinePaint(Color.BLACK); ChartPanel chartp = new ChartPanel(cht); jPanel6.removeAll(); jPanel6.add(chartp); jPanel6.validate(); DefaultPieDataset pds = new DefaultPieDataset(); pds.setValue("Femme", new Integer(f)); pds.setValue("Homme", new Integer(h)); JFreeChart chartpie = ChartFactory.createPieChart("Genre", pds, true, true, false); PiePlot plot = (PiePlot) chartpie.getPlot(); ChartPanel chartpiepanel = new ChartPanel(chartpie); jPanel7.removeAll(); jPanel7.add(chartpiepanel); jPanel7.validate(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Framedashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Framedashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Framedashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Framedashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Framedashboard().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnSup; private javax.swing.JButton btnupdate; private javax.swing.JComboBox cboxgender; private javax.swing.JComboBox cbrole; private javax.swing.JCheckBox chbenabled; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JPanel jPanel7; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTable jTable1; private javax.swing.JToggleButton jToggleButton4; private javax.swing.JTextArea taadress; private javax.swing.JTextField tfbonus; private javax.swing.JTextField tfemail; private javax.swing.JTextField tffirstname; private javax.swing.JTextField tflastname; private javax.swing.JTextField tfphone; private javax.swing.JTextField tfusername; // End of variables declaration//GEN-END:variables }