/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUIS; import CDB.Movies; import DB.CouchDbAccess; import Helpers.SwingUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JDialog; import javax.swing.JLabel; /** * * @author Greenlamp */ public class Selection extends javax.swing.JDialog { /** * Creates new form Selection */ private LinkedList<String[]> listeCriteres = null; private Set<String> listeId = null; private CouchDbAccess couch = null; private LinkedList<Movies> listeMovie = null; JDialog fenetre = null; private java.awt.Frame parent = null; public Selection(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } Selection(java.awt.Frame parent, boolean modal, LinkedList<String[]> liste) { super(parent, modal); initComponents(); this.parent = parent; couch = new CouchDbAccess(); listeCriteres = liste; fenetre = this; new Thread(){ public void run(){ listeId = couch.getListeId(listeCriteres); fenetre.setTitle("Nombre de résultat: " + String.valueOf(listeId.size())); listeMovie = new LinkedList<>(); buildListMovie(listeMovie, listeId, LnbFilms); } }.start(); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); GtableFilms = new javax.swing.JTable(); Benvoyer = new javax.swing.JButton(); Bannuler = new javax.swing.JButton(); BtoutSelect = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); LnbFilms = new javax.swing.JLabel(); BtoutDeselect = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); GtableFilms.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Identifiant", "Titre", "Classification", "Ajouter" } ) { Class[] types = new Class [] { java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class }; boolean[] canEdit = new boolean [] { false, false, false, true }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); GtableFilms.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { GtableFilmsMouseClicked(evt); } }); jScrollPane1.setViewportView(GtableFilms); GtableFilms.getColumnModel().getColumn(0).setResizable(false); GtableFilms.getColumnModel().getColumn(2).setResizable(false); GtableFilms.getColumnModel().getColumn(3).setResizable(false); Benvoyer.setText("Envoyer"); Benvoyer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BenvoyerActionPerformed(evt); } }); Bannuler.setText("Annuler"); Bannuler.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BannulerActionPerformed(evt); } }); BtoutSelect.setText("Tout sélectionner"); BtoutSelect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtoutSelectActionPerformed(evt); } }); jLabel1.setText("Nombre de films: "); LnbFilms.setText("0"); BtoutDeselect.setText("Tout Désélectionner"); BtoutDeselect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtoutDeselectActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(Benvoyer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Bannuler) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(LnbFilms) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(BtoutDeselect) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(BtoutSelect)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Benvoyer) .addComponent(Bannuler) .addComponent(BtoutSelect) .addComponent(jLabel1) .addComponent(LnbFilms) .addComponent(BtoutDeselect)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); java.awt.Dimension dialogSize = getSize(); setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2); }// </editor-fold>//GEN-END:initComponents private void BannulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BannulerActionPerformed this.dispose(); }//GEN-LAST:event_BannulerActionPerformed private void BtoutSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtoutSelectActionPerformed int size = GtableFilms.getRowCount(); for(int i=0; i<size; i++){ GtableFilms.getModel().setValueAt(true, i, 3); } }//GEN-LAST:event_BtoutSelectActionPerformed private void BtoutDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtoutDeselectActionPerformed int size = GtableFilms.getRowCount(); for(int i=0; i<size; i++){ GtableFilms.getModel().setValueAt(false, i, 3); } }//GEN-LAST:event_BtoutDeselectActionPerformed private void GtableFilmsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GtableFilmsMouseClicked if(evt.getClickCount() == 2){ int column = GtableFilms.columnAtPoint(evt.getPoint()); System.out.println("Column: " + column); if(column < 3){ int row = GtableFilms.rowAtPoint(evt.getPoint()); String key = String.valueOf(GtableFilms.getValueAt(row, 0)); Details details = new Details(parent, true, key, couch); details.show(); } } }//GEN-LAST:event_GtableFilmsMouseClicked private void BenvoyerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BenvoyerActionPerformed int size = GtableFilms.getRowCount(); LinkedList<String> listeIdEffective = new LinkedList<>(); for(int i=0; i<size; i++){ if(GtableFilms.getModel().getValueAt(i, 3) == true){ String id = GtableFilms.getModel().getValueAt(i, 0).toString(); listeIdEffective.add(id); } } Send send = new Send(parent, true, listeIdEffective, couch); send.show(); }//GEN-LAST:event_BenvoyerActionPerformed /** * @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(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { Selection dialog = new Selection(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Bannuler; private javax.swing.JButton Benvoyer; private javax.swing.JButton BtoutDeselect; private javax.swing.JButton BtoutSelect; private javax.swing.JTable GtableFilms; private javax.swing.JLabel LnbFilms; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables private Vector getVecFilm(int id, String titre, String classification) { //SwingUtils.addToTable(GtableFilms, getVecFilm(1, "olol le film", 12.5)); Vector vec = new Vector(); vec.add(id); vec.add(titre); vec.add(classification); vec.add(false); return vec; } private void buildListMovie(LinkedList<Movies> listeMovie, Set<String> listeId, JLabel nbFilm) { int cpt = 0; SortedMap<String, Map<String, String>> liste = null; File f = new File("allMovies"); if(f.exists()){ liste = new TreeMap<>(); try { FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); liste = (SortedMap<String, Map<String, String>>)ois.readObject(); ois.close(); } catch (Exception ex) { Logger.getLogger(CouchDbAccess.class.getName()).log(Level.SEVERE, null, ex); } }else{ liste = couch.getAllMovies(); try { FileOutputStream fos = new FileOutputStream(f); ObjectOutput out = new ObjectOutputStream(fos); out.writeObject(liste); out.close(); } catch (Exception ex) { Logger.getLogger(CouchDbAccess.class.getName()).log(Level.SEVERE, null, ex); } } for(String id : listeId){ String name = liste.get(id).get("name"); String certif = liste.get(id).get("certification"); Movies movie = new Movies(); movie.setId(Integer.parseInt(id)); movie.setName(name); movie.setCertification(certif); listeMovie.add(movie); SwingUtils.addToTable(GtableFilms, getVecFilm(movie.getId(), movie.getName(), movie.getCertification())); cpt++; nbFilm.setText(String.valueOf(cpt)); } } }