blob: c3a8c912342d79ce1845db36367d1f703134a2f5 [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.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.IEclipseContext;
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.ui.api.customfields.IBlobService;
import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService;
import org.eclipse.osbp.ui.api.themes.IThemeResourceService;
import org.eclipse.osbp.vaaclipse.common.ecview.ECViewToE4ServiceBridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.server.ClientConnector.AttachEvent;
import com.vaadin.server.ClientConnector.AttachListener;
import com.vaadin.server.ClientConnector.DetachEvent;
import com.vaadin.server.ClientConnector.DetachListener;
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;
import org.eclipse.osbp.runtime.common.annotations.DtoUtils;
/**
* The Class AbstractECViewDialog.
*
* @param <DTO>
* the generic type
*/
@SuppressWarnings("all")
public abstract class AbstractECViewDisplay<DTO> {
/** The Constant log. */
private final static Logger log = LoggerFactory.getLogger("vaaclipseDisplay");
/** The eclipse context. */
@Inject
private IEclipseContext eclipseContext;
@Inject
private IDSLMetadataService dslMetadataService;
@Inject
private IThemeResourceService themeResourceService;
@Inject
private IBlobService blobService;
/** The view context. */
// ecview stuff
private IViewContext viewContext;
/** The ecview component. */
private ECViewComponent ecviewComponent;
/** The rendering params. */
private Map<String, Object> renderingParams;
private AbstractOrderedLayout parent;
public AbstractECViewDisplay(UI ui, String fragment) {
}
public void handle(VerticalLayout parent, IEclipseContext context) {
this.parent = parent;
this.eclipseContext = eclipseContext;
createView();
}
private void createView() {
ecviewComponent = new ECViewComponent();
ecviewComponent.setSizeFull();
parent.addComponent(ecviewComponent);
renderingParams = createRenderingParams(blobService);
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);
}
}
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));
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();
}