| /** |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.infogrid.vaaclipse; |
| |
| import java.util.List; |
| import java.util.Locale; |
| 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.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.dsl.dto.lib.services.IDTOService; |
| 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.types.ITypeProviderService; |
| 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.CustomComponent; |
| |
| @SuppressWarnings({ "restriction", "serial" }) |
| public class SingleInfoGridComponent extends CustomComponent implements |
| EditorSavedStrategy { |
| |
| final static Logger LOGGER = LoggerFactory |
| .getLogger(SingleInfoGridComponent.class); |
| |
| @Inject |
| IEventBroker eventBroker; |
| @Inject |
| IEclipseContext context; |
| @Inject |
| IGridSourceFacade gridSourceService; |
| @Inject |
| IECViewSessionHelper ecviewPropsProvider; |
| @Inject |
| ITypeProviderService bundleSpaceTypeProvider; |
| @Inject |
| Locale locale; |
| @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 { |
| // log.error("gridsource not available:" + gridSourceId); |
| // } |
| // return null; |
| // } |
| |
| @PostConstruct |
| protected void setup() { |
| |
| if (gridSourceId != null) { |
| descriptor = createGridSourceDescriptor(gridSourceId); |
| if (descriptor != null) { |
| layout = new CssLayout(); |
| layout.setSizeFull(); |
| Component grid = createComponent(gridSourceId); |
| layout.addComponent(grid); |
| setCompositionRoot(layout); |
| } 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(); |
| String topic = createTopic(); |
| CxGrid grid = descriptor.getGrid(); |
| |
| // if there is inputdata, just put it into the grids collection |
| if (inputdata != null) { |
| grid.getCollection().addAll(inputdata); |
| } |
| 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); |
| } |
| } |
| |
| protected String createTopic() { |
| String fqn = descriptor.getSource().getRootTypeFQN(); |
| fqn = fqn.substring(fqn.lastIndexOf(".") + 1, fqn.length()).replaceAll( |
| "Dto", ""); |
| return EventBrokerMsg.AUTOWIRED_ENTITY_PREFIX + fqn; |
| } |
| |
| /** |
| * 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; |
| } |
| }); |
| descriptor.setLocale(locale); |
| 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 List<?> getInputData() { |
| return inputdata; |
| } |
| |
| public void setInputData(List<?> inputData) { |
| this.inputdata = inputData; |
| } |
| |
| protected BundleContext getBundleContext() { |
| return FrameworkUtil.getBundle(getClass()).getBundleContext(); |
| } |
| |
| } |