| package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common; |
| |
| import java.util.Locale; |
| import java.util.function.Consumer; |
| |
| import javax.annotation.PreDestroy; |
| |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.osbp.ecview.core.common.beans.AbstractSlot; |
| import org.eclipse.osbp.ecview.core.common.context.ContextException; |
| import org.eclipse.osbp.ecview.core.common.context.IViewContext; |
| import org.eclipse.osbp.ecview.core.common.model.core.CoreModelFactory; |
| import org.eclipse.osbp.ecview.core.common.model.core.YAlignment; |
| import org.eclipse.osbp.ecview.core.common.model.core.YView; |
| import org.eclipse.osbp.ecview.core.common.util.ECViewUtil; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelFactory; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YFilteringComponent; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YVerticalLayout; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YVerticalLayoutCellStyle; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.VaadinRenderer; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.data.Container.Filter; |
| import com.vaadin.ui.CustomComponent; |
| import com.vaadin.ui.VerticalLayout; |
| |
| @SuppressWarnings("serial") |
| public class FilteringComponentEmbeddable<T> extends CustomComponent { |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(FilteringComponentEmbeddable.class); |
| |
| private final Class<T> type; |
| private final int depth; |
| |
| private boolean hideGrid; |
| private IViewContext viewContext; |
| private Consumer<T> acceptConsumer; |
| private YFilteringComponent yFilteringComp; |
| private int filterCols; |
| |
| private Consumer<Filter> acceptFilterConsumer; |
| |
| public FilteringComponentEmbeddable(Class<T> type, int depth) { |
| this(type, depth, 2); |
| } |
| |
| public FilteringComponentEmbeddable(Class<T> type, int depth, int filterCols) { |
| this.type = type; |
| this.depth = depth; |
| this.filterCols = filterCols; |
| } |
| |
| /** |
| * This consumer forwards the accept operation. Eg to forward the selection |
| * to the EventBroker. |
| * |
| * @param acceptConsumer |
| */ |
| public void setAcceptCallback(Consumer<T> acceptConsumer) { |
| this.acceptConsumer = acceptConsumer; |
| } |
| |
| /** |
| * If the filter is accepted, then this consumer is called. |
| * |
| * @param acceptFilterConsumer |
| */ |
| public void setFilterAcceptedCallback(Consumer<Filter> acceptFilterConsumer) { |
| this.acceptFilterConsumer = acceptFilterConsumer; |
| } |
| |
| /** |
| * Hides the grid. |
| * @return |
| */ |
| public boolean isHideGrid() { |
| return hideGrid; |
| } |
| |
| /** |
| * Hides the grid. |
| * @param hideGrid |
| */ |
| public void setHideGrid(boolean hideGrid) { |
| this.hideGrid = hideGrid; |
| } |
| |
| public void init(Locale locale) { |
| VerticalLayout content = new VerticalLayout(); |
| content.setMargin(true); |
| content.setSpacing(true); |
| setCompositionRoot(content); |
| |
| VerticalLayout renderTarget = new VerticalLayout(); |
| |
| content.addComponent(renderTarget); |
| content.setExpandRatio(renderTarget, 1.0f); |
| |
| YView yView = createViewModel(); |
| |
| VaadinRenderer renderer = new VaadinRenderer(); |
| try { |
| viewContext = renderer.render(renderTarget, yView, null); |
| } catch (ContextException e1) { |
| LOGGER.error("{}", e1); |
| } |
| viewContext.setLocale(locale); |
| |
| // create the bean slot eager |
| AbstractSlot slot = (AbstractSlot) viewContext.createBeanSlot(yFilteringComp.getSelectionBeanSlotName(), type); |
| slot.addPropertyChangeListener(e -> { |
| if (acceptConsumer != null) { |
| @SuppressWarnings("unchecked") |
| T selection = (T) viewContext.getBean(yFilteringComp.getSelectionBeanSlotName()); |
| acceptConsumer.accept(selection); |
| } |
| }); |
| } |
| |
| /** |
| * Returns the filter. |
| * |
| * @return |
| */ |
| public Filter getFilter() { |
| return (Filter) yFilteringComp.getFilter(); |
| } |
| |
| private YView createViewModel() { |
| |
| CoreModelFactory coreModelFactory = CoreModelFactory.eINSTANCE; |
| ExtensionModelFactory modelFactory = ExtensionModelFactory.eINSTANCE; |
| |
| YView view = coreModelFactory.createYView(); |
| view.setMargin(false); |
| |
| YVerticalLayout content = modelFactory.createYVerticalLayout(); |
| view.setContent(content); |
| |
| content.setMargin(false); |
| content.setSpacing(false); |
| |
| yFilteringComp = modelFactory.createYFilteringComponent(); |
| yFilteringComp.setMargin(false); |
| yFilteringComp.setSpacing(false); |
| yFilteringComp.setHideGrid(hideGrid); |
| |
| CustomAdapter adapter = new CustomAdapter(); |
| yFilteringComp.eAdapters().add(adapter); |
| |
| content.getElements().add(yFilteringComp); |
| YVerticalLayoutCellStyle cellStyle = content.addCellStyle(yFilteringComp); |
| cellStyle.setAlignment(YAlignment.FILL_FILL); |
| |
| yFilteringComp.setFilterCols(filterCols); |
| ECViewUtil.fill(type, depth, yFilteringComp); |
| |
| return view; |
| } |
| |
| @PreDestroy |
| public void dispose() { |
| if (viewContext != null) { |
| viewContext.dispose(); |
| viewContext = null; |
| } |
| } |
| |
| public IViewContext getViewContext() { |
| return viewContext; |
| } |
| |
| class CustomAdapter extends AdapterImpl { |
| @Override |
| public void notifyChanged(Notification notification) { |
| if (notification.getFeature() == ExtensionModelPackage.Literals.YFILTERING_COMPONENT__FILTER) { |
| if (acceptFilterConsumer != null) { |
| acceptFilterConsumer.accept(getFilter()); |
| } |
| } |
| } |
| } |
| } |