package com.thingtrack.konekti.view.web.form.selector; import java.io.Serializable; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import com.vaadin.annotations.AutoGenerated; import com.vaadin.event.ItemClickEvent; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import com.thingtrack.konekti.domain.Product; import com.thingtrack.konekti.service.api.ProductService; import com.thingtrack.konekti.view.addon.data.BindingSource; import com.thingtrack.konekti.view.addon.ui.DataGridView; import com.thingtrack.konekti.view.kernel.IWorkbenchContext; import com.vaadin.event.ItemClickEvent.ItemClickListener; @SuppressWarnings("serial") public class ProductSelectorWindow extends Window { @AutoGenerated private VerticalLayout mainLayout; @AutoGenerated private HorizontalLayout hlToolButtons; @AutoGenerated private Button btnCancel; @AutoGenerated private Button btnSelect; @AutoGenerated private DataGridView dgProduct; /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ private Window parentWindow; private Product domainEntity; // selector service private ProductService productService; // selector datasource private BindingSource<Product> bsProduct = new BindingSource<Product>(Product.class, 0); private DialogResult dialogResultSelected; private IWorkbenchContext context; public enum DialogResult { SELECT, CANCEL } private static final boolean ACTIVE = 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 ProductSelectorWindow(IWorkbenchContext context, final Window parentWindow, Product entity, final CloseWindowDialogListener listener) { buildMainLayout(); setContent(mainLayout); this.context = context; this.parentWindow = parentWindow; this.domainEntity = entity; center(); setModal(true); setResizable(false); setClosable(false); setSizeUndefined(); this.addListener(new Window.CloseListener() { public void windowClose(Window.CloseEvent e) { if (listener != null) { listener.windowDialogClose(new CloseWindowDialogEvent(getWindow(), dialogResultSelected, domainEntity)); } } }); dgProduct.addListener(new ItemClickListener() { @Override public void itemClick(ItemClickEvent event) { if (event.getItemId() != null && event.isDoubleClick()) { btnSelect_Click(null); } } }); // get business service getServices(); // initialize container gridview initView(); // refresh grid container refreshBindindSource(); // select container selected if (entity != null) dgProduct.select(entity); this.parentWindow.addWindow(this); } private void initView() { try { dgProduct.setImmediate(true); dgProduct.setSelectable(true); dgProduct.setFilterBarVisible(); bsProduct.addNestedContainerProperty("productType.description"); bsProduct.addNestedContainerProperty("productSupplier.description"); dgProduct.setBindingSource(bsProduct); dgProduct.setVisibleColumns(new String[] { "productSupplier.description", "productType.description", "code", "name", "version" } ); dgProduct.setColumnHeaders(new String[] { "Proveedor", "Tipo", "Codigo", "Nombre", "Version" } ); } catch(Exception e) { throw new RuntimeException("¡Error al crear el grid de Productos!", e); } } private void refreshBindindSource() { try { bsProduct.removeAllItems(); bsProduct.addAll(productService.getAll(context.getUser(), ACTIVE)); } catch (IllegalArgumentException e) { throw new RuntimeException("¡No se pudo refrescar los productos!", e); } catch (Exception e) { throw new RuntimeException("¡No se pudo refrescar los productos!", e); } } @SuppressWarnings({ "rawtypes", "unchecked" }) private void getServices() { try { BundleContext bundleContext = FrameworkUtil.getBundle(ProductSelectorWindow.class).getBundleContext(); ServiceReference productServiceReference = bundleContext.getServiceReference(ProductService.class.getName()); this.productService = ProductService.class.cast(bundleContext.getService(productServiceReference)); } catch (Exception e) { e.getMessage(); } } /** * Select result button */ public void btnSelect_Click (Button.ClickEvent event) { domainEntity = bsProduct.getItemId(); dialogResultSelected = DialogResult.SELECT; this.parentWindow.removeWindow(this); } /** * Cancel result button */ public void btnCancel_Click (Button.ClickEvent event) { dialogResultSelected = DialogResult.CANCEL; this.parentWindow.removeWindow(this); } @AutoGenerated private VerticalLayout buildMainLayout() { // common part: create layout mainLayout = new VerticalLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("700px"); mainLayout.setHeight("350px"); mainLayout.setMargin(false); // top-level component properties setWidth("500px"); setHeight("250px"); // dgHole dgProduct = new DataGridView(); dgProduct.setImmediate(false); dgProduct.setWidth("100.0%"); dgProduct.setHeight("100.0%"); mainLayout.addComponent(dgProduct); mainLayout.setExpandRatio(dgProduct, 1.0f); // hlToolButtons hlToolButtons = buildHlToolButtons(); mainLayout.addComponent(hlToolButtons); return mainLayout; } @AutoGenerated private HorizontalLayout buildHlToolButtons() { // common part: create layout hlToolButtons = new HorizontalLayout(); hlToolButtons.setImmediate(false); hlToolButtons.setWidth("100.0%"); hlToolButtons.setHeight("-1px"); hlToolButtons.setMargin(false); hlToolButtons.setSpacing(true); // btnSelect btnSelect = new Button("Seleccionar", this, "btnSelect_Click"); btnSelect.setCaption("Seleccionar"); btnSelect.setImmediate(true); btnSelect.setWidth("-1px"); btnSelect.setHeight("-1px"); hlToolButtons.addComponent(btnSelect); hlToolButtons.setExpandRatio(btnSelect, 1.0f); hlToolButtons.setComponentAlignment(btnSelect, new Alignment(6)); // btnCancel btnCancel = new Button("Cancelar", this, "btnCancel_Click"); btnCancel.setCaption("Cancelar"); btnCancel.setImmediate(true); btnCancel.setWidth("-1px"); btnCancel.setHeight("-1px"); hlToolButtons.addComponent(btnCancel); hlToolButtons.setComponentAlignment(btnCancel, new Alignment(6)); return hlToolButtons; } public interface CloseWindowDialogListener extends Serializable { public void windowDialogClose(CloseWindowDialogEvent event); } public class CloseWindowDialogEvent extends CloseEvent { private final DialogResult dialogResult; private Product domainEntity; public CloseWindowDialogEvent(Component source) { super(source); this.dialogResult = DialogResult.CANCEL; } public CloseWindowDialogEvent(Component source, DialogResult dialogResult, Product domainEntity) { super(source); this.dialogResult = dialogResult; this.domainEntity = domainEntity; } /** * Gets the dialog window result * * @return the Source of the event. */ public DialogResult getDialogResult() { return this.dialogResult; } /** * Gets the dialog window result * * @return the Source of the event. */ public Product getDomainEntity() { return this.domainEntity; } } }