| package org.eclipse.osbp.runtime.web.vaadin.components.fields.filter2; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.Locale; |
| import java.util.function.Consumer; |
| |
| import javax.annotation.PostConstruct; |
| import javax.inject.Inject; |
| |
| import org.eclipse.osbp.ecview.core.common.context.ILocaleChangedService; |
| import org.eclipse.osbp.ecview.core.common.context.IViewContext; |
| import org.eclipse.osbp.runtime.common.i18n.II18nService; |
| import org.eclipse.osbp.runtime.common.util.BeanUtils; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.BeanServiceLazyLoadingContainer; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchService; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchServiceFactory; |
| import org.vaadin.gridutil.renderer.BooleanRenderer; |
| |
| import com.vaadin.data.Container.Filter; |
| import com.vaadin.ui.CustomComponent; |
| import com.vaadin.ui.Grid; |
| import com.vaadin.ui.Grid.Column; |
| import com.vaadin.ui.Grid.SelectionMode; |
| import com.vaadin.ui.VerticalLayout; |
| import com.vaadin.ui.renderers.DateRenderer; |
| import com.vaadin.ui.renderers.NumberRenderer; |
| |
| @SuppressWarnings("serial") |
| public class FilteringGridComponent<T> extends CustomComponent implements ILocaleChangedService.LocaleListener { |
| |
| final IViewContext viewContext; |
| |
| Class<T> type; |
| |
| VerticalLayout mainLayout; |
| Grid grid; |
| |
| IBeanSearchService<T> service; |
| BeanServiceLazyLoadingContainer<T> container; |
| |
| private List<Object> columns = new ArrayList<>(); |
| |
| private Consumer<T> selectionConsumer; |
| |
| private II18nService i18nService; |
| |
| @Inject |
| public FilteringGridComponent(IViewContext viewContext) { |
| this.viewContext = viewContext; |
| } |
| |
| @SuppressWarnings("unchecked") |
| @PostConstruct |
| public void init(Class<T> type, IBeanSearchServiceFactory factory) { |
| this.type = type; |
| service = factory.createService(type); |
| container = new BeanServiceLazyLoadingContainer<T>(service, type, null); |
| |
| mainLayout = new VerticalLayout(); |
| mainLayout.setSizeFull(); |
| setCompositionRoot(mainLayout); |
| |
| grid = new Grid(); |
| grid.setSizeFull(); |
| mainLayout.addComponent(grid); |
| |
| grid.setContainerDataSource(container); |
| grid.removeAllColumns(); |
| |
| grid.addSelectionListener(e -> { |
| if (selectionConsumer != null) { |
| selectionConsumer.accept((T) grid.getSelectedRow()); |
| } |
| }); |
| |
| ILocaleChangedService service = viewContext.getService(ILocaleChangedService.class.getName()); |
| service.addLocaleListener(this); |
| |
| i18nService = viewContext.getService(II18nService.ID); |
| } |
| |
| public void dispose() { |
| ILocaleChangedService service = viewContext.getService(ILocaleChangedService.class.getName()); |
| service.removeLocaleListener(this); |
| } |
| |
| public void setSelectionMode(SelectionMode mode) { |
| if (grid == null) { |
| throw new IllegalStateException("Call #init before"); |
| } |
| grid.setSelectionMode(mode); |
| } |
| |
| public void addColumn(String propertyId) { |
| if (columns.contains(propertyId)) { |
| return; |
| } |
| |
| if (propertyId.contains(".")) { |
| container.addNestedContainerProperty(propertyId); |
| } |
| |
| columns.add(propertyId); |
| grid.setColumns(columns.toArray(new Object[columns.size()])); |
| |
| Column col = grid.getColumn(propertyId); |
| if (BeanUtils.isBoolean(type, propertyId)) { |
| col.setRenderer(new BooleanRenderer()); |
| } else if (BeanUtils.isDecimal(type, propertyId)) { |
| col.setRenderer(new NumberRenderer()); |
| } else if (BeanUtils.isDate(type, propertyId)) { |
| col.setRenderer(new DateRenderer()); |
| } |
| |
| } |
| |
| protected Class<?> getType(String propertyId) { |
| return BeanUtils.getNestedFieldType(type, propertyId); |
| } |
| |
| /** |
| * Sets a filter to the container. |
| * |
| * @param filter |
| */ |
| public void setFilter(Filter filter) { |
| container.removeAllContainerFilters(); |
| if (filter != null) { |
| container.addContainerFilter(filter); |
| } |
| } |
| |
| public void removeAllColumns() { |
| columns.clear(); |
| grid.removeAllColumns(); |
| } |
| |
| public void setSelectionConsumer(Consumer<T> selectionConsumer) { |
| this.selectionConsumer = selectionConsumer; |
| } |
| |
| @Override |
| public void localeChanged(Locale locale) { |
| for (Column col : grid.getColumns()) { |
| col.setHeaderCaption( |
| Util.getCaption(i18nService, (String) col.getPropertyId(), (String) col.getPropertyId(), locale)); |
| } |
| } |
| } |