com.save.employee.EmployeesDataGridProperties.java Source code

Java tutorial

Introduction

Here is the source code for com.save.employee.EmployeesDataGridProperties.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.maintenance.MRUI;
import com.save.employee.request.RLUI;
import com.save.abstractclasses.AbstractDataGridProperties;
import com.save.service.EmployeeService;
import com.save.employee.serviceprovider.EmployeeServiceImpl;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;

/**
 *
 * @author jetdario
 */
public class EmployeesDataGridProperties extends AbstractDataGridProperties
        implements ItemClickEvent.ItemClickListener {

    EmployeeService es = new EmployeeServiceImpl();
    IndexedContainer container = new EmployeeDataContainer();
    private String employeeModuleType;

    private HorizontalSplitPanel hsplit;

    public EmployeesDataGridProperties(HorizontalSplitPanel hsplit, String employeeModuleType) {
        this.hsplit = hsplit;
        this.employeeModuleType = employeeModuleType;

        setContainerDataSource(container);
        addItemClickListener(this);

        // Create a header row to hold column filters
        HeaderRow filterRow = appendHeaderRow();

        // Set up a filter for all columns
        //        getContainerDataSource().getContainerPropertyIds().stream().filter((pid) -> (pid.equals("name"))).forEach((pid) -> {
        getContainerDataSource().getContainerPropertyIds().stream().forEach((pid) -> {
            HeaderCell cell = filterRow.getCell(pid);

            // Have an input field to use for filter
            TextField filterField = new TextField();
            filterField.setWidth("100%");
            filterField.addStyleName(ValoTheme.TEXTFIELD_TINY);
            filterField.setInputPrompt("Filter");

            // Update filter When the filter input is changed
            filterField.addTextChangeListener(change -> {
                // Can't modify filters so need to replace
                container.removeContainerFilters(pid);

                // (Re)create the filter if necessary
                if (!change.getText().isEmpty())
                    container.addContainerFilter(new SimpleStringFilter(pid, change.getText(), true, true));
                recalculateColumnWidths();
            });
            cell.setComponent(filterField);
        });

        getColumn("name").setWidth(280);
        removeColumn("id");
    }

    @Override
    public void populateDataGrid() {
        //TODO
    }

    @Override
    public void gridRenrderersAndGenerator() {
        //TODO
    }

    @Override
    public void itemClick(ItemClickEvent event) {
        //TODO
        Object itemId = event.getItemId();
        Item item = getContainerDataSource().getItem(itemId);

        switch (getEmployeeModuleType()) {
        case "maintenance": {
            getHsplit().setSecondComponent(new MRUI((int) item.getItemProperty("id").getValue()));
            break;
        }

        case "request": {
            getHsplit().setSecondComponent(new RLUI((int) item.getItemProperty("id").getValue()));
            break;
        }

        default: {
            getHsplit().setSecondComponent(
                    new EmployeeInformationTabSheet(getHsplit(), (int) item.getItemProperty("id").getValue()));
            break;
        }
        }
    }

    HorizontalSplitPanel getHsplit() {
        return hsplit;
    }

    String getEmployeeModuleType() {
        return employeeModuleType;
    }
}