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; import com.save.common.CommonComboBox; import com.save.common.DepartmentComboBox; import com.save.common.PositionComboBox; import com.save.model.EmployeePositionHistory; import com.save.service.EmployeeService; import com.save.employee.serviceprovider.EmployeeServiceImpl; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; import com.vaadin.ui.FormLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.HorizontalSplitPanel; import com.vaadin.ui.Notification; import com.vaadin.ui.TextArea; /** * * @author jetdario */ public class EmploymentInformationForm extends FormLayout { private static String EDIT_BUTTON_CAPTION = "EDIT INFORMATION"; private static String CANCEL_BTN_CAPTION = "CANCEL"; private static String ADD_BUTTON_CAPTION = "ADD INFORMATION"; private static String FORM_CAPTION = "employment information form"; EmployeeService es = new EmployeeServiceImpl(); HorizontalSplitPanel hsplit; private int employeeId; private boolean readOnly = true; private boolean isEditClicked = true; private boolean isAddClicked = true; ComboBox position; ComboBox department; ComboBox employmentStatus; TextArea remarks; Button editBtn; // Button addBtn; Button cancelBtn; HorizontalLayout footer; public EmploymentInformationForm(HorizontalSplitPanel hsplit, int employeeId) { this.hsplit = hsplit; this.employeeId = employeeId; setMargin(new MarginInfo(true, true, false, false)); setWidth("80%"); addStyleName("light"); setReadOnly(readOnly); setImmediate(true); editBtn = new Button(EDIT_BUTTON_CAPTION); editBtn.addClickListener(editBtnListener); editBtn.setImmediate(true); cancelBtn = new Button(CANCEL_BTN_CAPTION, cancelBtnListener); cancelBtn.setImmediate(true); cancelBtn.setEnabled(false); // addBtn = new Button(ADD_BUTTON_CAPTION, addBtnListener); // addBtn.setImmediate(true); // Label section = new Label("Employment Info"); // section.addStyleName("h1"); // section.addStyleName("colored"); // addComponent(section); position = new PositionComboBox(); addComponent(position); department = new DepartmentComboBox(); addComponent(department); employmentStatus = CommonComboBox.employmentStatus("Status: "); addComponent(employmentStatus); remarks = new TextArea("Remarks: "); remarks.setWidth("100%"); remarks.setRequired(true); remarks.setRequiredError("Required Remarks"); addComponent(remarks); if (getEmployeeId() != 0) { EmployeePositionHistory eph = es.getEmployeesEmploymentDataById(getEmployeeId()); position.setValue(eph.getPosition()); department.setValue(eph.getDepartment()); employmentStatus.setValue(eph.getEmploymentStatus()); remarks.setValue(eph.getRemarks()); } footer = new HorizontalLayout(); footer.setMargin(new MarginInfo(true, true, true, false)); footer.setSpacing(true); footer.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT); addComponent(footer); footer.addComponent(editBtn); footer.addComponent(cancelBtn); // footer.addComponent(addBtn); // Label lastModified = new Label("Last modified by you a minute ago"); // lastModified.addStyleName("light"); // footer.addComponent(lastModified); disableFields(false); } int getEmployeeId() { return employeeId; } Button.ClickListener editBtnListener = (Button.ClickEvent event) -> { readOnly = isReadOnly(); if (readOnly) { setReadOnly(false); removeStyleName("light"); event.getButton().setCaption("SAVE INFORMATION"); event.getButton().addStyleName("primary"); disableFields(true); cancelBtn.setEnabled(true); } else { setReadOnly(true); addStyleName("light"); event.getButton().setCaption(EDIT_BUTTON_CAPTION); event.getButton().removeStyleName("primary"); disableFields(false); cancelBtn.setEnabled(false); } if (event.getButton().getCaption().equals(EDIT_BUTTON_CAPTION)) { if (position.getValue() == null) { return; } if (department.getValue() == null) { return; } if (employmentStatus.getValue() == null) { return; } // if(remarks.getValue() == null || remarks.getValue().trim().isEmpty()){ return; } EmployeePositionHistory eph = new EmployeePositionHistory(); eph.setPosition(position.getValue().toString()); eph.setDepartment(department.getValue().toString()); eph.setEmploymentStatus(employmentStatus.getValue().toString()); // eph.setRemarks(remarks.getValue().trim()); eph.setEmployeeId(getEmployeeId()); if (es.updateEmploymentInformation(eph)) { Notification.show("EDIT Employement Info", Notification.Type.TRAY_NOTIFICATION); } } }; Button.ClickListener cancelBtnListener = (Button.ClickEvent event) -> { setReadOnly(true); addStyleName("light"); disableFields(false); editBtn.setCaption(EDIT_BUTTON_CAPTION); editBtn.removeStyleName("primary"); editBtn.setEnabled(true); // addBtn.setEnabled(true); cancelBtn.setEnabled(false); // isEditClicked = true; // isAddClicked = true; }; // Button.ClickListener addBtnListener = (Button.ClickEvent event) -> { // readOnly = isReadOnly(); // if (readOnly) { // setReadOnly(false); // removeStyleName("light"); // event.getButton().setCaption("SAVE INFORMATION"); // event.getButton().addStyleName("primary"); // disableFields(true); // editBtn.setEnabled(false); // isAddClicked = false; // cancelBtn.setEnabled(true); // } else { // setReadOnly(true); // addStyleName("light"); // event.getButton().setCaption(ADD_BUTTON_CAPTION); // event.getButton().removeStyleName("primary"); // disableFields(false); // editBtn.setEnabled(true); // isAddClicked = true; // cancelBtn.setEnabled(true); // } // // if(position.getValue() == null){ return; } // if(department.getValue() == null){ return; } // if(employmentStatus.getValue() == null){ return; } // if(remarks.getValue() == null || remarks.getValue().trim().isEmpty()){ return; } // // EmployeePositionHistory eph = new EmployeePositionHistory(); // eph.setPosition(position.getValue().toString().toLowerCase()); // eph.setDepartment(department.getValue().toString().toLowerCase()); // eph.setEmploymentStatus(employmentStatus.getValue().toString().toLowerCase()); // eph.setRemarks(remarks.getValue().trim().toLowerCase()); // eph.setEmployeeId(getEmployeeId()); // // if(isAddClicked){ // boolean result = employeeService.updateEmploymentInformation(eph, false); // if(result){ // Notification.show("ADD Employement Info", Notification.Type.TRAY_NOTIFICATION); // } // } // }; HorizontalSplitPanel getHsplit() { return hsplit; } void disableFields(boolean bool) { position.setEnabled(bool); department.setEnabled(bool); employmentStatus.setEnabled(bool); remarks.setEnabled(bool); } }