com.hris.payroll.PayrollLedgerUI.java Source code

Java tutorial

Introduction

Here is the source code for com.hris.payroll.PayrollLedgerUI.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.hris.payroll;

import com.hris.employee.EmployeeGridUI;
import com.hris.employee.container.EmployeeDataContainer;
import com.hris.employee.grid.EmployeeDataGridProperties;
import com.hris.service.EmployeeService;
import com.hris.employee.serviceprovider.EmployeeServiceImpl;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.server.Sizeable;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;

/**
 *
 * @author jetdario
 */
public class PayrollLedgerUI extends HorizontalSplitPanel {

    EmployeeService es = new EmployeeServiceImpl();

    EmployeeDataGridProperties employeeGrid;
    EmployeeGridUI employeeGridUI;

    ComboBox employeeStatus = new ComboBox();
    private int branchId;

    public PayrollLedgerUI(int branchId) {
        this.branchId = branchId;

        setSizeFull();
        setLocked(true);
        setSplitPosition(300, Sizeable.Unit.PIXELS);

        if (branchId == 0) {
            setFirstComponent(firstComponent());
        } else {
            setFirstComponent(firstComponent(branchId));
        }
    }

    private VerticalLayout firstComponent() {
        employeeGridUI = new EmployeeGridUI(new EmployeeDataContainer());
        VerticalLayout v = new VerticalLayout();
        v.setWidth("100%");
        v.setHeight("100%");

        employeeStatus = employeeStatusComboBox();
        employeeStatus.addValueChangeListener((Property.ValueChangeEvent event) -> {
            v.removeComponent(employeeGridUI);

            if (employeeStatus.getValue() == null) {
                if (getBranchId() == 0) {
                    employeeGridUI = new EmployeeGridUI(new EmployeeDataContainer());
                } else {
                    employeeGridUI = new EmployeeGridUI(
                            new EmployeeDataContainer(getBranchId(), (int) employeeStatus.getValue()));
                }
            } else {
                employeeGridUI = new EmployeeGridUI(
                        new EmployeeDataContainer(getBranchId(), (int) employeeStatus.getValue()));
            }

            v.addComponent(employeeGridUI);
            v.setExpandRatio(employeeGridUI, 2);
        });
        v.addComponent(employeeStatus);

        v.addComponent(employeeGridUI);
        v.setExpandRatio(employeeGridUI, 2);

        return v;
    }

    private VerticalLayout firstComponent(int branchId) {
        employeeGridUI = new EmployeeGridUI(new EmployeeDataContainer(branchId));
        VerticalLayout v = new VerticalLayout();
        v.setWidth("100%");
        v.setHeight("100%");

        employeeStatus = employeeStatusComboBox();
        employeeStatus.addValueChangeListener((Property.ValueChangeEvent event) -> {
            v.removeComponent(employeeGridUI);

            if (employeeStatus.getValue() == null) {
                employeeGridUI = new EmployeeGridUI(new EmployeeDataContainer(getBranchId()));
            } else {
                employeeGridUI = new EmployeeGridUI(
                        new EmployeeDataContainer(getBranchId(), (int) employeeStatus.getValue()));
            }

            v.addComponent(employeeGridUI);
            v.setExpandRatio(employeeGridUI, 2);
        });
        v.addComponent(employeeStatus);

        v.addComponent(employeeGridUI);
        v.setExpandRatio(employeeGridUI, 2);

        return v;
    }

    private ComboBox employeeStatusComboBox() {
        String str[] = { "EMPLOYED", "RESIGNED" };

        ComboBox select = new ComboBox();
        select.setWidth("100%");
        select.addStyleName(ValoTheme.COMBOBOX_SMALL);
        select.addStyleName(ValoTheme.TEXTFIELD_ALIGN_RIGHT);
        select.setNullSelectionAllowed(true);
        select.setImmediate(true);
        select.addContainerProperty("c", String.class, "");
        select.setItemCaptionPropertyId("c");

        Item item = null;
        int i = 0;
        for (String s : str) {
            item = select.addItem(i);
            item.getItemProperty("c").setValue(s);
            i++;
        }

        return select;
    }

    public int getBranchId() {
        return branchId;
    }

}