blob: c639310e36c48ed9939f0d2cb7426f4637ad42e2 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.dialogdsl.common;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.translation.TranslationService;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import org.eclipse.osbp.ecview.core.common.model.core.YView;
import org.eclipse.osbp.ecview.extension.model.YStrategyLayout;
import org.eclipse.osbp.ecview.extension.presentation.vaadin.components.common.ECViewComponent;
import org.eclipse.osbp.runtime.common.annotations.DtoUtils;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.ui.api.customfields.IBlobService;
import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService;
import org.eclipse.osbp.vaaclipse.common.ecview.ECViewToE4ServiceBridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
/**
* The Class AbstractECViewDialog.
*
* @param <DTO>
* the generic type
*/
@SuppressWarnings("all")
public abstract class AbstractDisplay<DTO> {
/** The Constant log. */
protected final static Logger log = LoggerFactory.getLogger("displays");
private AbstractOrderedLayout parent;
/** The eclipse context. */
@Inject
protected IEclipseContext eclipseContext;
/** The view context. */
protected IViewContext viewContext;
/** The ecview component. */
protected ECViewComponent ecviewComponent;
/** The rendering params. */
protected Map<String, Object> renderingParams;
/** The blob service. */
protected IBlobService blobService;
/** The translator class service. */
protected IDSLMetadataService dslMetadataService;
/** The translation service. */
private II18nService i18nService;
public AbstractDisplay(UI ui, String fragment) {
}
public void handle(VerticalLayout parent, IEclipseContext context) {
this.parent = parent;
this.eclipseContext = eclipseContext;
this.blobService = eclipseContext.get(IBlobService.class);
this.dslMetadataService = eclipseContext.get(IDSLMetadataService.class);
this.i18nService = eclipseContext.get(II18nService.class);
createView(parent);
}
protected void createView(VerticalLayout parent) {
ecviewComponent = new ECViewComponent();
ecviewComponent.setSizeFull();
parent.addComponent(ecviewComponent);
renderingParams = createRenderingParams(blobService);
ecviewComponent.setLocale((Locale)eclipseContext.get(TranslationService.LOCALE));
viewContext = ecviewComponent.setValue(getViewId(), createNew(getBaseDtoName()), renderingParams);
if (viewContext == null) {
String message = "ViewContext could not be set for '" + getViewId() + "'";
log.error(message);
Notification.show(message, Type.ERROR_MESSAGE);
} else {
YView yView = (YView) viewContext.getViewEditpart().getModel();
if (yView == null) {
Notification.show("Display could not be found! " + getViewId(), Type.ERROR_MESSAGE);
return;
}
YStrategyLayout yStrategyLayoutT = findLayout(yView);
}
}
protected void createComponents() {
}
public void dispose() {
removeOldView();
}
public void sendData(Map<String, Object> data) {
if (viewContext == null) {
return;
}
Object currentDto = viewContext.getBean(IViewContext.MAIN_BEAN_SLOT);
if (currentDto != null) {
for(Entry<String, Object> entry:data.entrySet()) {
String[] tokens = entry.getKey().split("/");
if(currentDto.getClass().getCanonicalName().equals(tokens[0])) {
DtoUtils.setValue(currentDto, tokens[1], entry.getValue());
}
}
viewContext.setBean(IViewContext.MAIN_BEAN_SLOT, currentDto);
}
}
/**
* Removes the old view.
*/
private void removeOldView() {
if (viewContext != null) {
viewContext.dispose();
}
if (parent.getComponentCount() > 0) {
parent.removeAllComponents();
}
}
public YStrategyLayout findLayout(YView yView) {
for (Iterator<EObject> iterator = EcoreUtil
.getAllContents(yView, false); iterator.hasNext();) {
EObject type = (EObject) iterator.next();
if (type instanceof YStrategyLayout) {
return (YStrategyLayout) type;
}
}
return null;
}
/**
* Creates the rendering parameters.
*
* @return the map
*/
protected Map<String, Object> createRenderingParams(IBlobService blobService) {
Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> services = new HashMap<String, Object>();
params.put(IViewContext.PARAM_SERVICES, services);
params.put(IViewContext.PROP_SLOT, IViewContext.MAIN_BEAN_SLOT);
params.put(IViewContext.PARAM_THIRDPARTY_SERVICE_PROVIDER, new ECViewToE4ServiceBridge(eclipseContext));
services.put(II18nService.class.getName(), i18nService);
params.put(IViewContext.PRESET_LOCALE, (Locale)eclipseContext.get(TranslationService.LOCALE));
return params;
}
protected Object createNew(String typeName) {
Object newDto = null;
Class<?> clazz = null;
try {
clazz = dslMetadataService.createType(typeName);
} catch (ClassNotFoundException e) {
log.error("{}", e);
return null;
}
try {
newDto = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
log.error("{}", e);
return null;
}
ecviewComponent.setValue(getViewId(), newDto, renderingParams);
if (viewContext != null) {
viewContext.setBean(IViewContext.MAIN_BEAN_SLOT, newDto);
}
return newDto;
}
/**
* Gets the view id.
*
* @return the view id
*/
protected abstract String getViewId();
/**
* Gets the full qualified dto name.
*
* @return the base class dto name
*/
protected abstract String getBaseDtoName();
/**
* Gets the full qualified entity name.
*
* @return the base class entity name
*/
protected abstract String getBaseEntityName();
}