| /** |
| * |
| * 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.infogrid.vaaclipse; |
| |
| import java.util.List; |
| import java.util.Map; |
| |
| import javax.annotation.PostConstruct; |
| import javax.annotation.PreDestroy; |
| import javax.inject.Inject; |
| import javax.inject.Named; |
| |
| import org.eclipse.e4.core.contexts.IEclipseContext; |
| import org.eclipse.e4.core.di.annotations.Optional; |
| import org.eclipse.e4.core.services.events.IEventBroker; |
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.osbp.dsl.dto.lib.impl.DtoServiceAccess; |
| import org.eclipse.osbp.ecview.core.common.model.core.YView; |
| import org.eclipse.osbp.ecview.core.util.emf.ModelUtil; |
| import org.eclipse.osbp.ecview.extension.grid.CxGrid; |
| import org.eclipse.osbp.ecview.extension.grid.CxGridPackage; |
| import org.eclipse.osbp.eventbroker.EventBrokerMsg; |
| import org.eclipse.osbp.infogrid.api.IECViewGridSourceDescriptor; |
| import org.eclipse.osbp.infogrid.api.IECViewGridSourceDescriptor.EditorSavedStrategy; |
| import org.eclipse.osbp.infogrid.api.IGridSourceDescriptor; |
| import org.eclipse.osbp.infogrid.api.IGridSourceFacade; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSource; |
| import org.eclipse.osbp.runtime.common.filter.IDTOService; |
| import org.eclipse.osbp.runtime.common.types.ITypeProviderService; |
| import org.eclipse.osbp.ui.api.user.IUser; |
| import org.eclipse.osbp.vaaclipse.common.ecview.api.IECViewSessionHelper; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.CssLayout; |
| import com.vaadin.ui.VerticalLayout; |
| |
| @SuppressWarnings("restriction") |
| public class SingleInfoGridView implements EditorSavedStrategy { |
| |
| final static Logger LOGGER = LoggerFactory |
| .getLogger(SingleInfoGridView.class); |
| |
| @Inject |
| IEventBroker eventBroker; |
| @Inject |
| IEclipseContext context; |
| @Inject |
| VerticalLayout parentLayout; |
| @Inject |
| IGridSourceFacade gridSourceService; |
| @Inject |
| IECViewSessionHelper ecviewPropsProvider; |
| @Inject |
| MPart mPart; |
| @Inject |
| ITypeProviderService bundleSpaceTypeProvider; |
| @Inject |
| IUser user; |
| @Inject |
| @Optional |
| @Named("gridSourceId") |
| String gridSourceId; |
| @Inject |
| @Optional |
| @Named("inputdata") |
| private List<?> inputdata; |
| |
| protected CssLayout layout; |
| protected Component activeContent; |
| private IECViewGridSourceDescriptor descriptor; |
| private Class<?> rootType; |
| private IDTOService<Object> dtoService; |
| |
| public Component createGrid(String gridSourceId, |
| IGridSourceFacade gridSourceService, |
| IECViewSessionHelper ecviewPropsProvider, |
| ITypeProviderService bundleSpaceTypeProvider, IUser user) { |
| this.gridSourceService = gridSourceService; |
| this.ecviewPropsProvider = ecviewPropsProvider; |
| this.bundleSpaceTypeProvider = bundleSpaceTypeProvider; |
| |
| descriptor = createGridSourceDescriptor(gridSourceId); |
| descriptor.setLocale(user.getLocale()); |
| descriptor.setInput(inputdata); |
| if (descriptor != null) { |
| return createComponent(gridSourceId); |
| } else { |
| LOGGER.error("gridsource not available:" + gridSourceId); |
| } |
| return null; |
| } |
| |
| @PostConstruct |
| protected void setup() { |
| |
| // first we check the context, whether a gridSourceId was injected |
| if (gridSourceId == null || gridSourceId.isEmpty()) { |
| // if we could not find an injected gridSourceId, then lets check |
| // the mPart for it. |
| gridSourceId = mPart.getPersistedState().get("gridSourceId"); |
| } |
| |
| if (gridSourceId != null) { |
| descriptor = createGridSourceDescriptor(gridSourceId); |
| if (descriptor != null) { |
| layout = new CssLayout(); |
| layout.setSizeFull(); |
| parentLayout.setMargin(true); |
| parentLayout.setSpacing(true); |
| parentLayout.addComponent(layout); |
| Component grid = createComponent(gridSourceId); |
| layout.addComponent(grid); |
| } else { |
| LOGGER.error("gridsource not available:" + gridSourceId); |
| } |
| } |
| } |
| |
| protected Component createComponent(String gridSourceId) { |
| Component component = null; |
| if (descriptor != null) { |
| setupSaveStrategy(); |
| |
| component = (Component) descriptor.getComponent(); |
| component.setSizeFull(); |
| CxGrid grid = descriptor.getGrid(); |
| grid.eAdapters().add(new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification msg) { |
| if (msg.getFeature() == CxGridPackage.Literals.CX_GRID__SELECTION) { |
| // eventBroker.send(topic, msg.getNewValue()); |
| } |
| } |
| }); |
| } |
| return component; |
| } |
| |
| @SuppressWarnings("unchecked") |
| protected void setupSaveStrategy() { |
| descriptor.setEditorSavedStrategy(this); |
| dtoService = (IDTOService<Object>) DtoServiceAccess |
| .getService(rootType); |
| } |
| |
| @Override |
| public void editorSaved(Object value) { |
| if (dtoService != null) { |
| dtoService.update(value); |
| } |
| } |
| |
| /** |
| * Uses the IGridSourceService to create the descriptor that should be used |
| * for the given id. |
| * |
| * @param sourceId |
| * @return |
| */ |
| protected IECViewGridSourceDescriptor createGridSourceDescriptor( |
| String sourceId) { |
| |
| CxGridSource gridSource = gridSourceService.getSource(sourceId); |
| gridSource.getTags().add(IGridSourceDescriptor.TAG__SINGLE_GRID); |
| rootType = bundleSpaceTypeProvider.forName(null, |
| gridSource.getRootTypeFQN()); |
| IGridSourceDescriptor descriptor = gridSourceService.getDescriptor( |
| rootType, gridSource, |
| new IGridSourceDescriptor.ConfigCallback() { |
| @Override |
| public Map<String, Object> getProperties( |
| IGridSourceDescriptor descriptor) { |
| CxGridSource cxSource = descriptor.getSource(); |
| if (cxSource.getKind().equals( |
| IGridSourceDescriptor.KIND_ECVIEW)) { |
| return createECViewProperties(descriptor); |
| } |
| return null; |
| } |
| }); |
| |
| return (IECViewGridSourceDescriptor) descriptor; |
| } |
| |
| /** |
| * Creates the ecview properties to properly config the descriptor. |
| * |
| * @param descriptor |
| * @return |
| */ |
| protected Map<String, Object> createECViewProperties( |
| IGridSourceDescriptor descriptor) { |
| Map<String, Object> props = ecviewPropsProvider.createBasicProperties(); |
| if (inputdata != null) { |
| props.put(IECViewGridSourceDescriptor.PROP_MANUAL_BEANS, true); |
| } |
| return props; |
| } |
| |
| @PreDestroy |
| protected void destroy() { |
| if (descriptor != null) { |
| descriptor.dispose(); |
| descriptor = null; |
| } |
| } |
| |
| public void focus(){ |
| CxGrid cxGrid = descriptor.getGrid(); |
| YView yView = ModelUtil.getView(cxGrid); |
| yView.setCurrentFocus(cxGrid); |
| } |
| |
| public List<?> getInputData() { |
| return inputdata; |
| } |
| |
| public void setInputData(List<?> inputData) { |
| this.inputdata = inputData; |
| } |
| |
| protected BundleContext getBundleContext() { |
| return FrameworkUtil.getBundle(getClass()).getBundleContext(); |
| } |
| |
| } |