| 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.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.extension.model.extension.ExtensionModelFactory; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YKanban; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YKanbanEvent; |
| 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.ui.CustomComponent; |
| import com.vaadin.ui.VerticalLayout; |
| |
| @SuppressWarnings("serial") |
| public class KanbanBoardEmbeddable extends CustomComponent { |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(KanbanBoardEmbeddable.class); |
| |
| private final Class<?> type; |
| private final String dialogId; |
| private final String cardId; |
| |
| private IViewContext viewContext; |
| private YKanban yKanban; |
| private Consumer<YKanbanEvent> doubleClickConsumer; |
| private Consumer<YKanbanEvent> stateChangedConsumer; |
| |
| public KanbanBoardEmbeddable(Class<?> type, String dialogId, String cardId) { |
| this.type = type; |
| this.dialogId = dialogId; |
| this.cardId = cardId; |
| } |
| |
| 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); |
| } |
| |
| 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); |
| |
| yKanban = modelFactory.createYKanban(); |
| yKanban.setCardECViewId(cardId); |
| yKanban.setEditDialogId(dialogId); |
| |
| content.getElements().add(yKanban); |
| YVerticalLayoutCellStyle cellStyle = content.addCellStyle(yKanban); |
| cellStyle.setAlignment(YAlignment.FILL_FILL); |
| |
| yKanban.setType(type); |
| yKanban.setTypeQualifiedName(type.getCanonicalName()); |
| |
| yKanban.eAdapters().add(new AdapterCustom()); |
| |
| return view; |
| } |
| |
| public Consumer<YKanbanEvent> getDoubleClickConsumer() { |
| return doubleClickConsumer; |
| } |
| |
| public void setDoubleClickConsumer(Consumer<YKanbanEvent> doubleClickConsumer) { |
| this.doubleClickConsumer = doubleClickConsumer; |
| } |
| |
| public Consumer<YKanbanEvent> getStateChangedConsumer() { |
| return stateChangedConsumer; |
| } |
| |
| public void setStateChangedConsumer(Consumer<YKanbanEvent> stateChangedConsumer) { |
| this.stateChangedConsumer = stateChangedConsumer; |
| } |
| |
| @PreDestroy |
| public void dispose() { |
| if (viewContext != null) { |
| viewContext.dispose(); |
| viewContext = null; |
| } |
| } |
| |
| public IViewContext getViewContext() { |
| return viewContext; |
| } |
| |
| /** |
| * Refreshes the given card. |
| * |
| * @param dto |
| */ |
| public void refreshCard(Object dto) { |
| yKanban.setToRefresh(new YKanbanEvent(dto)); |
| } |
| |
| private class AdapterCustom extends AdapterImpl { |
| @Override |
| public void notifyChanged(Notification msg) { |
| if (msg.getFeature() == ExtensionModelPackage.Literals.YKANBAN__STATE_CHANGED) { |
| if (stateChangedConsumer != null) { |
| stateChangedConsumer.accept((YKanbanEvent) msg.getNewValue()); |
| } |
| } else if (msg.getFeature() == ExtensionModelPackage.Literals.YKANBAN__DOUBLE_CLICKED) { |
| if (doubleClickConsumer != null) { |
| doubleClickConsumer.accept((YKanbanEvent) msg.getNewValue()); |
| } |
| } |
| } |
| } |
| |
| } |