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.employee.maintenance; import com.save.model.MaintenanceReimbursement; import com.save.common.CommonComboBox; import com.save.service.EmployeeService; import com.save.service.MRService; import com.save.service.RLService; import com.save.employee.serviceprovider.EmployeeServiceImpl; import com.save.employee.serviceprovider.MRServiceImpl; import com.save.employee.serviceprovider.RLServiceImpl; import com.save.utilities.CommonUtilities; import com.vaadin.data.Property; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; import com.vaadin.ui.DateField; import com.vaadin.ui.FormLayout; import com.vaadin.ui.Grid; import com.vaadin.ui.Notification; import com.vaadin.ui.Panel; import com.vaadin.ui.TextArea; import com.vaadin.ui.TextField; import com.vaadin.ui.Window; import com.vaadin.ui.themes.ValoTheme; /** * * @author jetdario */ public class MRFormWindow extends Window { EmployeeService es = new EmployeeServiceImpl(); RLService rs = new RLServiceImpl(); MRService mrs = new MRServiceImpl(); private int employeeId; private int mrId = 0; private Grid grid; private boolean view = false; MaintenanceReimbursement mr; ComboBox formType; ComboBox area; TextField plateNo; DateField dateCovered; TextField amount; TextArea description; Button actionButton; Button delete; Panel panel; public MRFormWindow(int employeeId) { this.employeeId = employeeId; setCaption("M/R WINDOW FORM"); setWidth("600px"); setModal(true); setResizable(false); center(); setContent(getFormPanel()); getContent().setHeightUndefined(); } public MRFormWindow(int employeeId, boolean view, int mrId) { this.employeeId = employeeId; this.view = view; this.mrId = mrId; setCaption("M/R WINDOW FORM"); setWidth("600px"); setModal(true); setResizable(false); center(); setContent(getFormPanel()); getContent().setHeightUndefined(); } public MRFormWindow(int employeeId, int mrId, Grid grid) { this.employeeId = employeeId; this.mrId = mrId; this.grid = grid; setCaption("M/R WINDOW FORM"); setWidth("600px"); setModal(true); setResizable(false); center(); setContent(getFormPanel()); getContent().setHeightUndefined(); } Panel getFormPanel() { panel = new Panel(); panel.setWidth("100%"); panel.setCaption("Maintenance Form"); panel.setContent(buildForms()); return panel; } FormLayout buildForms() { FormLayout form = new FormLayout(); form.setWidth("100%"); form.setMargin(true); formType = CommonComboBox.getFormType("Select Form Type.."); formType.setValue(1); formType.setWidth("60%"); form.addComponent(formType); area = CommonComboBox.areas(); area.setWidth("60%"); form.addComponent(area); plateNo = new TextField("Plate No: "); plateNo.setWidth("60%"); form.addComponent(plateNo); dateCovered = new DateField("Date Covered: "); dateCovered.setWidth("60%"); form.addComponent(dateCovered); amount = new TextField("Amount: "); amount.setWidth("60%"); amount.addStyleName("align-right"); form.addComponent(amount); description = new TextArea("Description: "); description.setWidth("100%"); description.setRows(3); form.addComponent(description); actionButton = new Button(); actionButton.setCaption("SAVE"); actionButton.setWidth("100%"); actionButton.addStyleName(ValoTheme.BUTTON_PRIMARY); actionButton.addClickListener(buttonClickListener); form.addComponent(actionButton); formType.addValueChangeListener((Property.ValueChangeEvent e) -> { if (e.getProperty().getValue().toString().equals("1")) { if (getMrId() != 0) { actionButton.setCaption("UPDATE"); } else { actionButton.setCaption("SAVE"); } panel.setCaption("MAINTENANCE FORM"); } else { if (getMrId() != 0) { actionButton.setCaption("UPDATE"); } else { actionButton.setCaption("SAVE"); } panel.setCaption("REIMBURSEMENT FORM"); } }); editForm(); viewForm(form); return form; } Button.ClickListener buttonClickListener = (Button.ClickEvent event) -> { if (area.getValue() == null) { requiredFields(); return; } if (plateNo.getValue() == null || plateNo.getValue().trim().isEmpty()) { requiredFields(); return; } if (dateCovered.getValue() == null) { requiredFields(); return; } if (description.getValue() == null || description.getValue().trim().isEmpty()) { requiredFields(); return; } if (amount.getValue() == null || amount.getValue().trim().isEmpty()) { requiredFields(); return; } else { if (!CommonUtilities.checkInputIfDouble(amount.getValue().trim())) { Notification.show("Enter numeric data for Amount", Notification.Type.ERROR_MESSAGE); return; } } mr = new MaintenanceReimbursement(); mr.setEmployeeId(getEmployeeId()); mr.setAreaId((int) area.getValue()); mr.setPlateNo(plateNo.getValue().toLowerCase()); mr.setDateCovered(dateCovered.getValue()); mr.setDescription(description.getValue().toLowerCase()); mr.setAmount(CommonUtilities.convertStringToDouble(amount.getValue())); mr.setFormType(formType.getItemCaption(formType.getValue())); switch (event.getButton().getCaption()) { case "UPDATE": { mr.setMaintenanceId(getMrId()); boolean result = mrs.updateMaintenanceReimbursement(mr); if (result) { getMRDataGrid().getContainerDataSource().removeAllItems(); getMRDataGrid().setContainerDataSource(new MRDataContainer(getEmployeeId())); clearFields(); close(); } break; } default: { boolean result = mrs.insertNewMaintenanceReimbursement(mr); if (result) { clearFields(); close(); } break; } } }; void requiredFields() { Notification.show("Required All Fields", Notification.Type.ERROR_MESSAGE); } int getEmployeeId() { return employeeId; } int getMrId() { return mrId; } Grid getMRDataGrid() { return grid; } boolean isViewable() { return view; } void clearFields() { plateNo.setValue(""); amount.setValue(""); description.setValue(""); Notification.show("Save " + formType.getItemCaption(formType.getValue()), Notification.Type.TRAY_NOTIFICATION); } void editForm() { if (getMrId() != 0) { actionButton.setCaption("UPDATE"); mr = mrs.getMaintenanceReimbursementById(getMrId()); formType.setValue((mr.getFormType().equals("maintenance")) ? 1 : 2); area.setValue(mr.getAreaId()); plateNo.setValue(mr.getPlateNo()); dateCovered.setValue(mr.getDateCovered()); amount.setValue(String.valueOf(mr.getAmount())); description.setValue(mr.getDescription()); } } void viewForm(FormLayout form) { if (isViewable()) { actionButton.setVisible(false); mr = mrs.getMaintenanceReimbursementById(getMrId()); formType.setValue(mr.getFormType()); formType.setEnabled(false); area.setValue(mr.getAreaId()); area.setEnabled(false); plateNo.setValue(mr.getPlateNo()); plateNo.setEnabled(false); dateCovered.setValue(mr.getDateCovered()); dateCovered.setEnabled(false); amount.setValue(String.valueOf(mr.getAmount())); amount.setEnabled(false); description.setValue(mr.getDescription()); description.setEnabled(false); form.setReadOnly(true); form.addStyleName("light"); } } }