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.client; import com.save.area.ProvincePropertyChangeListener; import com.save.common.CommonComboBox; import com.save.model.Client; import com.save.service.AreaService; import com.save.service.ClientService; import com.save.area.serviceprovider.AreaServiceImpl; import com.save.serviceprovider.ClientServiceImpl; import com.vaadin.data.Property; 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.OptionGroup; import com.vaadin.ui.Panel; import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.Window; /** * * @author jetdario */ public class ClientInformationForm extends Panel { AreaService as = new AreaServiceImpl(); private static String EDIT_BUTTON_CAPTION = "EDIT INFORMATION"; private static String NEW_BUTTON_CAPTION = "NEW CLIENT"; private static String CANCEL_BUTTON_CAPTION = "CANCEL"; private boolean readOnly = true; private int clientId; private HorizontalSplitPanel hsplit; FormLayout form = new FormLayout(); TextField client; ComboBox province = CommonComboBox.provinces(); ComboBox city = new ComboBox("City/Town: "); TextField address; TextField landline; TextField mobile; OptionGroup setAsDistributor; ComboBox clientType = CommonComboBox.clientType(); Button editClientBtn; Button cancelBtn; Button newClientBtn; Button removeBtn; ClientService clientService = new ClientServiceImpl(); private int asDistributor; public ClientInformationForm(HorizontalSplitPanel hsplit, int clientId) { this.hsplit = hsplit; this.clientId = clientId; setCaption("Client Information"); setWidth("80%"); setContent(clientInformationForm()); } FormLayout clientInformationForm() { form.setMargin(false); form.setWidth("100%"); form.addStyleName("light"); form.setReadOnly(readOnly); form.setImmediate(true); editClientBtn = new Button(EDIT_BUTTON_CAPTION, editBtnListener); editClientBtn.setEnabled(false); cancelBtn = new Button(CANCEL_BUTTON_CAPTION, cancelBtnListener); cancelBtn.setEnabled(false); newClientBtn = new Button(NEW_BUTTON_CAPTION, newBtnListener); client = new TextField("Name: "); client.setWidth("50%"); client.setRequired(true); client.setRequiredError("Required Name!"); form.addComponent(client); province.setInputPrompt("Select Province.."); province.setRequired(true); province.setRequiredError("Required Province!"); city.setInputPrompt("Select City.."); city.setRequired(true); city.setRequiredError("Required City/Town!"); province.addValueChangeListener(new ProvincePropertyChangeListener(city)); province.setWidth("100%"); form.addComponent(province); city.setWidth("100%"); form.addComponent(city); address = new TextField("Street/Brgy: "); address.setWidth("50%"); address.setRequired(true); address.setRequiredError("Required Street/Brgy!"); form.addComponent(address); landline = new TextField("Landline No: "); landline.setWidth("50%"); landline.setRequired(true); landline.setRequiredError("Required Contact No!"); form.addComponent(landline); mobile = new TextField("Mobile No: "); mobile.setWidth("50%"); mobile.setRequired(true); mobile.setRequiredError("Required Contact No!"); form.addComponent(mobile); setAsDistributor = new OptionGroup("Distributor"); setAsDistributor.addItems("Yes", "No"); setAsDistributor.addStyleName("horizontal"); setAsDistributor.setRequired(readOnly); setAsDistributor.setRequiredError("Set option(Y/N) As Distrbutor!"); setAsDistributor.addValueChangeListener(distributorCheckboxListener); form.addComponent(setAsDistributor); clientType.setWidth("90%"); clientType.setRequired(true); clientType.setRequiredError("Required Client Type!"); clientType.setVisible(false); form.addComponent(clientType); if (getClientId() != 0) { editClientBtn.setEnabled(true); Client c = clientService.getClientDataById(getClientId()); client.setValue(c.getClientName().toUpperCase()); province.setValue(c.getProvince().getProvinceId()); city.setValue(c.getCityId()); address.setValue(c.getStreet()); landline.setValue(c.getLandline()); mobile.setValue(c.getMobile()); if (c.getAsDistributor() == 0) { setAsDistributor.setValue("No"); clientType.setVisible(true); clientType.setValue(c.getClientType()); } else { setAsDistributor.setValue("Yes"); clientType.setVisible(false); clientType.setValue("distributor"); } } form.setReadOnly(true); HorizontalLayout footer = new HorizontalLayout(); footer.setMargin(new MarginInfo(true, true, true, false)); footer.setSpacing(true); footer.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT); form.addComponent(footer); footer.addComponent(editClientBtn); footer.addComponent(cancelBtn); footer.addComponent(newClientBtn); removeBtn = new Button("Remove Account"); removeBtn.addClickListener(removeBtnListener); form.addComponent(removeBtn); disableFields(false); return form; } int getClientId() { return clientId; } HorizontalSplitPanel getHsplit() { return hsplit; } void clearForm() { client.setValue(""); address.setValue(""); landline.setValue(""); mobile.setValue(""); } Button.ClickListener editBtnListener = (Button.ClickEvent event) -> { boolean readOnly1 = form.isReadOnly(); if (readOnly1) { form.setReadOnly(false); form.removeStyleName("light"); event.getButton().setCaption("Update Client Information"); event.getButton().addStyleName("primary"); newClientBtn.setEnabled(false); cancelBtn.setEnabled(true); disableFields(true); } else { editClient(); form.setReadOnly(true); form.addStyleName("light"); event.getButton().setCaption(EDIT_BUTTON_CAPTION); event.getButton().removeStyleName("primary"); newClientBtn.setEnabled(true); cancelBtn.setEnabled(false); clientId = 0; disableFields(false); } }; Button.ClickListener newBtnListener = (Button.ClickEvent event) -> { boolean readOnly1 = form.isReadOnly(); if (readOnly1) { // refreshComboBox(); form.setReadOnly(false); form.removeStyleName("light"); event.getButton().setCaption("Save Client Information"); event.getButton().addStyleName("primary"); clearForm(); editClientBtn.setEnabled(false); cancelBtn.setEnabled(true); disableFields(true); } else { saveNewClient(); form.setReadOnly(true); form.addStyleName("light"); event.getButton().setCaption(NEW_BUTTON_CAPTION); event.getButton().removeStyleName("primary"); editClientBtn.setEnabled(true); cancelBtn.setEnabled(false); clientId = 0; disableFields(false); } }; Button.ClickListener cancelBtnListener = (Button.ClickEvent event) -> { boolean readOnly1 = form.isReadOnly(); if (readOnly1) { form.setReadOnly(false); form.removeStyleName("light"); } else { form.setReadOnly(true); form.addStyleName("light"); cancelBtn.setEnabled(false); newClientBtn.setEnabled(true); newClientBtn.setCaption(NEW_BUTTON_CAPTION); newClientBtn.removeStyleName("primary"); editClientBtn.setEnabled(true); editClientBtn.setCaption(EDIT_BUTTON_CAPTION); editClientBtn.removeStyleName("primary"); clientId = 0; } disableFields(false); }; Button.ClickListener removeBtnListener = new Button.ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { if (getClientId() == 0) { Notification.show("Select a Client to Remove!", Notification.Type.ERROR_MESSAGE); return; } Window sub = new RemoveAccountWindow(getClientId()); UI.getCurrent().addWindow(sub); sub.addCloseListener(new Window.CloseListener() { @Override public void windowClose(Window.CloseEvent e) { getHsplit().setFirstComponent(new ClientsDataGridProperties(getHsplit(), "client")); clearFields(); } }); } }; Property.ValueChangeListener distributorCheckboxListener = (Property.ValueChangeEvent event) -> { if (setAsDistributor.getValue() == null) { } else { if (setAsDistributor.getValue().equals("Yes")) { clientType.setVisible(false); asDistributor = 1; } else { clientType.setVisible(true); asDistributor = 0; } } }; void saveNewClient() { if (client.getValue() == null || client.getValue().trim().isEmpty()) { getNotification(); return; } if (province.getValue() == null) { getNotification(); return; } if (city.getValue() == null) { getNotification(); return; } if (address.getValue() == null || address.getValue().trim().isEmpty()) { getNotification(); return; } if (landline.getValue() == null || landline.getValue().trim().isEmpty()) { getNotification(); return; } if (mobile.getValue() == null || mobile.getValue().trim().isEmpty()) { getNotification(); return; } if (setAsDistributor.getValue() == null) { getNotification(); return; } if (asDistributor == 0 && clientType.getValue() == null) { getNotification(); return; } Client c = new Client(); c.setClientName(client.getValue().trim().toLowerCase()); c.setCityId((int) city.getValue()); c.setStreet(address.getValue().trim().toLowerCase()); c.setLandline(landline.getValue().trim()); c.setMobile(mobile.getValue().trim().toLowerCase()); c.setAsDistributor(asDistributor); c.setClientType(clientType.getItemCaption(clientType.getValue())); boolean result = clientService.insertNewClient(c); if (result) { getHsplit().setFirstComponent(new ClientsDataGridProperties(getHsplit(), "client")); } } void editClient() { if (getClientId() == 0) { Notification.show("Select a Client from Table", Notification.Type.WARNING_MESSAGE); return; } if (client.getValue() == null || client.getValue().trim().isEmpty()) { getNotification(); return; } if (province.getValue() == null) { getNotification(); return; } if (city.getValue() == null) { getNotification(); return; } if (address.getValue() == null || address.getValue().trim().isEmpty()) { getNotification(); return; } if (landline.getValue() == null || landline.getValue().trim().isEmpty()) { getNotification(); return; } if (mobile.getValue() == null || mobile.getValue().trim().isEmpty()) { getNotification(); return; } Client c = new Client(); c.setClientName(client.getValue().trim().toLowerCase()); c.setCityId((int) city.getValue()); c.setStreet(address.getValue().trim().toLowerCase()); c.setLandline(landline.getValue().trim()); c.setMobile(mobile.getValue().trim().toLowerCase()); c.setAsDistributor(asDistributor); if (asDistributor == 1) { c.setClientType("distributor"); } else { c.setClientType(clientType.getItemCaption(clientType.getValue())); } c.setClientId(getClientId()); boolean result = clientService.editClient(c); if (result) { getHsplit().setFirstComponent(new ClientsDataGridProperties(getHsplit(), "client")); } } void getNotification() { Notification.show("Complete Fields!", Notification.Type.ERROR_MESSAGE); } void refreshComboBox() { province.removeAllItems(); province = CommonComboBox.provinces(); city.removeAllItems(); } void clearFields() { client.setValue(""); address.setValue(""); landline.setValue(""); mobile.setValue(""); setAsDistributor.setValue(null); } void disableFields(boolean bool) { client.setEnabled(bool); province.setEnabled(bool); city.setEnabled(bool); address.setEnabled(bool); landline.setEnabled(bool); mobile.setEnabled(bool); setAsDistributor.setEnabled(bool); clientType.setEnabled(bool); } }