com.hris.employee.grid.EmployeeDataGridProperties.java Source code

Java tutorial

Introduction

Here is the source code for com.hris.employee.grid.EmployeeDataGridProperties.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.employee.grid;

import com.hris.common.abstractclasses.AbstractDataGridProperties;
import com.hris.employee.container.EmployeeDataContainer;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;

/**
 *
 * @author jetdario
 */
public class EmployeeDataGridProperties extends AbstractDataGridProperties {

    private int branchId;
    private IndexedContainer container;

    //    public EmployeeDataGridProperties() {
    //        setContainerDataSource(new EmployeeDataContainer());
    //        gridRenrderersAndGenerator();
    //        filterDataGrid(new EmployeeDataContainer());
    //    }

    public EmployeeDataGridProperties(IndexedContainer container) {
        setContainerDataSource(container);
        gridRenrderersAndGenerator();
        filterDataGrid(container);
    }

    @Override
    protected void gridRenrderersAndGenerator() {
        removeColumn("employeeId");
    }

    @Override
    protected Window removeGridItem(int id, Object itemId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void filterDataGrid(IndexedContainer container) {
        // Create a header row to hold column filters
        HeaderRow filterRow = appendHeaderRow();

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

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

                    filterField.addTextChangeListener(change -> {
                        container.removeContainerFilters(pid);

                        if (!change.getText().isEmpty()) {
                            container.addContainerFilter(
                                    new SimpleStringFilter(pid, change.getText().toUpperCase(), true, false));
                        }
                    });
                    cell.setComponent(filterField);
                });
    }
}