cz.moz.ctmanager.main.AddContactFrame.java Source code

Java tutorial

Introduction

Here is the source code for cz.moz.ctmanager.main.AddContactFrame.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 cz.moz.ctmanager.main;

import cz.moz.ctmanager.daos.ContactsDao;
import cz.moz.ctmanager.daos.EmailsDao;
import cz.moz.ctmanager.daos.PhonesDao;
import cz.moz.ctmanager.domain.Contact;
import javax.swing.JOptionPane;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Michal
 */
public class AddContactFrame extends javax.swing.JFrame {

    public AddContactFrame() {
    }

    @Autowired
    public ContactsDao contactsDao;

    @Autowired
    public PhonesDao phonesDao;

    @Autowired
    public EmailsDao emailsDao;

    @Autowired
    public MainAppFrame mainAppFrame;

    @Autowired
    public DetailsFrame detailsFrame;

    public Contact contact;

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public void initializeComponents() {
        initComponents();
        if (contact != null) {
            firstNameTextField.setText(contact.getFirstName());
            lastNameTextField.setText(contact.getLastName());
            addressTextField.setText(contact.getAddress());
        }
        this.setTitle("Add contact");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ApplytBTN = new javax.swing.JButton();
        firstNameTextField = new javax.swing.JTextField();
        lastNameTextField = new javax.swing.JTextField();
        addressTextField = new javax.swing.JTextField();
        cancleBTN = new javax.swing.JButton();

        ApplytBTN.setText("Apply");
        ApplytBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ApplytBTNActionPerformed(evt);
            }
        });

        firstNameTextField.setText("First Name");
        firstNameTextField.setToolTipText("");

        lastNameTextField.setText("Last Name");
        lastNameTextField.setToolTipText("");

        addressTextField.setText("Address");
        addressTextField.setToolTipText("");
        addressTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addressTextFieldActionPerformed(evt);
            }
        });

        cancleBTN.setText("Cancle");
        cancleBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancleBTNActionPerformed(evt);
            }
        });

        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)
                                .addComponent(addressTextField).addComponent(firstNameTextField)
                                .addComponent(lastNameTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(ApplytBTN, javax.swing.GroupLayout.PREFERRED_SIZE, 94,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(cancleBTN, javax.swing.GroupLayout.PREFERRED_SIZE, 94,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ApplytBTN, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cancleBTN, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void ApplytBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ApplytBTNActionPerformed
        if (contact != null) {
            contact.setFirstName(firstNameTextField.getText());
            contact.setLastName(lastNameTextField.getText());
            contact.setAddress(addressTextField.getText());
            contactsDao.editContact(contact);
            JOptionPane.showMessageDialog(this, "Contact changed");
            detailsFrame.refreshList();
            this.setVisible(false);
        } else {
            Contact newContact = new Contact();
            newContact.setFirstName(firstNameTextField.getText());
            newContact.setLastName(lastNameTextField.getText());
            newContact.setAddress(addressTextField.getText());
            contactsDao.saveContact(newContact);
            JOptionPane.showMessageDialog(this, "Contact added");
            mainAppFrame.refreshList();
            this.setVisible(false);
        }

    }//GEN-LAST:event_ApplytBTNActionPerformed

    private void addressTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addressTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_addressTextFieldActionPerformed

    private void cancleBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancleBTNActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancleBTNActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ApplytBTN;
    private javax.swing.JTextField addressTextField;
    private javax.swing.JButton cancleBTN;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JTextField lastNameTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @param contact the contact to set
     */

}