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

Java tutorial

Introduction

Here is the source code for com.iucosoft.eavertizare.gui.FirmaJDialog.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.dao.ConfiguratiiDaoIntf;
import com.iucosoft.eavertizare.dao.FirmaDaoIntf;
import com.iucosoft.eavertizare.gui.models.FirmaTableModel;
import com.iucosoft.eavertizare.model.Client;
import com.iucosoft.eavertizare.model.Configuratii;
import com.iucosoft.eavertizare.model.Firma;
import java.util.List;
import javax.swing.JOptionPane;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

    private Firma firma;
    private Client clients;
    private Configuratii configuratii;
    private final MainJFrame parent;
    private final FirmaDaoIntf firmaDao;
    private final ConfiguratiiDaoIntf configuratiiDao;
    private final ClientsDaoIntf clientsDao;
    private final ClassPathXmlApplicationContext contex;
    private FirmaTableModel firmaTableModel;

    public FirmaJDialog(MainJFrame parent, boolean modal, Firma firma) {
        setTitle("Adaugare sau editare firma");
        this.firma = firma;
        this.parent = parent;
        contex = new ClassPathXmlApplicationContext("spring-dao.xml");
        firmaDao = contex.getBean("firmaDao", FirmaDaoIntf.class);
        configuratiiDao = contex.getBean("configuratiiDao", ConfiguratiiDaoIntf.class);
        clientsDao = contex.getBean("clientsDao", ClientsDaoIntf.class);

        setModal(modal);
        initComponents();
        fillForm();
        setLocationRelativeTo(parent);
    }

    public FirmaJDialog(MainJFrame parent, Firma firma, boolean modal, FirmaTableModel firmaTableModel) {
        setTitle("Adaugare sau editare firma");
        this.firma = firma;
        this.parent = parent;
        this.firmaTableModel = firmaTableModel;
        contex = new ClassPathXmlApplicationContext("spring-dao.xml");
        firmaDao = contex.getBean("firmaDao", FirmaDaoIntf.class);
        configuratiiDao = contex.getBean("configuratiiDao", ConfiguratiiDaoIntf.class);
        clientsDao = contex.getBean("clientsDao", ClientsDaoIntf.class);

        setModal(modal);
        initComponents();
        fillForm();
        setLocationRelativeTo(null);
    }

    private void fillForm() {
        if (firma != null) {
            jTextFieldNumeFirma.setText(firma.getNumeFirma());
            jTextFieldAdresaFirma.setText(firma.getAdresaFirma());
            jTextAreaDescriere.setText(firma.getDescriereFirma());
            jTextAreaMesaj.setText(firma.getMesajPentruClienti());
            configuratii = firma.getConfiguratii();
        }
    }

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

        jLabelNumeFirma = new javax.swing.JLabel();
        jLabelAdresaFirma = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButtonConfiguratiFirma = new javax.swing.JButton();
        jTextFieldNumeFirma = new javax.swing.JTextField();
        jTextFieldAdresaFirma = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDescriere = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMesaj = new javax.swing.JTextArea();
        jButtonSaveFirma = new javax.swing.JButton();
        jLabelConfigurati = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelNumeFirma.setText("Denumire:");

        jLabelAdresaFirma.setText("Adresa:");

        jLabel1.setText("Descriere:");

        jLabel2.setText("Mesaj pentru clienti:");

        jButtonConfiguratiFirma.setText("...");
        jButtonConfiguratiFirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfiguratiFirmaActionPerformed(evt);
            }
        });

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

        jTextAreaMesaj.setColumns(20);
        jTextAreaMesaj.setRows(5);
        jTextAreaMesaj.setToolTipText(
                "Atentie! Scrieti nume, prenume, data si compania- ele vor fii inlocuite cu date concrete.");
        jScrollPane2.setViewportView(jTextAreaMesaj);
        jTextAreaMesaj.getAccessibleContext().setAccessibleDescription(
                "Atentie! Scrieti nume, prenume, data si compania - ele vor fii inlocuite cu date concrete.");

        jButtonSaveFirma.setText("Save");
        jButtonSaveFirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveFirmaActionPerformed(evt);
            }
        });

        jLabelConfigurati.setText("Configuratii:");

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

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 51));
        jLabel4.setText("Atentie!");
        jLabel4.setToolTipText("Denumirea nu trebuie sa contina spatii");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(
                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(jButtonSaveFirma))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabelConfigurati, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabelAdresaFirma,
                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabelNumeFirma, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jTextFieldNumeFirma)
                                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        266, Short.MAX_VALUE)
                                                .addComponent(jTextFieldAdresaFirma)
                                                .addComponent(jScrollPane1,
                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jLabel4).addComponent(jLabel3)
                                                                .addComponent(jButtonConfiguratiFirma,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 84,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGap(0, 0, Short.MAX_VALUE)))))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addComponent(jLabel4).addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabelNumeFirma).addComponent(jTextFieldNumeFirma,
                                        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(jLabelAdresaFirma).addComponent(jTextFieldAdresaFirma,
                                        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.LEADING)
                                .addComponent(jLabel1).addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(2, 2, 2).addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2).addComponent(jScrollPane2,
                                        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(jButtonConfiguratiFirma).addComponent(jLabelConfigurati))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSaveFirma)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void jButtonSaveFirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveFirmaActionPerformed
        getDataFirma();
        String word = "";
        if (firma.getId() < 1) { //save
            int idConfiguratie = firmaDao.getMaxId("configuratii_db");
            configuratii = configuratiiDao.findById(idConfiguratie);
            firma.setConfiguratii(configuratii);
            firmaDao.save(firma);
            updateDate();
            word = "adaugata";
        } else {// update
            firmaDao.update(firma);
            word = "editata";
        }
        JOptionPane.showMessageDialog(this, "Firma " + word + " cu success", "Info",
                JOptionPane.INFORMATION_MESSAGE);
        parent.refreshFrame();
        if (firmaTableModel != null) {
            firmaTableModel.refreshModel();
        }
        this.dispose();
    }//GEN-LAST:event_jButtonSaveFirmaActionPerformed

    private void updateDate() {
        int idFirma = firmaDao.getMaxId("firme");
        firma = firmaDao.findById(idFirma);
        List<Client> listClients = clientsDao.findAllClientsForFirmaRemote(firma);
        clientsDao.createTabeleClienti(firma);
        clientsDao.saveLocal(firma, listClients);
    }

    private void jButtonConfiguratiFirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfiguratiFirmaActionPerformed
        if (configuratii == null) {
            showConfiguratiJDialog(new Configuratii());
        } else {
            showConfiguratiJDialog(configuratii);
        }
    }//GEN-LAST:event_jButtonConfiguratiFirmaActionPerformed

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

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ConfiguratiJDialog dialog = new ConfiguratiJDialog(parent, true, configuratii);
                dialog.setVisible(true);
            }
        });
    }

    private void getDataFirma() {

        firma.setNumeFirma(jTextFieldNumeFirma.getText());
        firma.setAdresaFirma(jTextFieldAdresaFirma.getText());
        firma.setDescriereFirma(jTextAreaDescriere.getText());
        firma.setMesajPentruClienti(jTextAreaMesaj.getText());
        //  + "\nCu mult drag firma " + firma.getNumeFirma());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfiguratiFirma;
    private javax.swing.JButton jButtonSaveFirma;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelAdresaFirma;
    private javax.swing.JLabel jLabelConfigurati;
    private javax.swing.JLabel jLabelNumeFirma;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaDescriere;
    private javax.swing.JTextArea jTextAreaMesaj;
    private javax.swing.JTextField jTextFieldAdresaFirma;
    private javax.swing.JTextField jTextFieldNumeFirma;
    // End of variables declaration//GEN-END:variables

}