com.save.employee.CreateNewAccountWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.save.employee.CreateNewAccountWindow.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.model.Employee;
import com.save.service.EmployeeService;
import com.save.employee.serviceprovider.EmployeeServiceImpl;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Notification;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

/**
 *
 * @author jetdario
 */
public class CreateNewAccountWindow extends Window {

    EmployeeService employeeService = new EmployeeServiceImpl();
    HorizontalSplitPanel hsplit;

    public CreateNewAccountWindow(HorizontalSplitPanel hsplit) {
        this.hsplit = hsplit;
        setCaption("CREATE NEW ACCOUNT");
        setWidth("400px");
        setModal(true);
        setResizable(false);
        center();

        setContent(getLayout());
        getContent().setHeightUndefined();
    }

    FormLayout getLayout() {
        FormLayout f = new FormLayout();
        f.setReadOnly(false);
        f.setSpacing(true);
        f.setMargin(true);

        final TextField employeeNo = new TextField("Employee No: ");
        employeeNo.setWidth("100%");
        employeeNo.setRequired(true);
        employeeNo.setNullSettingAllowed(false);
        f.addComponent(employeeNo);

        final TextField firstname = new TextField("Firstname: ");
        firstname.setWidth("100%");
        firstname.setRequired(true);
        firstname.setNullSettingAllowed(false);
        f.addComponent(firstname);

        final TextField middlename = new TextField("Middlename: ");
        middlename.setWidth("100%");
        middlename.setRequired(true);
        middlename.setNullSettingAllowed(false);
        f.addComponent(middlename);

        final TextField lastname = new TextField("Lastname: ");
        lastname.setWidth("100%");
        lastname.setRequired(true);
        lastname.setNullSettingAllowed(false);
        f.addComponent(lastname);

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

        final ComboBox status = new ComboBox("Status: ");
        status.setWidth("100%");
        status.setNullSelectionAllowed(false);
        status.addItem("Single");
        status.addItem("Married");
        status.addItem("Widow");
        status.addItem("Separated");
        f.addComponent(status);

        Button saveBtn = new Button("SAVE");
        saveBtn.setWidth("100%");
        saveBtn.addClickListener((Button.ClickEvent event) -> {
            //TODO
            if (employeeNo.getValue().isEmpty() || employeeNo.getValue() == null) {
                Notification.show("Requried Emloyee ID", Notification.Type.WARNING_MESSAGE);
                return;
            }

            if (firstname.getValue().isEmpty() || firstname.getValue() == null) {
                Notification.show("Requried Firstname", Notification.Type.WARNING_MESSAGE);
                return;
            }

            if (middlename.getValue().isEmpty() || middlename.getValue() == null) {
                Notification.show("Requried Middlename", Notification.Type.WARNING_MESSAGE);
                return;
            }

            if (lastname.getValue().isEmpty() || lastname.getValue() == null) {
                Notification.show("Requried Lastname", Notification.Type.WARNING_MESSAGE);
                return;
            }

            if (status.getValue() == null) {
                Notification.show("Requried Status", Notification.Type.WARNING_MESSAGE);
            }

            if (employeeService.checkIfEmployeeNoExist(employeeNo.getValue().trim().toLowerCase())) {
                Notification.show("EmployeeId already Exist!", Notification.Type.ERROR_MESSAGE);
                return;
            }

            Employee e = new Employee();
            e.setEmployeeNo(employeeNo.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().trim().toLowerCase());
            e.setPersonalStatus(status.getValue().toString());

            boolean result = employeeService.createNewAccount(e);
            if (result) {
                close();
                getHsplit().setFirstComponent(new EmployeesDataGridProperties(getHsplit(), "personal"));
            }
        });
        f.addComponent(saveBtn);

        return f;
    }

    HorizontalSplitPanel getHsplit() {
        return hsplit;
    }
}