| /** |
| * |
| * 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.lang.reflect.Field; |
| import java.util.Iterator; |
| import java.util.LinkedHashMap; |
| 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.e4.ui.model.application.ui.advanced.MPerspective; |
| 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.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.annotations.DtoUtils; |
| import org.eclipse.osbp.runtime.common.event.EventDispatcherEvent; |
| import org.eclipse.osbp.runtime.common.event.EventDispatcherEvent.EventDispatcherDataTag; |
| import org.eclipse.osbp.runtime.common.event.IEventDispatcher; |
| import org.eclipse.osbp.runtime.common.event.SelectionStore; |
| import org.eclipse.osbp.runtime.common.filter.IDTOService; |
| import org.eclipse.osbp.runtime.common.types.ITypeProviderService; |
| import org.eclipse.osbp.utils.common.EntityUtils; |
| 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("selectionEvnt") |
| LinkedHashMap<String, EventDispatcherEvent> selectionEvntList; |
| @Inject |
| @Optional |
| @Named("inputdata") |
| private List<?> inputdata; |
| @Inject |
| private IEventDispatcher eventDispatcher; |
| |
| protected CssLayout layout; |
| protected Component activeContent; |
| private IECViewGridSourceDescriptor descriptor; |
| private Class<?> rootType; |
| private IDTOService<Object> dtoService; |
| |
| private MPart mPart; |
| |
| private MPerspective mPerspective; |
| |
| @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(); |
| CxGrid grid = descriptor.getGrid(); |
| |
| mPart = context.get(MPart.class); |
| mPerspective = context.get(MPerspective.class); |
| |
| // 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) { |
| int idx = 0; |
| Iterator<EventDispatcherEvent> selectionEventIterator = selectionEvntList.values().iterator(); |
| EventDispatcherEvent selectionEvnt; |
| while (selectionEventIterator.hasNext()) { |
| selectionEvnt = selectionEventIterator.next(); |
| // msg.getNewValue() provides a dto object |
| if(msg.getNewValue() != null) { |
| // The first selection event is from the parent dto ... |
| Object idFieldValue = null; |
| if (idx == 0) { |
| idFieldValue = DtoUtils.getIdValue(msg.getNewValue()); |
| selectionEvnt.addItem(EventDispatcherDataTag.ID, idFieldValue); |
| selectionEvnt.setPerspective(mPerspective); |
| SelectionStore.putSelectionToPerspectiveContext(mPart, selectionEvnt.getTopic(), idFieldValue); |
| } else { |
| // ... the rest is from the referenced dtos |
| List<Field> refs = DtoUtils.getOwnerDomainReferences(msg.getNewValue().getClass()); |
| for (Field field : refs) { |
| if (!field.getDeclaringClass().getName().equals(field.getType().getName())){ |
| Object fieldValue = DtoUtils.getValue(msg.getNewValue(), field.getName()); |
| idFieldValue = fieldValue != null ? DtoUtils.getIdValue(fieldValue) : null; |
| String topic = selectionEvnt.getTopic(); |
| selectionEvnt.setPerspective(mPerspective); |
| String entityName = EntityUtils.getQualifiedEntityNameForQualifiedDtoName(field.getType().getName()); |
| if (topic!=null && topic.startsWith(entityName)){ |
| selectionEvnt.addItem(EventDispatcherDataTag.ID, idFieldValue); |
| break; |
| } |
| } |
| }; |
| } |
| } else { |
| selectionEvnt.addItem(EventDispatcherDataTag.ID, null); |
| break; |
| } |
| if(!selectionEvnt.getData().isEmpty()) { |
| eventDispatcher.sendEvent(selectionEvnt); |
| } |
| idx++; |
| } |
| } |
| } |
| }); |
| } |
| 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; |
| } |
| }); |
| 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; |
| } |
| } |
| |
| @Override |
| 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; |
| } |
| |
| public void refreshData() { |
| descriptor.refreshData(); |
| } |
| |
| protected BundleContext getBundleContext() { |
| return FrameworkUtil.getBundle(getClass()).getBundleContext(); |
| } |
| |
| } |