com.save.employee.maintenance.MRDataGridProperties.java Source code

Java tutorial

Introduction

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

import com.save.abstractclasses.AbstractDataGridProperties;
import com.save.service.MRService;
import com.save.employee.serviceprovider.MRServiceImpl;
import com.save.utilities.CommonUtilities;
import com.template.save.FontIconRenderer;
import com.vaadin.data.Item;
import com.vaadin.ui.Button;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;

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

    MRService mrs = new MRServiceImpl();

    private int employeeId;

    public MRDataGridProperties(int employeeId) {
        this.employeeId = employeeId;

        setWidth("100%");
        setHeight("100%");
        setContainerDataSource(new MRDataContainer(getEmployeeId()));
        gridRenrderersAndGenerator();
    }

    int getEmployeeId() {
        return employeeId;
    }

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

    @Override
    public void gridRenrderersAndGenerator() {
        //TODO
        removeColumn("id");
        getColumn("edit").setWidth(40);
        //        getColumn("view").setWidth(40);
        getColumn("del").setWidth(40);
        getDefaultHeaderRow().join("edit", "del").setText("");

        setFrozenColumnCount(1);

        getColumn("del").setRenderer(new FontIconRenderer(e -> {
            Object itemId = e.getItemId();
            Item item = getContainerDataSource().getItem(itemId);

            Window sub = deletConfirmationWindow(
                    CommonUtilities.convertStringToInt(item.getItemProperty("id").getValue().toString()), itemId);
            if (sub.getParent() == null) {
                UI.getCurrent().addWindow(sub);
            }
        }));

        //        getColumn("view").setRenderer(new FontIconRenderer(e -> {
        //            Object itemId = e.getItemId();
        //            Item item = getContainerDataSource().getItem(itemId);
        //            
        //            Window sub = new MRFormWindow(getEmployeeId(), 
        //                    true, 
        //                    CommonUtilities.convertStringToInt(item.getItemProperty("id").getValue().toString()));
        //            if(sub.getParent() == null){
        //                UI.getCurrent().addWindow(sub);                
        //            }
        //        }));

        getColumn("edit").setRenderer(new FontIconRenderer(e -> {
            Object itemId = e.getItemId();
            Item item = getContainerDataSource().getItem(itemId);

            Window sub = new MRFormWindow(getEmployeeId(),
                    CommonUtilities.convertStringToInt(item.getItemProperty("id").getValue().toString()), this);
            if (sub.getParent() == null) {
                UI.getCurrent().addWindow(sub);
            }
        }));

        recalculateColumnWidths();
    }

    Window deletConfirmationWindow(int mrId, Object itemId) {
        Window sub = new Window();
        sub.setCaption("CONFIRM DELETE");
        sub.setWidth("250px");
        sub.setModal(true);

        VerticalLayout v = new VerticalLayout();
        v.setWidth("100%");
        v.setMargin(true);

        Button deleteBtn = new Button("DELETE?");
        deleteBtn.setWidth("100%");
        deleteBtn.addStyleName(ValoTheme.BUTTON_PRIMARY);
        deleteBtn.addClickListener((Button.ClickEvent event) -> {
            boolean result = mrs.removeMaintenanceReimbursement(mrId);
            if (result) {
                getContainerDataSource().removeItem(itemId);
                sub.close();
            }
        });
        v.addComponent(deleteBtn);

        sub.setContent(v);
        sub.getContent().setHeightUndefined();
        return sub;
    }
}