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

Java tutorial

Introduction

Here is the source code for cz.moz.ctmanager.main.MainAppFrame.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 java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.springframework.beans.factory.annotation.Autowired;

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

    public MainAppFrame() {
    }

    @Autowired
    AddContactFrame addContactFrame;

    @Autowired
    ContactsDao contactsDao;

    @Autowired
    PhonesDao phonesDao;

    @Autowired
    EmailsDao emailsDao;

    public void initializeComponents() {
        this.initComponents();
        this.setTitle("Contact Manager 2000");
        this.refreshList();
    }

    public void refreshList() {
        DefaultTableModel newTableModel = (DefaultTableModel) contactTable.getModel();
        newTableModel.setRowCount(0);
        List<Contact> contactList = contactsDao.getAllContacts();
        for (Contact iteratorContact : contactList) {
            int id = iteratorContact.getID();
            String firstName = iteratorContact.getFirstName();
            String lastName = iteratorContact.getLastName();
            String address = iteratorContact.getAddress();
            newTableModel.addRow(new Object[] { id, firstName, lastName, address });
        }
        contactTable.setModel(newTableModel);
    }

    /**
     * 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();
        contactTable = new javax.swing.JTable();
        addContactButton = new javax.swing.JButton();
        removeContactButton = new javax.swing.JButton();
        importExportButton = new javax.swing.JButton();
        detailsButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        }, new String[] { "ID", "First Name", "Last Name", "Address" }) {
            Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class,
                    java.lang.Object.class };
            boolean[] canEdit = new boolean[] { false, false, false, false };

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

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

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

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

        importExportButton.setText("Import/Export");
        importExportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importExportButtonActionPerformed(evt);
            }
        });

        detailsButton.setText("Details...");
        detailsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detailsButtonActionPerformed(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()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 556,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(addContactButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(removeContactButton, javax.swing.GroupLayout.DEFAULT_SIZE, 137,
                                        Short.MAX_VALUE)
                                .addComponent(importExportButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(detailsButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(addContactButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeContactButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(importExportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(detailsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap()));

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

    private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addContactButtonActionPerformed
        addContactFrame.setVisible(true);
        addContactFrame.initializeComponents();

    }//GEN-LAST:event_addContactButtonActionPerformed

    private void removeContactButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeContactButtonActionPerformed
        int selectedRow = contactTable.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) contactTable.getModel();
                int selectedContactID = (int) contactTable.getValueAt(contactTable.getSelectedRow(), 0);
                newTableModel.removeRow(selectedRow);
                emailsDao.removeAll(selectedContactID);
                phonesDao.removeAll(selectedContactID);
                contactsDao.removeContact(selectedContactID);
            }
        }

    }//GEN-LAST:event_removeContactButtonActionPerformed

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

    private void detailsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detailsButtonActionPerformed
        int selectedRow = contactTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "Nothing Selected", "Error Message", JOptionPane.ERROR_MESSAGE);
        } else {
            DetailsFrame detailsFrame = new DetailsFrame();
            int selectedContactID = (int) contactTable.getValueAt(contactTable.getSelectedRow(), 0);
            Contact contact = contactsDao.getContact(selectedContactID);
            detailsFrame.setContact(contact);
            detailsFrame.initializeComponents();
            detailsFrame.setVisible(true);
        }
    }//GEN-LAST:event_detailsButtonActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addContactButton;
    private javax.swing.JTable contactTable;
    private javax.swing.JButton detailsButton;
    private javax.swing.JButton importExportButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeContactButton;
    // End of variables declaration//GEN-END:variables
}