com.save.employee.PersonalInformationForm.java Source code

Java tutorial

Introduction

Here is the source code for com.save.employee.PersonalInformationForm.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.save.employee;

import com.save.employee.EmployeesDataGridProperties;
import com.save.model.Employee;
import com.save.service.EmployeeService;
import com.save.employee.serviceprovider.EmployeeServiceImpl;
import com.save.utilities.CommonUtilities;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Notification;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;
import java.util.Date;

/**
 *
 * @author jetdario
 */
public class PersonalInformationForm extends FormLayout {

    private static String BUTTON_CAPTION = "EDIT INFORMATION";
    private static String FORM_CAPTION = "personal information form";

    EmployeeService es = new EmployeeServiceImpl();
    private int employeeId;

    TextField employeeNoField;
    TextField firstname;
    TextField middlename;
    TextField lastname;
    OptionGroup gender;
    ComboBox status;
    DateField birthday;

    Button editBtn;
    Button cancelBtn;
    Button newEntryBtn;
    Button removeBtn;

    private HorizontalSplitPanel hsplit;
    private Grid grid;
    private Object itemId;

    public PersonalInformationForm(HorizontalSplitPanel hsplit, int employeeId, Grid grid, Object itemId) {
        this.hsplit = hsplit;
        this.employeeId = employeeId;
        this.grid = grid;
        this.itemId = itemId;

        setMargin(new MarginInfo(true, true, false, false));
        setWidth("80%");
        addStyleName("light");

        editBtn = new Button(BUTTON_CAPTION, editBtnListener);
        editBtn.setEnabled(false);

        cancelBtn = new Button("CANCEL");
        cancelBtn.setEnabled(false);

        newEntryBtn = new Button("NEW ACCOUNT");

        employeeNoField = new TextField("Employee ID: ");
        employeeNoField.setWidth("50%");
        employeeNoField.setRequired(true);
        employeeNoField.setRequiredError("Required Employee ID!");
        addComponent(employeeNoField);

        firstname = new TextField("Firstname: ");
        firstname.setWidth("50%");
        firstname.setRequired(true);
        firstname.setRequiredError("Required Firstname!");
        addComponent(firstname);

        middlename = new TextField("Middlename: ");
        middlename.setWidth("50%");
        middlename.setRequired(true);
        middlename.setRequiredError("Required Middlename!");
        addComponent(middlename);

        lastname = new TextField("Lastname: ");
        lastname.setWidth("50%");
        lastname.setRequired(true);
        lastname.setRequiredError("Required Lastname!");
        addComponent(lastname);

        gender = new OptionGroup("Gender: ");
        gender.addItem("Female");
        gender.addItem("Male");
        gender.addStyleName("horizontal");
        addComponent(gender);

        status = new ComboBox("Status: ");
        status.setNullSelectionAllowed(false);
        status.addItem("Single");
        status.addItem("Married");
        status.addItem("Widow");
        status.addItem("Separated");
        addComponent(status);

        birthday = new DateField("Birthday");
        birthday.setValue(new Date());
        addComponent(birthday);

        if (getEmployeeId() != 0) {
            Employee e = es.getEmployeesPersonalDataById(getEmployeeId());
            employeeNoField.setValue(e.getEmployeeNo());
            firstname.setValue(e.getFirstname());
            middlename.setValue(e.getMiddlename());
            lastname.setValue(e.getLastname());
            gender.setValue(CommonUtilities.capitalizeFirstLetter(e.getGender()));
            status.setValue(e.getPersonalStatus());
            birthday.setValue((e.getDob() == null) ? new Date() : e.getDob());
            editBtn.setEnabled(true);
            cancelBtn.setEnabled(true);
        }

        setReadOnly(true);

        HorizontalLayout footer = new HorizontalLayout();
        footer.setMargin(new MarginInfo(true, false, true, false));
        footer.setSpacing(true);
        footer.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
        addComponent(footer);
        footer.addComponent(editBtn);
        footer.addComponent(cancelBtn);
        footer.addComponent(newEntryBtn);

        removeBtn = new Button("REMOVE ACCOUNT");
        removeBtn.addClickListener(removeBtnListener);
        addComponent(removeBtn);

        cancelBtn.addClickListener((Button.ClickEvent event) -> {
            setReadOnly(false);
            addStyleName("light");
            editBtn.setCaption(BUTTON_CAPTION);
            editBtn.removeStyleName("primary");
            setReadOnly(true);
            disableFields(false);
            newEntryBtn.setEnabled(true);
        });

        newEntryBtn.addClickListener((Button.ClickEvent event) -> {
            setReadOnly(true);
            addStyleName("light");
            editBtn.setCaption(BUTTON_CAPTION);
            editBtn.removeStyleName("primary");

            Window sub = new CreateNewAccountWindow(getHsplit());
            UI.getCurrent().addWindow(sub);
        });

        disableFields(false);
    }

