Package View

Source Code of View.Main

package View;

import Modele.FeuilleNote;
import Modele.GestionPromotion;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.mail.MessagingException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
*
* @author Tiphaine TEYSSIER
* @author Tan Phong HUYNH
*
* @version 1.0
*
*
* CNote 20/20
*
* Programme enseignant permettant de crypté, authentification et assurer l'intégrité de note scolaire
* et de les envoyer par e-mail.
*
* Projet réalisé dans le cadre de notre master 1 en 2013.
*
*
*/
public class Main extends javax.swing.JFrame {

        // variable pour quand un thread fait un travail il l'incremente et lorsque on quit lapplication on verifie quel soit a 0
        private static volatile int canQuit = 0;

        /**
         * Creates new form Main
         */
        public Main() {
                // On créer les instance qui vont charger les parametre
                Config.Config.getInstance();
                GestionPromotion.getInstance();
                try {
                        setIconImage(ImageIO.read(getClass().getResource("/View/ico.png")));
                } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                initComponents();
        }

        /**
         * 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() {
                bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

                fileChooser = new javax.swing.JFileChooser();
                jLabel2 = new javax.swing.JLabel();
                tabpane = new javax.swing.JTabbedPane();
                toolbar = new javax.swing.JToolBar();
                jLabel1 = new javax.swing.JLabel();
                menuBar = new javax.swing.JMenuBar();
                fileMenu = new javax.swing.JMenu();
                newMenuItem = new javax.swing.JMenuItem();
                openMenuItem = new javax.swing.JMenuItem();
                saveMenuItem = new javax.swing.JMenuItem();
                saveAsMenuItem = new javax.swing.JMenuItem();
                sendEmailMenuItem = new javax.swing.JMenuItem();
                zipMenuItem = new javax.swing.JMenuItem();
                closeMenuItem = new javax.swing.JMenuItem();
                exitMenuItem = new javax.swing.JMenuItem();
                toolsMenu = new javax.swing.JMenu();
                gestion_promosMenuItem = new javax.swing.JMenuItem();
                optionMenuItem = new javax.swing.JMenuItem();
                helpMenu = new javax.swing.JMenu();
                contentsMenuItem = new javax.swing.JMenuItem();
                aboutMenuItem = new javax.swing.JMenuItem();

                fileChooser.setFileFilter(new FileNameExtensionFilter("Fichier fnote", "fnote"));

                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/View/ico.png"))); // NOI18N
                jLabel2.setText("jLabel2");

                setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
                setTitle("CNote 20/20 - Envoyer les notes de vos étudiants en toute sécurité");
                addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent evt) {
                                formWindowClosing(evt);
                        }
                });

                tabpane.addChangeListener(new javax.swing.event.ChangeListener() {
                        public void stateChanged(javax.swing.event.ChangeEvent evt) {
                                tabpaneStateChanged(evt);
                        }
                });

                toolbar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
                toolbar.setFloatable(false);
                toolbar.add(jLabel1);

                fileMenu.setMnemonic('f');
                fileMenu.setText("Fichier");

                newMenuItem.setMnemonic('n');
                newMenuItem.setText("Nouveau");
                newMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                newMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(newMenuItem);

                openMenuItem.setMnemonic('o');
                openMenuItem.setText("Ouvrir");
                openMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                openMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(openMenuItem);

                saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
                saveMenuItem.setMnemonic('s');
                saveMenuItem.setText("Sauvegarder");

                org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tabpane, org.jdesktop.beansbinding.ELProperty.create("${selectedIndex!=-1}"), saveMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
                bindingGroup.addBinding(binding);

                saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                saveMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(saveMenuItem);

                saveAsMenuItem.setMnemonic('a');
                saveAsMenuItem.setText("Sauvegarder sous");

                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tabpane, org.jdesktop.beansbinding.ELProperty.create("${selectedIndex!=-1}"), saveAsMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
                bindingGroup.addBinding(binding);

                saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                saveAsMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(saveAsMenuItem);

                sendEmailMenuItem.setMnemonic('e');
                sendEmailMenuItem.setText("Envoyer email");

                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tabpane, org.jdesktop.beansbinding.ELProperty.create("${selectedIndex!=-1}"), sendEmailMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"), "");
                bindingGroup.addBinding(binding);

                sendEmailMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                sendEmailMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(sendEmailMenuItem);

                zipMenuItem.setMnemonic('c');
                zipMenuItem.setText("Créer archive");

                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tabpane, org.jdesktop.beansbinding.ELProperty.create("${selectedIndex!=-1}"), zipMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
                bindingGroup.addBinding(binding);

                zipMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                zipMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(zipMenuItem);

                closeMenuItem.setMnemonic('f');
                closeMenuItem.setText("Fermer");

                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tabpane, org.jdesktop.beansbinding.ELProperty.create("${selectedIndex!=-1}"), closeMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
                bindingGroup.addBinding(binding);

                closeMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                closeMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(closeMenuItem);

                exitMenuItem.setMnemonic('q');
                exitMenuItem.setText("Quitter");
                exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                exitMenuItemActionPerformed(evt);
                        }
                });
                fileMenu.add(exitMenuItem);

                menuBar.add(fileMenu);

                toolsMenu.setMnemonic('o');
                toolsMenu.setText("Outils");

                gestion_promosMenuItem.setMnemonic('g');
                gestion_promosMenuItem.setText("Gestion promotion");
                gestion_promosMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                gestion_promosMenuItemActionPerformed(evt);
                        }
                });
                toolsMenu.add(gestion_promosMenuItem);

                optionMenuItem.setMnemonic('o');
                optionMenuItem.setText("Options");
                optionMenuItem.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                optionMenuItemActionPerformed(evt);
                        }
                });
                toolsMenu.add(optionMenuItem);

                menuBar.add(toolsMenu);

                helpMenu.setMnemonic('a');
                helpMenu.setText("Aide");
                helpMenu.setEnabled(false);

                contentsMenuItem.setMnemonic('c');
                contentsMenuItem.setText("Contents");
                helpMenu.add(contentsMenuItem);

                aboutMenuItem.setMnemonic('a');
                aboutMenuItem.setText("About");
                helpMenu.add(aboutMenuItem);

                menuBar.add(helpMenu);

                setJMenuBar(menuBar);

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(toolbar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                        .addComponent(tabpane, javax.swing.GroupLayout.Alignment.TRAILING)
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(tabpane, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                );

                bindingGroup.bind();

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed

            if (canQuit == 0) {
                    System.exit(0);
            } else {
                    JOptionPane.showMessageDialog(this, "Veuillez attendre la fin des actions en cours avant de quitter", "Wait please", JOptionPane.INFORMATION_MESSAGE);
            }

    }//GEN-LAST:event_exitMenuItemActionPerformed

        private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed


                FeuilleNote f = ((FeuilleNoteView) getTabPane().getSelectedComponent()).getFeuilleModel();

                // test si le feuille est presente sur le disque dur
                if (f.getPath() != null) {
                        f.save(f.getPath());
                } else {
                        saveAsMenuItemActionPerformed(evt);
                }
        }//GEN-LAST:event_saveMenuItemActionPerformed

        private boolean checkIfSmtpParamAreSet() {
                Config.Config cfg = Config.Config.getInstance();
                if (cfg.getNomProf() == null
                        || cfg.getNomProf().isEmpty()
                        || cfg.getPrenomProf() == null
                        || cfg.getPrenomProf().isEmpty()
                        || cfg.getEmailProf() == null
                        || cfg.getEmailProf().isEmpty()
                        || cfg.getSMTP_serv() == null
                        || cfg.getSMTP_serv().isEmpty()
                        || cfg.getSMTP_port() == null
                        || cfg.getSMTP_port().isEmpty()) {

                        return false;
                }
                return true;
        }

        private boolean checkIfKeyParamAreSet() {
                Config.Config cfg = Config.Config.getInstance();
                if (cfg.getNomProf() == null
                        || cfg.getNomProf().isEmpty()
                        || cfg.getPrenomProf() == null
                        || cfg.getPrenomProf().isEmpty()
                        || cfg.getEmailProf() == null
                        || cfg.getEmailProf().isEmpty()
                        || cfg.getPathCertificat() == null
                        || cfg.getPathCertificat().isEmpty()
                        || cfg.getPathPrivateKey() == null
                        || cfg.getPathPrivateKey().isEmpty()) {

                        return false;
                }
                return true;
        }

        private void sendEmailMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendEmailMenuItemActionPerformed

                // verifie que les parametre son bien renseigné
                if (!checkIfSmtpParamAreSet() || !checkIfKeyParamAreSet()) {
                        JOptionPane.showMessageDialog(this, "Veuillez configurer le logiciel avant !", "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                }

                // TODO : gerer la bar de status individuelement pour chaque tab
                final FeuilleNote f = ((FeuilleNoteView) getTabPane().getSelectedComponent()).getFeuilleModel();



                f.setMessage(JOptionPane.showInputDialog(this, "Entrez un message à ajouter exemple : Note du TP1"));
                if (f.getMessage() != null) {
                        Thread t = new Thread() {
                                @Override
                                public void run() {
                                        canQuit++;
                                        jLabel1.setText(" Envoi en cours ...");
                                        try {
                                                f.sendNotes();
                                        } catch (MessagingException ex) {
                                                JOptionPane.showMessageDialog(null, "Erreur lors de l'envoie du mail. Vérifié vos paramètres SMTP", "Error email", JOptionPane.ERROR_MESSAGE);
                                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                        jLabel1.setText("");
                                        canQuit--;
                                }
                        };
                        t.start();
                }


        }//GEN-LAST:event_sendEmailMenuItemActionPerformed

        // Menu open file
        private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
                if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                        // On recupere le fichier et on l'ouvre dans un nouveau tab
                        tabpane.add(fileChooser.getSelectedFile().getName(), new FeuilleNoteView(fileChooser.getSelectedFile().getAbsolutePath()));
                        tabpane.setSelectedIndex(tabpane.getTabCount() - 1);
                }

        }//GEN-LAST:event_openMenuItemActionPerformed

        //TODO : gerer la sauvegarde : quand on a des fichier pas enregistrer demander l'enregistrement changer le nom du tab metre letoile etc
        private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
                if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                        // On recupere le path et on l'enregistre creation
                        ((FeuilleNoteView) getTabPane().getSelectedComponent()).getFeuilleModel().save(fileChooser.getSelectedFile().getAbsolutePath());

                }
        }//GEN-LAST:event_saveAsMenuItemActionPerformed

        private void closeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeMenuItemActionPerformed
                getTabPane().remove(getTabPane().getSelectedComponent());
        }//GEN-LAST:event_closeMenuItemActionPerformed

        private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
                NewFile nf = new NewFile(this, true);
                nf.setVisible(true);
                if (nf.getReturnStatus() == NewFile.RET_OK) {
                        tabpane.add(nf.getPromotionSelected().getLibelle(), new FeuilleNoteView(nf.getPromotionSelected()));
                        tabpane.setSelectedIndex(tabpane.getTabCount() - 1);
                }

        }//GEN-LAST:event_newMenuItemActionPerformed

        // Permet de notifier que la proprieter du pane a etait modifier (utile pour les bind qui utilise cette proprieté)
        private void tabpaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabpaneStateChanged
                tabpane.firePropertyChange("selectedIndex", tabpane.getSelectedIndex() - 1, tabpane.getSelectedIndex());
        }//GEN-LAST:event_tabpaneStateChanged

        private void gestion_promosMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gestion_promosMenuItemActionPerformed
                GestionPromoView v = new GestionPromoView();
                v.setVisible(true);
        }//GEN-LAST:event_gestion_promosMenuItemActionPerformed

        private void optionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optionMenuItemActionPerformed
                LinkedList<OptionIterface> options = new LinkedList<OptionIterface>();
                options.add(new OptionSMTP());
                options.add(new OptionCertificatClePrivee());
                OptionView optionView = new OptionView(this, true, options);
                optionView.setVisible(true);
        }//GEN-LAST:event_optionMenuItemActionPerformed

        private void zipMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zipMenuItemActionPerformed
                 // verifie que les parametre son bien renseigné
                if (!checkIfKeyParamAreSet()) {
                        JOptionPane.showMessageDialog(this, "Veuillez configurer le logiciel avant !", "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                }
                final FeuilleNote f = ((FeuilleNoteView) getTabPane().getSelectedComponent()).getFeuilleModel();
                f.setMessage(JOptionPane.showInputDialog(this, "Entrez un message à ajouter exemple : Note du TP1"));
                if (f.getMessage() != null) {
                        final JFileChooser fc = new JFileChooser();
                        fc.setSelectedFile(new File("archive.zip"));
                        if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                                Thread t = new Thread() {
                                        @Override
                                        public void run() {
                                                canQuit++;
                                                jLabel1.setText(" Creation de l'archive en cours ...");

                                                f.createZip(fc.getSelectedFile().getAbsolutePath());

                                                jLabel1.setText("");
                                                canQuit--;
                                        }
                                };
                                t.start();
                        }

                }
        }//GEN-LAST:event_zipMenuItemActionPerformed

        private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
              
                exitMenuItemActionPerformed(null);
        }//GEN-LAST:event_formWindowClosing

        public JTabbedPane getTabPane() {
                return this.tabpane;
        }

        /**
         * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                }
                //</editor-fold>

                /* Create and display the form */
                java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                new Main().setVisible(true);
                        }
                });
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JMenuItem aboutMenuItem;
        private javax.swing.JMenuItem closeMenuItem;
        private javax.swing.JMenuItem contentsMenuItem;
        private javax.swing.JMenuItem exitMenuItem;
        private javax.swing.JFileChooser fileChooser;
        private javax.swing.JMenu fileMenu;
        private javax.swing.JMenuItem gestion_promosMenuItem;
        private javax.swing.JMenu helpMenu;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JMenuBar menuBar;
        private javax.swing.JMenuItem newMenuItem;
        private javax.swing.JMenuItem openMenuItem;
        private javax.swing.JMenuItem optionMenuItem;
        private javax.swing.JMenuItem saveAsMenuItem;
        private javax.swing.JMenuItem saveMenuItem;
        private javax.swing.JMenuItem sendEmailMenuItem;
        private javax.swing.JTabbedPane tabpane;
        private javax.swing.JToolBar toolbar;
        private javax.swing.JMenu toolsMenu;
        private javax.swing.JMenuItem zipMenuItem;
        private org.jdesktop.beansbinding.BindingGroup bindingGroup;
        // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of View.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.