Java tutorial
/* * 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.reports.maintenance; import com.save.common.CommonButton; import com.save.common.CommonComboBox; import com.save.common.CommonTextField; import com.save.global.ErrorLoggedNotification; import com.save.reports.ExportDataGridToExcel; import com.save.reports.promodeals.PromoDealDataContainer; import com.save.utilities.CommonUtilities; import com.vaadin.data.Property; import com.vaadin.server.Page; import com.vaadin.server.StreamResource; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.CheckBox; import com.vaadin.ui.ComboBox; import com.vaadin.ui.DateField; import com.vaadin.ui.FormLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Notification; import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import java.io.File; import java.io.FileInputStream; import java.util.Date; /** * * @author jetdario */ public class MaintenanceReportUI extends VerticalLayout implements Button.ClickListener { MaintenanceDataGridProperties mrDataGrid = new MaintenanceDataGridProperties(); boolean isAreaSelect = false; boolean isEmployeeSelect = false; boolean isAmountSelect = false; boolean isDateSelect = false; public MaintenanceReportUI() { setSizeFull(); mrDataGrid.setFrozenColumnCount(2); addComponent(mrDataGrid); setExpandRatio(mrDataGrid, 1); HorizontalLayout h = new HorizontalLayout(); h.setWidth("100%"); h.setSpacing(true); h.setMargin(new MarginInfo(false, true, true, false)); Button filter = new CommonButton("FILTER"); filter.setWidth("200px"); filter.addClickListener(this); h.addComponent(filter); h.setComponentAlignment(filter, Alignment.MIDDLE_RIGHT); h.setExpandRatio(filter, 1); Button exportToExcel = new CommonButton("EXPORT TO EXCEL"); exportToExcel.setWidth("200px"); exportToExcel.addClickListener(this); h.addComponent(exportToExcel); h.setComponentAlignment(exportToExcel, Alignment.MIDDLE_RIGHT); addComponent(h); } private Window filterReport() { Window sub = new Window("FILTER REPORT"); sub.setWidth("400px"); sub.setModal(true); sub.center(); FormLayout f = new FormLayout(); f.setWidth("100%"); f.setMargin(true); f.setSpacing(true); CheckBox areaCheckBox = new CheckBox("Filter by Area"); f.addComponent(areaCheckBox); CheckBox employeeCheckBox = new CheckBox("Filter by Employee"); f.addComponent(employeeCheckBox); CheckBox amountCheckBox = new CheckBox("Filter by Amount"); f.addComponent(amountCheckBox); CheckBox dateCheckBox = new CheckBox("Filter by Date"); f.addComponent(dateCheckBox); ComboBox areaComboBox = CommonComboBox.areas(); ComboBox employeeComboBox = CommonComboBox.getAllClients(); areaComboBox.setEnabled(false); f.addComponent(areaComboBox); areaCheckBox.addValueChangeListener((Property.ValueChangeEvent event) -> { areaComboBox.setEnabled((boolean) event.getProperty().getValue()); employeeComboBox.setEnabled(!(boolean) event.getProperty().getValue()); employeeCheckBox.setValue(!(boolean) event.getProperty().getValue()); isAreaSelect = (boolean) event.getProperty().getValue(); isEmployeeSelect = !(boolean) event.getProperty().getValue(); }); employeeComboBox.setEnabled(false); f.addComponent(employeeComboBox); employeeCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> { employeeComboBox.setEnabled((boolean) e.getProperty().getValue()); areaComboBox.setEnabled(!(boolean) e.getProperty().getValue()); areaCheckBox.setValue(!(boolean) e.getProperty().getValue()); isAreaSelect = !(boolean) e.getProperty().getValue(); isEmployeeSelect = (boolean) e.getProperty().getValue(); }); TextField amountField = new CommonTextField("Amount: "); amountField.addStyleName("align-right"); amountField.setEnabled(false); f.addComponent(amountField); amountCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> { amountField.setEnabled((boolean) e.getProperty().getValue()); isAmountSelect = (boolean) e.getProperty().getValue(); }); HorizontalLayout h = new HorizontalLayout(); h.setCaption("Date: "); h.setWidth("100%"); h.setSpacing(true); DateField from = new DateField(); from.setWidth("100%"); from.setEnabled(false); h.addComponent(from); DateField to = new DateField(); to.setWidth("100%"); to.setEnabled(false); h.addComponent(to); dateCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> { from.setEnabled((boolean) e.getProperty().getValue()); to.setEnabled((boolean) e.getProperty().getValue()); isDateSelect = (boolean) e.getProperty().getValue(); }); f.addComponent(h); Button generate = new CommonButton("Generate Report"); generate.addClickListener((Button.ClickEvent e) -> { if (!isEmployeeSelect && !isAmountSelect && !isDateSelect) { mrDataGrid.setContainerDataSource(new MaintenanceDataContainer()); mrDataGrid.setFrozenColumnCount(2); } if (isAreaSelect && !isAmountSelect && !isDateSelect) { if (areaComboBox.getValue() == null) { Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource( new MaintenanceDataContainer(areaComboBox.getItemCaption(areaComboBox.getValue()))); mrDataGrid.setFrozenColumnCount(2); } if (isEmployeeSelect && !isAmountSelect && !isDateSelect) { if (employeeComboBox.getValue() == null) { Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer((int) employeeComboBox.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (!isEmployeeSelect && isAmountSelect && !isDateSelect) { if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer( CommonUtilities.convertStringToDouble(amountField.getValue().trim()))); mrDataGrid.setFrozenColumnCount(2); } if (!isEmployeeSelect && !isAmountSelect && isDateSelect) { if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer(from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (isAreaSelect && isAmountSelect && !isDateSelect) { if (areaComboBox.getValue() == null) { Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE); return; } if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } mrDataGrid.setContainerDataSource( new MaintenanceDataContainer(areaComboBox.getItemCaption(areaComboBox.getValue()), CommonUtilities.convertStringToDouble(amountField.getValue().trim()))); mrDataGrid.setFrozenColumnCount(2); } if (isEmployeeSelect && isAmountSelect && !isDateSelect) { if (employeeComboBox.getValue() == null) { Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE); return; } if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer((int) employeeComboBox.getValue(), CommonUtilities.convertStringToDouble(amountField.getValue().trim()))); mrDataGrid.setFrozenColumnCount(2); } if (isAreaSelect && !isAmountSelect && isDateSelect) { if (areaComboBox.getValue() == null) { Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE); return; } if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer( areaComboBox.getItemCaption(areaComboBox.getValue()), from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (isEmployeeSelect && !isAmountSelect && isDateSelect) { if (employeeComboBox.getValue() == null) { Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE); return; } if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer((int) employeeComboBox.getValue(), from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (!isEmployeeSelect && isAmountSelect && isDateSelect) { if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer( CommonUtilities.convertStringToDouble(amountField.getValue().trim()), from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (isAreaSelect && isAmountSelect && isDateSelect) { if (areaComboBox.getValue() == null) { Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE); return; } if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource( new MaintenanceDataContainer(employeeComboBox.getItemCaption(employeeComboBox.getValue()), CommonUtilities.convertStringToDouble(amountField.getValue().trim()), from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } if (isEmployeeSelect && isAmountSelect && isDateSelect) { if (employeeComboBox.getValue() == null) { Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE); return; } if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) { Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE); return; } else { if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) { Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE); return; } } if (from.getValue() == null) { Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE); return; } if (to.getValue() == null) { Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE); return; } mrDataGrid.setContainerDataSource(new MaintenanceDataContainer((int) employeeComboBox.getValue(), CommonUtilities.convertStringToDouble(amountField.getValue().trim()), from.getValue(), to.getValue())); mrDataGrid.setFrozenColumnCount(2); } sub.close(); }); f.addComponent(generate); sub.setContent(f); sub.getContent().setHeightUndefined(); return sub; } @Override public void buttonClick(Button.ClickEvent event) { //TODO switch (event.getButton().getCaption()) { case "FILTER": { reset(); Window sub = filterReport(); if (sub.getParent() == null) { UI.getCurrent().addWindow(sub); } break; } default: { if (!isEmployeeSelect() && !isAmountSelect() && !isDateSelect()) { Window sub = exportLargeData(); if (sub.getParent() == null) { UI.getCurrent().addWindow(sub); } return; } processExportDataToExcel(); break; } } } public boolean isAreaSelect() { return isAreaSelect; } public boolean isEmployeeSelect() { return isEmployeeSelect; } public boolean isAmountSelect() { return isAmountSelect; } public boolean isDateSelect() { return isDateSelect; } void reset() { isEmployeeSelect = false; isAmountSelect = false; isDateSelect = false; } void processExportDataToExcel() { ExportDataGridToExcel export = new ExportDataGridToExcel(mrDataGrid); export.workSheet(); StreamResource.StreamSource source = () -> { try { File f = new File(export.getFilePath()); FileInputStream fis = new FileInputStream(f); return fis; } catch (Exception e) { ErrorLoggedNotification.showErrorLoggedOnWindow(e.toString(), this.getClass().getName()); return null; } }; Date date = new Date(); StreamResource resource = new StreamResource(source, "MaintenanceReport" + "-" + date.getTime() + ".xls"); resource.setMIMEType("application/vnd.ms-office"); Page.getCurrent().open(resource, null, false); } Window exportLargeData() { Window sub = new Window("EXPORT LARGE DATA"); sub.setWidth("300px"); sub.setModal(true); sub.center(); VerticalLayout v = new VerticalLayout(); v.setSizeFull(); v.setMargin(true); v.setSpacing(true); Label status = new Label("Exporting large amount of data will take longer and will eat a lot of memory.", ContentMode.HTML); status.setContentMode(ContentMode.HTML); v.addComponent(status); Button b = new CommonButton("PROCEED TO EXPORT?"); b.addClickListener((Button.ClickEvent e) -> { sub.close(); processExportDataToExcel(); }); v.addComponent(b); sub.setContent(v); sub.getContent().setHeightUndefined(); return sub; } }