package com.thingtrack.konekti.view.web.form.field; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.vaadin.addon.customfield.CustomField; import org.vaadin.peter.buttongroup.ButtonGroup; import com.thingtrack.konekti.domain.EmployeeAgent; import com.thingtrack.konekti.service.api.EmployeeAgentService; import com.thingtrack.konekti.view.addon.ui.WindowDialog; import com.thingtrack.konekti.view.addon.ui.WindowDialog.DialogResult; import com.thingtrack.konekti.view.kernel.IWorkbenchContext; import com.thingtrack.konekti.view.web.form.EmployeeAgentViewForm; import com.thingtrack.konekti.view.web.form.selector.EmployeeAgentSelectorWindow; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Property; import com.vaadin.terminal.ThemeResource; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.TextField; import com.vaadin.ui.Button.ClickEvent; @SuppressWarnings("serial") public class EmployeeAgentSelectorField extends CustomField { @AutoGenerated private HorizontalLayout mainLayout; @AutoGenerated private Button btnSearchEntity; @AutoGenerated private Button btnClearEntity; @AutoGenerated private Button btnAddEntity; @AutoGenerated private TextField employeeAgentField; /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ private IWorkbenchContext context; private EmployeeAgent employeeAgent; private EmployeeAgentService employeeAgentService; private boolean clearEntityVisible = true; private boolean addEntityVisible = true; /** * The constructor should first build the main layout, set the * composition root and then do any custom initialization. * * The constructor will not be automatically regenerated by the * visual editor. */ public EmployeeAgentSelectorField() { buildMainLayout(); setCompositionRoot(mainLayout); // TODO add user code here getServices(); employeeAgentField.setNullRepresentation(""); employeeAgentField.setRequiredError(employeeAgentField.getCaption() + " es un campo requerido"); btnSearchEntity.addListener(new ClickListener() { @Override public void buttonClick(ClickEvent event) { try { @SuppressWarnings("unused") EmployeeAgentSelectorWindow windowDialog = new EmployeeAgentSelectorWindow(context, getApplication().getMainWindow(), employeeAgent, new EmployeeAgentSelectorWindow.CloseWindowDialogListener() { public void windowDialogClose(EmployeeAgentSelectorWindow.CloseWindowDialogEvent event) { if (event.getDialogResult() != EmployeeAgentSelectorWindow.DialogResult.SELECT) return ; employeeAgent = event.getDomainEntity(); employeeAgentField.setValue(employeeAgent.getName() + employeeAgent.getSurname()); } }); } catch (Exception e) { throw new RuntimeException("¡No se pudo abrir el selector de personal!", e); } } }); btnClearEntity.addListener(new ClickListener() { @Override public void buttonClick(ClickEvent event) { employeeAgent = null; employeeAgentField.setValue(null); } }); btnAddEntity.addListener(new ClickListener() { @Override public void buttonClick(ClickEvent event) { EmployeeAgent employeeAgent = new EmployeeAgent(); try { @SuppressWarnings("unused") WindowDialog<EmployeeAgent> windowDialog = new WindowDialog<EmployeeAgent>(getApplication().getMainWindow(), "Nuevo Empleado", "Guardar", DialogResult.SAVE, "Cancelar", DialogResult.CANCEL, new EmployeeAgentViewForm(context), employeeAgent, new WindowDialog.CloseWindowDialogListener<EmployeeAgent>() { public void windowDialogClose(WindowDialog<EmployeeAgent>.CloseWindowDialogEvent<EmployeeAgent> event) { if (event.getDialogResult() != WindowDialog.DialogResult.SAVE) return ; try { EmployeeAgent savingEmployeeAgent = event.getDomainEntity(); EmployeeAgent savedEmployeeAgent = employeeAgentService.save(savingEmployeeAgent); savingEmployeeAgent.setAgentId(savedEmployeeAgent.getAgentId()); employeeAgentField.setValue(savingEmployeeAgent.getName()); EmployeeAgentSelectorField.this.employeeAgent = savingEmployeeAgent; } catch (Exception e) { throw new RuntimeException( "¡No se pudo crear el nuevo empleado!", e); } } }); } catch (IllegalArgumentException e) { throw new RuntimeException( "¡No se pudo abrir el formulario Nuevo Empleado!", e); } catch (Exception e) { throw new RuntimeException( "¡No se pudo abrir el formulario Nuevo Empleado!", e); } } }); btnClearEntity.setVisible(clearEntityVisible); btnAddEntity.setVisible(addEntityVisible); } @Override public void focus() { employeeAgentField.focus(); } @SuppressWarnings({ "unchecked", "rawtypes" }) private void getServices() { try { BundleContext bundleContext = FrameworkUtil.getBundle(ProductSelectorField.class).getBundleContext(); ServiceReference employeeAgentServiceReference = bundleContext.getServiceReference(EmployeeAgentService.class.getName()); employeeAgentService = EmployeeAgentService.class.cast(bundleContext.getService(employeeAgentServiceReference)); } catch (Exception e) { e.getMessage(); } } public void setContext(IWorkbenchContext context) { this.context = context; } @Override public void setPropertyDataSource(Property newDataSource) { employeeAgent = (EmployeeAgent) newDataSource.getValue(); if (employeeAgent != null) employeeAgentField.setValue(employeeAgent.getName()); super.setPropertyDataSource(newDataSource); } @Override public Object getValue() { return employeeAgent; } @Override public Class<?> getType() { return EmployeeAgent.class; } /** * @return the clearEntityVisible */ public boolean isClearEntityVisible() { return clearEntityVisible; } /** * @param clearEntityVisible the clearEntityVisible to set */ public void setClearEntityVisible(boolean clearEntityVisible) { this.clearEntityVisible = clearEntityVisible; btnClearEntity.setVisible(clearEntityVisible); } /** * @return the addEntityVisible */ public boolean isAddEntityVisible() { return addEntityVisible; } /** * @param addEntityVisible the addEntityVisible to set */ public void setAddEntityVisible(boolean addEntityVisible) { this.addEntityVisible = addEntityVisible; btnAddEntity.setVisible(addEntityVisible); } @AutoGenerated private HorizontalLayout buildMainLayout() { // common part: create layout mainLayout = new HorizontalLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("-1px"); mainLayout.setHeight("28px"); mainLayout.setMargin(false); // top-level component properties setWidth("-1px"); setHeight("28px"); // holeField employeeAgentField = new TextField(); employeeAgentField.setImmediate(false); employeeAgentField.setWidth("150px"); employeeAgentField.setHeight("-1px"); mainLayout.addComponent(employeeAgentField); ButtonGroup editionButtonGroup = new ButtonGroup(); // btnSearch btnSearchEntity = new Button(); btnSearchEntity.setIcon(new ThemeResource("../konekti/images/icons/container-module/magnifier-zoom.png")); btnSearchEntity.setImmediate(true); btnSearchEntity.setWidth("-1px"); btnSearchEntity.setHeight("-1px"); editionButtonGroup.addButton(btnSearchEntity); btnClearEntity = new Button(); btnClearEntity.setIcon(new ThemeResource("../konekti/images/icons/eraser.png")); btnClearEntity.setImmediate(true); btnClearEntity.setWidth("-1px"); btnClearEntity.setHeight("-1px"); editionButtonGroup.addButton(btnClearEntity); btnAddEntity = new Button(); btnAddEntity.setIcon(new ThemeResource("../konekti/images/icons/plus-circle.png")); btnAddEntity.setImmediate(true); btnAddEntity.setWidth("-1px"); btnAddEntity.setHeight("-1px"); editionButtonGroup.addButton(btnAddEntity); mainLayout.addComponent(editionButtonGroup); mainLayout.setExpandRatio(editionButtonGroup, 1.0f); return mainLayout; } }