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

Java tutorial

Introduction

Here is the source code for cz.moz.ctmanager.main.DetailsFrame.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.EmailsDao;
import cz.moz.ctmanager.daos.PhonesDao;
import cz.moz.ctmanager.domain.Contact;
import cz.moz.ctmanager.domain.Email;
import cz.moz.ctmanager.domain.PhoneNumber;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.springframework.beans.factory.annotation.Autowired;

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

    /**
     * Creates new form DetailsFrame
     */
    public DetailsFrame() {
    }

    public Contact contact;

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

    public Contact getCurrentContact() {
        return this.contact;
    }

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

    @Autowired
    EmailsDao emailsDao;

    @Autowired
    PhonesDao phonesDao;

    @Autowired
    AddEmailFrame addEmailFrame;

    @Autowired
    AddPhoneNumberFrame addPhoneNumberFrame;

    @Autowired
    AddContactFrame addContactFrame;

    public void refreshList() {
        //For emails
        DefaultTableModel newEmailModel = (DefaultTableModel) emailTable.getModel();
        newEmailModel.setRowCount(0);
        List<Email> emailList = emailsDao.getAll(contact.getID());
        for (Email iteratorEmail : emailList) {
            int id = iteratorEmail.getId();
            String email = iteratorEmail.getEmail();
            String desc = iteratorEmail.getDescription();
            newEmailModel.addRow(new Object[] { id, email, desc });
        }
        emailTable.setModel(newEmailModel);

        //For phone numbers
        DefaultTableModel newPhoneModel = (DefaultTableModel) phoneTable.getModel();
        newPhoneModel.setRowCount(0);
        List<PhoneNumber> phoneList = phonesDao.getAll(contact.getID());
        for (PhoneNumber iteratorPhone : phoneList) {
            int id = iteratorPhone.getId();
            String email = iteratorPhone.getPhoneNumber();
            String desc = iteratorPhone.getDescription();
            newPhoneModel.addRow(new Object[] { id, email, desc });
        }
        phoneTable.setModel(newPhoneModel);
    }

    /**
     * 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();
        jTable1 = new javax.swing.JTable();
        jButton6 = new javax.swing.JButton();
        removeEmailButton = new javax.swing.JButton();
        removePhoneButton = new javax.swing.JButton();
        addPhoneButton = new javax.swing.JButton();
        addEmailButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        phoneTable = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        emailTable = new javax.swing.JTable();
        firstNameTextField = new javax.swing.JTextField();
        addressTextField = new javax.swing.JTextField();
        lastNameTextField = new javax.swing.JTextField();

        jTable1.setModel(
                new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        jScrollPane1.setViewportView(jTable1);

        jButton6.setText("Close");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        removeEmailButton.setText("Remove");
        removeEmailButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeEmailButtonActionPerformed(evt);
            }
        });

        removePhoneButton.setText("Remove");
        removePhoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removePhoneButtonActionPerformed(evt);
            }
        });

        addPhoneButton.setText("Add");
        addPhoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPhoneButtonActionPerformed(evt);
            }
        });

        addEmailButton.setText("Add");
        addEmailButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addEmailButtonActionPerformed(evt);
            }
        });

        phoneTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "ID", "Number", "Description" }) {
            Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane4.setViewportView(phoneTable);

        emailTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "ID", "Email", "Description" }) {
            Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane5.setViewportView(emailTable);

        firstNameTextField.setText("First Name");

        addressTextField.setText("Address");

        lastNameTextField.setText("Last Name");

        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()
                                        .addComponent(addEmailButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(removeEmailButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(addPhoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(removePhoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 325,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(
                                                jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 325,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(lastNameTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 190,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(firstNameTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 190,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 190,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(270, 270, 270)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(addressTextField, 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)
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(removePhoneButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(addPhoneButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(removeEmailButton).addComponent(addEmailButton))
                        .addContainerGap()));

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

    private void removeEmailButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeEmailButtonActionPerformed
        int selectedRow = emailTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "Nothing Selected", "Error Message", JOptionPane.ERROR_MESSAGE);
        } else {
            Object[] options = { "Are you Sure?" };
            int choice = JOptionPane.showConfirmDialog(this, options, "Are you Sure", 2);
            if (choice == 0) {
                DefaultTableModel newTableModel = (DefaultTableModel) emailTable.getModel();
                int selectedEmailID = (int) emailTable.getValueAt(emailTable.getSelectedRow(), 0);
                newTableModel.removeRow(selectedRow);
                emailsDao.removeEmail(selectedEmailID);
            }
        }
    }//GEN-LAST:event_removeEmailButtonActionPerformed

    private void removePhoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePhoneButtonActionPerformed
        int selectedRow = phoneTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "Nothing Selected", "Error Message", JOptionPane.ERROR_MESSAGE);
        } else {
            Object[] options = { "Are you Sure?" };
            int choice = JOptionPane.showConfirmDialog(this, options, "Are you Sure", 2);
            if (choice == 0) {
                DefaultTableModel newTableModel = (DefaultTableModel) phoneTable.getModel();
                int selectedPhoneID = (int) phoneTable.getValueAt(phoneTable.getSelectedRow(), 0);
                newTableModel.removeRow(selectedRow);
                phonesDao.removePhoneNumber(selectedPhoneID);
            }
        }
    }//GEN-LAST:event_removePhoneButtonActionPerformed

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

    private void addPhoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPhoneButtonActionPerformed
        addPhoneNumberFrame.setOwner(contact);
        addPhoneNumberFrame.setVisible(true);
    }//GEN-LAST:event_addPhoneButtonActionPerformed

    private void addEmailButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEmailButtonActionPerformed
        addEmailFrame.setOwner(contact);
        addEmailFrame.setVisible(true);
    }//GEN-LAST:event_addEmailButtonActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addEmailButton;
    private javax.swing.JButton addPhoneButton;
    private javax.swing.JTextField addressTextField;
    private javax.swing.JTable emailTable;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JButton jButton6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField lastNameTextField;
    private javax.swing.JTable phoneTable;
    private javax.swing.JButton removeEmailButton;
    private javax.swing.JButton removePhoneButton;
    // End of variables declaration//GEN-END:variables
}