dumbara.view.AddEmployee.java Source code

Java tutorial

Introduction

Here is the source code for dumbara.view.AddEmployee.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 dumbara.view;

import dumabara.controller.EmployeeController;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import dumbara.process.ImagePreviewPanel;
import dumbara.model.Employee;
import org.joda.time.LocalDate;
import org.joda.time.Years;
import dumbara.process.ImageCropDialog;

/**
 *
 * @author chanu1993
 */
public class AddEmployee extends javax.swing.JFrame {

    private ImageIcon img;

    /**
     * Creates new form AddEmployee
     */
    public AddEmployee() {
        initComponents();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(2);
        try {
            idText.setText(generateID());
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

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

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        assigneddatePicker = new org.jdesktop.swingx.JXDatePicker();
        addressText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        marriedButton = new javax.swing.JRadioButton();
        unmarriedButton = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nationalityPicker = new javax.swing.JComboBox();
        nameText = new javax.swing.JTextField();
        dobPicker = new org.jdesktop.swingx.JXDatePicker();
        jLabel12 = new javax.swing.JLabel();
        nicText = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        contactNoText = new javax.swing.JTextField();
        idText = new javax.swing.JTextField();
        ageText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        bloodGroupPicker = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        EmgergencyCallText = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        imageLabel = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        maleButton = new javax.swing.JRadioButton();
        femaleButton = new javax.swing.JRadioButton();
        jLabel13 = new javax.swing.JLabel();
        salaryText = new javax.swing.JTextField();
        resetButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        addButton.setBackground(new java.awt.Color(204, 204, 255));
        addButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        addButton.setText("Add Employee");
        addButton.setEnabled(false);
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        cancelButton.setBackground(new java.awt.Color(204, 204, 255));
        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 51, 51), 2));

        assigneddatePicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                assigneddatePickerActionPerformed(evt);
            }
        });

        addressText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        addressText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addressTextActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Contact No :");

        buttonGroup1.add(marriedButton);
        marriedButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        marriedButton.setText("Married");

        buttonGroup1.add(unmarriedButton);
        unmarriedButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        unmarriedButton.setText("Unmarried");
        unmarriedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unmarriedButtonActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Age :");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("EmpID :");

        nationalityPicker.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sinhala", "Tamil" }));
        nationalityPicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nationalityPickerActionPerformed(evt);
            }
        });

        nameText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        nameText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextActionPerformed(evt);
            }
        });

        dobPicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dobPickerActionPerformed(evt);
            }
        });
        dobPicker.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dobPickerFocusGained(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel12.setText("Assigned Date :");

        nicText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        nicText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nicTextActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Civil Status :");

        jLabel11.setText("MM / DD / YYYY");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Address :");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Nationality :");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("DOB :");

        contactNoText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        contactNoText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contactNoTextActionPerformed(evt);
            }
        });
        contactNoText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                contactNoTextKeyReleased(evt);
            }
        });

        idText.setEditable(false);
        idText.setBackground(new java.awt.Color(255, 153, 153));
        idText.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        idText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idTextActionPerformed(evt);
            }
        });

        ageText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ageText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ageTextActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Name :");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("NIC :");

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel14.setText("Blood Group :");

        bloodGroupPicker.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-" }));
        bloodGroupPicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bloodGroupPickerActionPerformed(evt);
            }
        });

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel15.setText("Emergency Call :");

        EmgergencyCallText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        EmgergencyCallText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EmgergencyCallTextActionPerformed(evt);
            }
        });
        EmgergencyCallText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                EmgergencyCallTextKeyReleased(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel16.setText("Add Image :");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setText("Browse");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel17.setText("Gender :");

        buttonGroup2.add(maleButton);
        maleButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        maleButton.setText("Male");

        buttonGroup2.add(femaleButton);
        femaleButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        femaleButton.setText("Female");

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel13.setText("Salary :");

        salaryText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        salaryText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                salaryTextKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(
                                        jPanel1Layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2)
                                                .addComponent(jLabel4))
                                .addGap(35, 35, 35)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE, 126,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(29, 29, 29).addComponent(jLabel3).addGap(3, 3, 3)
                                                .addComponent(nameText))
                                        .addComponent(addressText, javax.swing.GroupLayout.PREFERRED_SIZE, 547,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel5).addComponent(jLabel8))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(ageText, javax.swing.GroupLayout.PREFERRED_SIZE, 174,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(contactNoText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        174, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(46, 46, 46))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout
                                                .createSequentialGroup()
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel12).addComponent(jLabel14)
                                                        .addComponent(jLabel16))
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        false)
                                                                        .addComponent(assigneddatePicker,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(bloodGroupPicker, 0,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)))
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                                jPanel1Layout.createSequentialGroup()
                                                                        .addGap(32, 32, 32).addComponent(jButton1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                84,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addGap(79, 79, 79))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel9)
                                                .addGap(18, 18, 18)
                                                .addComponent(nationalityPicker,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 162,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(41, 41, 41))))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel10).addComponent(jLabel17)).addGap(18, 18, 18)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(marriedButton).addComponent(maleButton))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(femaleButton).addComponent(unmarriedButton)))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel7)
                                                .addGap(18, 18, 18)
                                                .addComponent(dobPicker, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(8, 8, 8).addComponent(jLabel11))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel15).addComponent(jLabel13))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(salaryText,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 169,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(EmgergencyCallText,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 181,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel6)
                                                .addGap(24, 24, 24).addComponent(nicText,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 272,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 232,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(171, 171, 171)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(30, 30, 30).addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2)
                        .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3).addComponent(nameText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4).addComponent(addressText,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel7)
                                        .addComponent(dobPicker, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel11))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5).addComponent(contactNoText,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel6).addComponent(nicText,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel8).addComponent(ageText,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(nationalityPicker, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10).addComponent(marriedButton).addComponent(unmarriedButton))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel12)
                                .addComponent(assigneddatePicker, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel17).addComponent(maleButton).addComponent(femaleButton))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel14)
                                .addComponent(bloodGroupPicker, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel15)
                                .addComponent(EmgergencyCallText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel16).addComponent(jButton1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(salaryText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel13)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)));

        resetButton.setBackground(new java.awt.Color(204, 204, 255));
        resetButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 0, 102));
        jLabel1.setText("Add Employee");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout
                .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup().addGap(282, 282, 282).addComponent(jLabel1)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel2Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel2Layout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(4, 4, 4).addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel2,
                        javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

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

    private void unmarriedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unmarriedButtonActionPerformed
        // TODO add your handling code here:
        assigneddatePicker.requestFocus();
    }//GEN-LAST:event_unmarriedButtonActionPerformed

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

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        // TODO add your handling code here:
        String id = idText.getText();
        String name = nameText.getText();
        String address = addressText.getText();
        String contactNo = contactNoText.getText();
        String dob;
        dob = dobPicker.getDate().toString();
        String age = ageText.getText();
        String nic = nicText.getText();
        String assignDate;
        assignDate = assigneddatePicker.getDate().toString();
        String gender = maleButton.isSelected() ? "Male" : "Female";
        String civilStatus;
        civilStatus = marriedButton.isSelected() ? "Married" : "Unmarried";
        //System.out.println(civilStatus);
        String nationality;
        nationality = nationalityPicker.getSelectedItem().toString();
        String bloodGroup = bloodGroupPicker.getSelectedItem().toString();
        String emergencyCall = EmgergencyCallText.getText();
        double salary = Double.parseDouble(salaryText.getText());
        Employee emp = new Employee(id, name, address, contactNo, dob, age, nic, assignDate, gender, civilStatus,
                nationality, bloodGroup, salary, emergencyCall, img);
        try {
            int res = EmployeeController.addEmployee(emp);
            if (res > 0) {
                JOptionPane.showMessageDialog(this, "Added " + id);
            } else {
                JOptionPane.showMessageDialog(this, "Error adding " + id);
            }
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        resetButton.requestFocus();
    }//GEN-LAST:event_addButtonActionPerformed

    private void idTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idTextActionPerformed
        // TODO add your handling code here:
        nameText.requestFocus();
    }//GEN-LAST:event_idTextActionPerformed

    private void nameTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextActionPerformed
        // TODO add your handling code here:
        addressText.requestFocus();
    }//GEN-LAST:event_nameTextActionPerformed

    private void addressTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addressTextActionPerformed
        // TODO add your handling code here:
        contactNoText.requestFocus();
    }//GEN-LAST:event_addressTextActionPerformed

    private void contactNoTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contactNoTextActionPerformed
        // TODO add your handling code here:
        nicText.requestFocus();
    }//GEN-LAST:event_contactNoTextActionPerformed

    private void nicTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nicTextActionPerformed
        // TODO add your handling code here:
        ageText.requestFocus();
    }//GEN-LAST:event_nicTextActionPerformed

    private void ageTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ageTextActionPerformed
        // TODO add your handling code here:
        dobPicker.requestFocus();
    }//GEN-LAST:event_ageTextActionPerformed

    private void dobPickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dobPickerActionPerformed
        long timeToday = dobPicker.getDate().getTime();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(timeToday));
        LocalDate ld = new LocalDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE));
        LocalDate newDate = new LocalDate();
        Years age = Years.yearsBetween(ld, newDate);
        System.out.println(age.getYears());
        ageText.setText(age.getYears() + "");
        nationalityPicker.requestFocus();
    }//GEN-LAST:event_dobPickerActionPerformed

    private void nationalityPickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nationalityPickerActionPerformed
        // TODO add your handling code here:
        unmarriedButton.requestFocus();
    }//GEN-LAST:event_nationalityPickerActionPerformed

    private void assigneddatePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assigneddatePickerActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_assigneddatePickerActionPerformed

    private void bloodGroupPickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bloodGroupPickerActionPerformed
        // TODO add your handling code here:
        EmgergencyCallText.requestFocus();
    }//GEN-LAST:event_bloodGroupPickerActionPerformed

    private void EmgergencyCallTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EmgergencyCallTextActionPerformed
        // TODO add your handling code here:
        addButton.requestFocus();
    }//GEN-LAST:event_EmgergencyCallTextActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        // TODO add your handling code here:
        dispose();
        new AddEmployee().setVisible(true);
    }//GEN-LAST:event_resetButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        JFileChooser fileChooser = new JFileChooser();

        ImagePreviewPanel previewPanel = new ImagePreviewPanel();
        fileChooser.setAccessory(previewPanel);
        fileChooser.addPropertyChangeListener(previewPanel);
        fileChooser.setDialogTitle("Select the employee photo");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG and PNG images", "jpg", "gif", "png",
                "jpeg");
        fileChooser.setFileFilter(filter);
        int res = fileChooser.showOpenDialog((Component) this);
        if (res == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            ImageIcon imageIcon = new ImageIcon(file.getPath());
            //imageIcon = new ImageIcon(imageIcon.getImage().getScaledInstance(800, (int)(h*ratio), BufferedImage.SCALE_SMOOTH));
            new ImageCropDialog(this, imageIcon, 20, 10, true);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void dobPickerFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dobPickerFocusGained

    }//GEN-LAST:event_dobPickerFocusGained

    private void EmgergencyCallTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_EmgergencyCallTextKeyReleased
        String txt = EmgergencyCallText.getText();
        int caretPosition = EmgergencyCallText.getCaretPosition();
        if (!txt.matches("^[0-9]{0,10}$")) {
            EmgergencyCallText.setText(txt.substring(0, caretPosition - 1) + txt.substring(caretPosition));
            EmgergencyCallText.setCaretPosition(caretPosition - 1);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_EmgergencyCallTextKeyReleased

    private void contactNoTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contactNoTextKeyReleased
        String txt = contactNoText.getText();
        int caretPosition = contactNoText.getCaretPosition();
        if (!txt.matches("^[0-9]{0,10}$")) {
            contactNoText.setText(txt.substring(0, caretPosition - 1) + txt.substring(caretPosition));
            contactNoText.setCaretPosition(caretPosition - 1);
        }
    }//GEN-LAST:event_contactNoTextKeyReleased

    private void salaryTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_salaryTextKeyReleased
        if (!idText.getText().equals("") && !nameText.getText().equals("") && !addressText.getText().equals("")
                && !contactNoText.getText().equals("") && !nicText.getText().equals("")
                && !ageText.getText().equals("") && !dobPicker.getDate().toString().equals(null)
                && !assigneddatePicker.getDate().toString().equals(null) && !EmgergencyCallText.getText().equals("")
                && !salaryText.getText().equals("")) {
            addButton.setEnabled(true);
        }
    }//GEN-LAST:event_salaryTextKeyReleased

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

    }

    private String generateID() throws ClassNotFoundException, SQLException {
        String EmpID = EmployeeController.getLastId();
        if (EmpID == null) {
            return "E0001";
        } else {
            int num = Integer.parseInt(EmpID.substring(1));
            num++;
            if (num < 10) {
                return "E000" + num;
            } else if (num < 100) {
                return "E00" + num;
            } else if (num < 1000) {
                return "E0" + num;
            } else {
                return "E" + num;
            }
        }
    }

    public void setImg(ImageIcon img) {
        this.img = img;
        imageLabel.setIcon(new ImageIcon(img.getImage().getScaledInstance(imageLabel.getWidth(),
                imageLabel.getHeight(), BufferedImage.SCALE_SMOOTH)));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField EmgergencyCallText;
    private javax.swing.JButton addButton;
    private javax.swing.JTextField addressText;
    private javax.swing.JTextField ageText;
    private org.jdesktop.swingx.JXDatePicker assigneddatePicker;
    private javax.swing.JComboBox bloodGroupPicker;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField contactNoText;
    private org.jdesktop.swingx.JXDatePicker dobPicker;
    private javax.swing.JRadioButton femaleButton;
    private javax.swing.JTextField idText;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton maleButton;
    private javax.swing.JRadioButton marriedButton;
    private javax.swing.JTextField nameText;
    private javax.swing.JComboBox nationalityPicker;
    private javax.swing.JTextField nicText;
    private javax.swing.JButton resetButton;
    private javax.swing.JTextField salaryText;
    private javax.swing.JRadioButton unmarriedButton;
    // End of variables declaration//GEN-END:variables
}