    int getEmployeeId() {
        return employeeId;
    }

    Button.ClickListener editBtnListener = new Button.ClickListener() {

        @Override
        public void buttonClick(Button.ClickEvent event) {
            boolean readOnly = isReadOnly();
            if (readOnly) {
                setReadOnly(false);
                removeStyleName("light");
                event.getButton().setCaption("UPDATE INFORMATION");
                event.getButton().addStyleName("primary");
                disableFields(true);
                newEntryBtn.setEnabled(false);
            } else {
                setReadOnly(true);
                addStyleName("light");
                event.getButton().setCaption("EDIT INFORMATION");
                event.getButton().removeStyleName("primary");
                disableFields(false);
                newEntryBtn.setEnabled(true);
            }

            if (firstname.getValue() == null || firstname.getValue().trim().isEmpty()) {
                return;
            }
            if (middlename.getValue() == null || middlename.getValue().trim().isEmpty()) {
                return;
            }
            if (lastname.getValue() == null || lastname.getValue().trim().isEmpty()) {
                return;
            }
            if (status.getValue() == null) {
                return;
            }
            if (birthday.getValue() == null) {
                return;
            }

            Employee e = new Employee();

            if (event.getButton().getCaption().equals("EDIT INFORMATION")) {
                e.setEmployeeNo(employeeNoField.getValue().trim().toLowerCase());
                e.setFirstname(firstname.getValue().trim().toLowerCase());
                e.setMiddlename(middlename.getValue().trim().toLowerCase());
                e.setLastname(lastname.getValue().trim().toLowerCase());
                e.setGender(gender.getValue().toString().toLowerCase());
                e.setPersonalStatus(status.getItemCaption(status.getValue()));
                e.setDob(birthday.getValue());
                e.setEmployeeId(getEmployeeId());

                if (es.checkIfEmployeeNoExist(employeeNoField.getValue().trim().toLowerCase())) {
                    Notification.show("EmployeeNo already exist", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (es.updatePersonalInformation(e)) {
                    Notification.show("Update Personal Info", Notification.Type.TRAY_NOTIFICATION);
                    getHsplit().setFirstComponent(new EmployeesDataGridProperties(getHsplit(), "personal"));
                }
            }
        }
    };

    Button.ClickListener removeBtnListener = (Button.ClickEvent event) -> {
        if (getEmployeeId() == 0) {
            Notification.show("Select an Employee to Remove!", Notification.Type.ERROR_MESSAGE);
            return;
        }

        Window sub = new RemoveAccountWindow(getEmployeeId());
        UI.getCurrent().addWindow(sub);

        sub.addCloseListener((Window.CloseEvent e) -> {
            getDataGrid().getContainerDataSource().removeItem(getDataItemId());
            clearFields();
        });
    };

    HorizontalSplitPanel getHsplit() {
        return hsplit;
    }

    Grid getDataGrid() {
        return grid;
    }

    Object getDataItemId() {
        return itemId;
    }

    void clearFields() {
        employeeNoField.setValue("");
        firstname.setValue("");
        middlename.setValue("");
        lastname.setValue("");
        gender.setValue(null);
    }

    void disableFields(boolean bool) {
        employeeNoField.setEnabled(bool);
        firstname.setEnabled(bool);
        middlename.setEnabled(bool);
        lastname.setEnabled(bool);
        gender.setEnabled(bool);
        status.setEnabled(bool);
        birthday.setEnabled(bool);
    }
}