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.request; import com.save.common.CommonComboBox; import com.save.model.LiquidationForm; import com.save.service.RLService; import com.save.employee.serviceprovider.RLServiceImpl; import com.save.utilities.CommonUtilities; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; import com.vaadin.ui.Component; import com.vaadin.ui.DateField; import com.vaadin.ui.Grid; import com.vaadin.ui.GridLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Notification; import com.vaadin.ui.TextArea; import com.vaadin.ui.TextField; 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 RequestFormWindow extends Window { RLService rls = new RLServiceImpl(); private int employeeId; private int requestId; private boolean liquidated; private Grid grid; TextField controlNo; DateField dateOfActivity; ComboBox area; TextField activity; TextField venue; TextArea requirements; TextField lodgingPax; TextField lodgingBudget; TextField lodgingAmount; TextField mealsPax; TextField mealsBudget; TextField mealsAmount; TextArea others; TextField othersAmount; public RequestFormWindow(int employeeId, int requestId, boolean liquidated, Grid grid) { this.employeeId = employeeId; this.requestId = requestId; this.liquidated = liquidated; this.grid = grid; setCaption("REQUEST FROM"); setWidth("700px"); setModal(true); setResizable(false); center(); setContent(buildRequestForm()); getContent().setHeightUndefined(); } Component buildRequestForm() { GridLayout glayout = new GridLayout(4, 7); glayout.setSizeFull(); glayout.setSpacing(true); glayout.setMargin(true); controlNo = new TextField("Control No."); controlNo.setWidth("100%"); controlNo.addStyleName(ValoTheme.TEXTFIELD_SMALL); glayout.addComponent(controlNo, 0, 0); dateOfActivity = new DateField("Date of Activity: "); dateOfActivity.setWidth("100%"); dateOfActivity.addStyleName(ValoTheme.DATEFIELD_SMALL); glayout.addComponent(dateOfActivity, 1, 0); area = CommonComboBox.areas(); area.setWidth("100%"); glayout.addComponent(area, 2, 0); activity = new TextField("Activity: "); activity.setWidth("100%"); activity.addStyleName(ValoTheme.TEXTFIELD_SMALL); glayout.addComponent(activity, 0, 1, 1, 1); venue = new TextField("Venue: "); venue.setWidth("100%"); venue.addStyleName(ValoTheme.TEXTFIELD_SMALL); glayout.addComponent(venue, 0, 2, 1, 2); requirements = new TextArea("Requirements: "); requirements.setWidth("100%"); requirements.addStyleName(ValoTheme.TEXTAREA_SMALL); glayout.addComponent(requirements, 2, 1, 3, 2); Label lodging = new Label("Lodging: "); lodging.setWidthUndefined(); glayout.addComponent(lodging, 0, 3); glayout.setComponentAlignment(lodging, Alignment.MIDDLE_CENTER); lodgingPax = new TextField("Pax: "); lodgingPax.setWidth("100%"); lodgingPax.setValue("0"); lodgingPax.addStyleName(ValoTheme.TEXTFIELD_SMALL); lodgingPax.addStyleName("align-right"); glayout.addComponent(lodgingPax, 1, 3); lodgingAmount = new TextField("Amount: "); lodgingAmount.setWidth("100%"); lodgingAmount.setValue("0.00"); lodgingAmount.addStyleName(ValoTheme.TEXTFIELD_SMALL); lodgingAmount.addStyleName("align-right"); lodgingAmount.setEnabled(false); lodgingAmount.setImmediate(true); glayout.addComponent(lodgingAmount, 3, 3); lodgingBudget = new TextField("Budget: "); lodgingBudget.setWidth("100%"); lodgingBudget.setValue("0.00"); lodgingBudget.addStyleName(ValoTheme.TEXTFIELD_SMALL); lodgingBudget.addStyleName("align-right"); lodgingBudget.addValueChangeListener((ValueChangeEvent event) -> { if (!CommonUtilities.checkInputIfInteger(lodgingPax.getValue().trim())) { Notification.show("Enter a numeric value for Pax", Notification.Type.ERROR_MESSAGE); return; } if (!CommonUtilities.checkInputIfDouble(lodgingBudget.getValue().trim())) { Notification.show("Enter a numeric value for Budget", Notification.Type.ERROR_MESSAGE); return; } lodgingAmount.setValue(String.valueOf(CommonUtilities.convertStringToInt(lodgingPax.getValue().trim()) * CommonUtilities.convertStringToDouble(lodgingBudget.getValue().trim()))); }); lodgingBudget.setImmediate(true); glayout.addComponent(lodgingBudget, 2, 3); Label meals = new Label("Meals: "); meals.setWidthUndefined(); glayout.addComponent(meals, 0, 4); glayout.setComponentAlignment(meals, Alignment.MIDDLE_CENTER); mealsPax = new TextField("Pax: "); mealsPax.setWidth("100%"); mealsPax.setValue("0"); mealsPax.addStyleName(ValoTheme.TEXTFIELD_SMALL); mealsPax.addStyleName("align-right"); glayout.addComponent(mealsPax, 1, 4); mealsAmount = new TextField("Amount: "); mealsAmount.setWidth("100%"); mealsAmount.setValue("0.00"); mealsAmount.addStyleName(ValoTheme.TEXTFIELD_SMALL); mealsAmount.addStyleName("align-right"); mealsAmount.setEnabled(false); mealsAmount.setImmediate(liquidated); glayout.addComponent(mealsAmount, 3, 4); mealsBudget = new TextField("Budget: "); mealsBudget.setWidth("100%"); mealsBudget.setValue("0.00"); mealsBudget.addStyleName(ValoTheme.TEXTFIELD_SMALL); mealsBudget.addStyleName("align-right"); mealsBudget.addValueChangeListener((ValueChangeEvent event) -> { if (!CommonUtilities.checkInputIfInteger(mealsPax.getValue().trim())) { Notification.show("Enter a numeric value for Pax", Notification.Type.ERROR_MESSAGE); return; } if (!CommonUtilities.checkInputIfDouble(mealsBudget.getValue().trim())) { Notification.show("Enter a numeric value for Budget", Notification.Type.ERROR_MESSAGE); return; } mealsAmount.setValue(String.valueOf(CommonUtilities.convertStringToInt(mealsPax.getValue().trim()) * CommonUtilities.convertStringToDouble(mealsBudget.getValue().trim()))); }); mealsAmount.setImmediate(true); glayout.addComponent(mealsBudget, 2, 4); others = new TextArea("Others: "); others.setWidth("100%"); others.setRows(3); others.addStyleName(ValoTheme.TEXTAREA_SMALL); glayout.addComponent(others, 0, 5, 2, 6); othersAmount = new TextField("Amount: "); othersAmount.setWidth("100%"); othersAmount.setValue("0.00"); othersAmount.addStyleName(ValoTheme.TEXTFIELD_SMALL); othersAmount.addStyleName("align-right"); glayout.addComponent(othersAmount, 3, 5); glayout.setComponentAlignment(othersAmount, Alignment.TOP_CENTER); Button rlButton = new Button(); rlButton.setCaption("SAVE"); rlButton.setWidth("100%"); rlButton.addStyleName(ValoTheme.BUTTON_PRIMARY); rlButton.addStyleName(ValoTheme.BUTTON_SMALL); rlButton.addClickListener(buttonClickListener); glayout.addComponent(rlButton, 3, 6); glayout.setComponentAlignment(rlButton, Alignment.TOP_CENTER); if (getRequestId() != 0) { LiquidationForm lf = rls.getRLById(getRequestId()); controlNo.setValue(String.valueOf(lf.getControlNo())); dateOfActivity.setValue(lf.getDateOfActivity()); area.setValue(lf.getAreaId()); activity.setValue(lf.getActivity()); requirements.setValue(lf.getRequirements()); venue.setValue(lf.getVenue()); others.setValue(lf.getOthersRemarks()); othersAmount.setValue(String.valueOf(lf.getOthersAmount())); lodgingAmount.setValue(String.valueOf(lf.getLodgingAmount())); mealsAmount.setValue(String.valueOf(lf.getMealsAmount())); if (getLiquidated() == true) { setCaption("LIQUIDATE FROM"); rlButton.setCaption("LIQUIDATE"); controlNo.setReadOnly(liquidated); dateOfActivity.setReadOnly(liquidated); area.setReadOnly(liquidated); activity.setReadOnly(liquidated); requirements.setReadOnly(liquidated); venue.setReadOnly(liquidated); } else { rlButton.setCaption("EDIT"); lodgingPax.setValue(String.valueOf(lf.getLodgingPax())); lodgingBudget.setValue(String.valueOf(lf.getLodgingBudget())); mealsPax.setValue(String.valueOf(lf.getMealsPax())); mealsBudget.setValue(String.valueOf(lf.getMealsBudget())); rlButton.setEnabled(rls.getRLById(getRequestId()).getLiquidated() == 0); } // delete.setVisible(true); } return glayout; } int getEmployeeId() { return employeeId; } int getRequestId() { return requestId; } Button.ClickListener buttonClickListener = (Button.ClickEvent event) -> { if (controlNo.getValue() == null || controlNo.getValue().trim().isEmpty()) { Notification.show("Add Control No.", Notification.Type.ERROR_MESSAGE); return; } if (dateOfActivity.getValue() == null) { Notification.show("Add Date of Activity!", Notification.Type.ERROR_MESSAGE); return; } if (area.getValue() == null) { Notification.show("Add Area!", Notification.Type.ERROR_MESSAGE); return; } if (activity.getValue() == null || activity.getValue().trim().isEmpty()) { Notification.show("Add Activity", Notification.Type.ERROR_MESSAGE); return; } if (venue.getValue() == null || venue.getValue().trim().isEmpty()) { Notification.show("Add Venue", Notification.Type.ERROR_MESSAGE); return; } if (requirements.getValue() == null || requirements.getValue().trim().isEmpty()) { Notification.show("Add Requirements!", Notification.Type.ERROR_MESSAGE); return; } if (others.getValue() == null || others.getValue().trim().isEmpty()) { Notification.show("Add Others Remarks", Notification.Type.ERROR_MESSAGE); return; } LiquidationForm l = new LiquidationForm(); l.setControlNo(controlNo.getValue().trim()); l.setAreaId((int) area.getValue()); l.setActivity(activity.getValue().trim()); l.setDateOfActivity(dateOfActivity.getValue()); l.setVenue(venue.getValue().trim()); l.setRequirements(requirements.getValue().trim()); l.setLodgingPax(CommonUtilities.convertStringToInt(lodgingPax.getValue().trim())); l.setLodgingBudget(CommonUtilities.convertStringToDouble(lodgingBudget.getValue().trim())); l.setLodgingAmount(CommonUtilities.convertStringToDouble(lodgingAmount.getValue().trim())); l.setMealsPax(CommonUtilities.convertStringToInt(mealsPax.getValue().trim())); l.setMealsBudget(CommonUtilities.convertStringToDouble(mealsBudget.getValue().trim())); l.setMealsAmount(CommonUtilities.convertStringToDouble(mealsAmount.getValue().trim())); l.setOthersRemarks(others.getValue().trim()); l.setOthersAmount(CommonUtilities.convertStringToDouble(othersAmount.getValue().trim())); switch (event.getButton().getCaption()) { case "SAVE": { if (rls.isControlNoExist(controlNo.getValue().trim())) { Notification.show("Control No. already Exist!", Notification.Type.ERROR_MESSAGE); return; } l.setEmployeeId(getEmployeeId()); boolean result = rls.insertNewRequest(l); if (result) { getRLDataGrid().getContainerDataSource().removeAllItems(); getRLDataGrid().setContainerDataSource(new RLDataContainer(getEmployeeId())); close(); } break; } case "LIQUIDATE": { l.setRequestId(getRequestId()); boolean result = rls.liquidateRequest(l); if (result) { getRLDataGrid().getContainerDataSource().removeAllItems(); getRLDataGrid().setContainerDataSource(new RLDataContainer(getEmployeeId())); close(); } break; } default: { l.setRequestId(getRequestId()); boolean result = rls.editRequest(l); if (result) { getRLDataGrid().getContainerDataSource().removeAllItems(); getRLDataGrid().setContainerDataSource(new RLDataContainer(getEmployeeId())); close(); } break; } } }; boolean getLiquidated() { return liquidated; } Window deleteRequestForm() { Window sub = new Window("DELETE REQUEST"); sub.setWidth("280px"); sub.setModal(true); sub.center(); VerticalLayout v = new VerticalLayout(); v.setWidth("100%"); v.setMargin(true); Button delete = new Button(); delete.setCaption("CONFIRM DELETE REQUEST?"); delete.setWidth("100%"); delete.addStyleName(ValoTheme.BUTTON_PRIMARY); delete.addStyleName(ValoTheme.BUTTON_SMALL); delete.addClickListener((Button.ClickEvent event) -> { boolean result = rls.deleteRequestById(getRequestId()); if (result) { sub.close(); close(); } }); v.addComponent(delete); sub.setContent(v); sub.getContent().setHeightUndefined(); return sub; } Grid getRLDataGrid() { return grid; } }