com.iucosoft.eavertizare.gui.SendAvertizareManualJDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.iucosoft.eavertizare.gui.SendAvertizareManualJDialog.java

Source

/*
 * 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 com.iucosoft.eavertizare.gui;

import com.iucosoft.eavertizare.dao.ClientsDaoIntf;
import com.iucosoft.eavertizare.model.Client;
import com.iucosoft.eavertizare.model.Firma;
import com.iucosoft.eavertizare.util.MyMailSender;
import com.iucosoft.eavertizare.util.MySmsSender;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author valera_
 */
public class SendAvertizareManualJDialog extends javax.swing.JDialog {

    Client client;
    Firma firma;
    ClientsDaoIntf clientsDao;
    MainJFrame parent;
    String varianta;
    private final MySmsSender smsSender;
    private final MyMailSender mailSender;
    private final ApplicationContext contextMail;

    public SendAvertizareManualJDialog(MainJFrame parent, boolean modal, ClientsDaoIntf clientsDao, Firma firma,
            Client client, String varianta) {
        setTitle("Trimite avertizare manual");
        contextMail = new ClassPathXmlApplicationContext("spring-mail.xml");
        this.client = client;
        this.firma = firma;
        this.clientsDao = clientsDao;
        this.parent = parent;
        this.varianta = varianta;
        smsSender = contextMail.getBean("mySmsSender", MySmsSender.class);
        mailSender = contextMail.getBean("myMailSender", MyMailSender.class);
        setModal(modal);
        initComponents();
        fillForm();
        setLocationRelativeTo(parent);
    }

    private void fillForm() {
        if (client != null) {
            jTextFieldNume.setText(client.getNume());
            jTextFieldPrenume.setText(client.getPrenume());
            jTextFieldFirma.setText(client.getFirma().getNumeFirma());
            jTextAreaMesaj.setText(client.getFirma().getMesajPentruClienti());
        }
        jTextFieldNume.setEditable(false);
        jTextFieldPrenume.setEditable(false);
        jTextFieldFirma.setEditable(false);
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldNume = new javax.swing.JTextField();
        jTextFieldPrenume = new javax.swing.JTextField();
        jTextFieldFirma = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMesaj = new javax.swing.JTextArea();
        jButtonSendAvertizare = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nume:");

        jLabel2.setText("Prenume:");

        jLabel3.setText("Firma:");

        jLabel4.setText("Mesaj:");

        jTextAreaMesaj.setColumns(20);
        jTextAreaMesaj.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMesaj);

        jButtonSendAvertizare.setText("send");
        jButtonSendAvertizare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendAvertizareActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 0, 51));
        jLabel5.setText("Atentie!");
        jLabel5.setToolTipText("Cuvintele: nume, prenume, data si compania. Vor fii inlocuite cu datele concrete.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButtonSendAvertizare)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextFieldFirma, javax.swing.GroupLayout.PREFERRED_SIZE, 310,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextFieldPrenume, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                310, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextFieldNume, javax.swing.GroupLayout.PREFERRED_SIZE, 310,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5))))
                        .addContainerGap(16, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(jTextFieldNume, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(jTextFieldPrenume,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(jTextFieldFirma,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(1, 1, 1).addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4).addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18).addComponent(jButtonSendAvertizare)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void jButtonSendAvertizareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSendAvertizareActionPerformed
        String mesaj = "";
        int contor = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
        String mesajClient = jTextAreaMesaj.getText().replaceFirst("nume", client.getNume())
                .replaceFirst("prenume", client.getPrenume())
                .replaceFirst("data", sdf.format(client.getDateExpirare()).toString())
                .replaceFirst("compania", client.getFirma().getNumeFirma());
        switch (varianta) {
        case "SMS AND E-MAIL":
            mesaj = "Sms si e-mail";
            try {
                mailSender.sendMail(client.getEmail(), "E-avertizare", mesajClient);
                smsSender.sendSms(client.getNrTelefon(), mesajClient);
            } catch (Exception ex) {
                //Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(new JFrame(), "Verificai conexiunea la internet!\n" + ex, "Error",
                        JOptionPane.ERROR_MESSAGE);
                contor = 1;
            }
            client.setTrimis(true);
            clientsDao.update(client.getFirma(), client);
            parent.refreshFrame();
            break;

        case "SMS":
            mesaj = "Sms";
            try {
                smsSender.sendSms(client.getNrTelefon(), mesajClient);
            } catch (Exception ex) {
                //Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(new JFrame(), "Verificai conexiunea la internet!\n" + ex, "Error",
                        JOptionPane.ERROR_MESSAGE);
                contor = 1;
            }
            break;

        case "E-MAIL":
            mesaj = "E-mail";
            try {
                mailSender.sendMail(client.getEmail(), "E-avertizare", mesajClient);
            } catch (Exception ex) {
                //Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(new JFrame(), "Verificai conexiunea la internet!\n" + ex, "Error",
                        JOptionPane.ERROR_MESSAGE);
                contor = 1;
            }
            break;
        }

        this.dispose();

        if (contor != 1) {
            JOptionPane.showMessageDialog(parent, mesaj + " transmis cu succes!", "Info",
                    JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_jButtonSendAvertizareActionPerformed

    /**
     * @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(SendAvertizareManualJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    //        } catch (InstantiationException ex) {
    //            java.util.logging.Logger.getLogger(SendAvertizareManualJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    //        } catch (IllegalAccessException ex) {
    //            java.util.logging.Logger.getLogger(SendAvertizareManualJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    //        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    //            java.util.logging.Logger.getLogger(SendAvertizareManualJDialog.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() {
    //                SendAvertizareManualJDialog dialog = new SendAvertizareManualJDialog(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 jButtonSendAvertizare;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaMesaj;
    private javax.swing.JTextField jTextFieldFirma;
    private javax.swing.JTextField jTextFieldNume;
    private javax.swing.JTextField jTextFieldPrenume;
    // End of variables declaration//GEN-END:variables
}