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.Supplier;
import com.thingtrack.konekti.service.api.SupplierService;
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 SupplierSelectorWindow extends Window {
@AutoGenerated
private VerticalLayout mainLayout;
@AutoGenerated
private HorizontalLayout hlToolButtons;
@AutoGenerated
private Button btnCancel;
@AutoGenerated
private Button btnSelect;
@AutoGenerated
private DataGridView dgSupplier;
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
private IWorkbenchContext context;
private Window parentWindow;
private Supplier domainEntity;
// selector service
private SupplierService supplierService;
// selector datasource
private BindingSource<Supplier> bsSupplier = new BindingSource<Supplier>(Supplier.class, 0);
private DialogResult dialogResultSelected;
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 SupplierSelectorWindow(IWorkbenchContext context, final Window parentWindow, Supplier 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));
}
}
});
dgSupplier.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)
dgSupplier.select(entity);
this.parentWindow.addWindow(this);
}
private void initView() {
try {
dgSupplier.setImmediate(true);
dgSupplier.setSelectable(true);
dgSupplier.setFilterBarVisible();
bsSupplier.addNestedContainerProperty("supplierType.description");
bsSupplier.addNestedContainerProperty("supplierGroup.description");
dgSupplier.setBindingSource(bsSupplier);
dgSupplier.setVisibleColumns(new String[] { "supplierGroup.description", "code", "name", "vat", "supplierType.description", "active" } );
dgSupplier.setColumnHeaders(new String[] { "Grupo", "Codigo", "Nombre", "CIF", "Tipo", "Activo" } );
}
catch(Exception e) {
throw new RuntimeException("¡Error al crear el grid de Proveedores!", e);
}
}
private void refreshBindindSource() {
try {
bsSupplier.removeAllItems();
bsSupplier.addAll(supplierService.getAll(context.getUser(), ACTIVE));
} catch (IllegalArgumentException e) {
throw new RuntimeException("¡No se pudo refrescar los proveedores!", e);
} catch (Exception e) {
throw new RuntimeException("¡No se pudo refrescar los proveedores!", e);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void getServices() {
try {
BundleContext bundleContext = FrameworkUtil.getBundle(SupplierSelectorWindow.class).getBundleContext();
ServiceReference supplierServiceReference = bundleContext.getServiceReference(SupplierService.class.getName());
this.supplierService = SupplierService.class.cast(bundleContext.getService(supplierServiceReference));
}
catch (Exception e) {
e.getMessage();
}
}
/**
* Select result button
*/
public void btnSelect_Click (Button.ClickEvent event) {
domainEntity = bsSupplier.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
dgSupplier = new DataGridView();
dgSupplier.setImmediate(false);
dgSupplier.setWidth("100.0%");
dgSupplier.setHeight("100.0%");
mainLayout.addComponent(dgSupplier);
mainLayout.setExpandRatio(dgSupplier, 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 Supplier domainEntity;
public CloseWindowDialogEvent(Component source) {
super(source);
this.dialogResult = DialogResult.CANCEL;
}
public CloseWindowDialogEvent(Component source, DialogResult dialogResult, Supplier 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 Supplier getDomainEntity() {
return this.domainEntity;
}
}
}