Skip to content

Package: GenerateTableColumnsForSubclassesHandler

GenerateTableColumnsForSubclassesHandler

nameinstructionbranchcomplexitylinemethod
GenerateTableColumnsForSubclassesHandler()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
execute(EObject)
M: 11 C: 117
91%
M: 3 C: 11
79%
M: 3 C: 5
63%
M: 4 C: 30
88%
M: 0 C: 1
100%
execute(ExecutionEvent)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
shouldShow(EObject)
M: 0 C: 10
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.handler;
15:
16: import java.util.LinkedHashSet;
17: import java.util.Set;
18:
19: import org.eclipse.core.commands.ExecutionEvent;
20: import org.eclipse.core.commands.ExecutionException;
21: import org.eclipse.core.databinding.property.value.IValueProperty;
22: import org.eclipse.emf.common.command.Command;
23: import org.eclipse.emf.ecore.EAttribute;
24: import org.eclipse.emf.ecore.EClass;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecore.EReference;
27: import org.eclipse.emf.ecore.EStructuralFeature;
28: import org.eclipse.emf.ecp.view.internal.editor.controls.Activator;
29: import org.eclipse.emf.ecp.view.spi.editor.controls.Helper;
30: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
31: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
32: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
33: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
34: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
35: import org.eclipse.emf.ecp.view.spi.table.model.VTablePackage;
36: import org.eclipse.emf.ecp.view.spi.treemasterdetail.ui.swt.MasterDetailAction;
37: import org.eclipse.emf.edit.command.AddCommand;
38: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
39: import org.eclipse.emf.edit.domain.EditingDomain;
40: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
41: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
42: import org.eclipse.emfforms.spi.ide.view.segments.ToolingModeUtil;
43: import org.eclipse.jface.viewers.IStructuredSelection;
44: import org.eclipse.ui.handlers.HandlerUtil;
45:
46: /**
47: * @author jfaltermeier
48: *
49: */
50: public class GenerateTableColumnsForSubclassesHandler extends MasterDetailAction {
51:
52:         @Override
53:         public Object execute(ExecutionEvent event) throws ExecutionException {
54:                 final Object selection = ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).getFirstElement();
55:•                if (selection == null || !(selection instanceof EObject)) {
56:                         return null;
57:                 }
58:                 execute((EObject) selection);
59:                 return null;
60:         }
61:
62:         @Override
63:         public boolean shouldShow(EObject eObject) {
64:•                return VTableControl.class.isInstance(eObject) && !ToolingModeUtil.isSegmentToolingEnabled();
65:         }
66:
67:         @Override
68:         public void execute(EObject object) {
69:                 final VTableControl tableControl = VTableControl.class.cast(object);
70:                 final VDomainModelReference domainModelReference = tableControl.getDomainModelReference();
71:•                if (domainModelReference == null || !VTableDomainModelReference.class.isInstance(domainModelReference)) {
72:                         return;
73:                 }
74:
75:                 final VTableDomainModelReference tableDMR = (VTableDomainModelReference) domainModelReference;
76:                 final EClass rootEClass = Helper.getRootEClass(tableControl);
77:                 IValueProperty<?, ?> valueProperty;
78:                 try {
79:                         valueProperty = Activator.getDefault().getEMFFormsDatabinding().getValueProperty(tableDMR, rootEClass);
80:                 } catch (final DatabindingFailedException ex) {
81:                         Activator.getDefault().getReportService().report(new DatabindingFailedReport(ex));
82:                         return;
83:                 }
84:                 final Object eStructuralFeature = valueProperty.getValueType();
85:•                if (!EReference.class.isInstance(eStructuralFeature)) {
86:                         return;
87:                 }
88:                 final EReference eReference = (EReference) eStructuralFeature;
89:
90:                 final Set<EStructuralFeature> existingFeatures = new LinkedHashSet<EStructuralFeature>();
91:•                for (final VDomainModelReference ref : tableDMR.getColumnDomainModelReferences()) {
92:                         final VFeaturePathDomainModelReference featureDMR = (VFeaturePathDomainModelReference) ref;
93:                         existingFeatures.add(featureDMR.getDomainModelEFeature());
94:                 }
95:                 final Set<VDomainModelReference> referencesToAdd = new LinkedHashSet<VDomainModelReference>();
96:                 final EClass subclass = GenerateTableColumnsUtil.selectSubClass(eReference.getEReferenceType()).orElse(null);
97:•                if (subclass == null) {
98:                         return;
99:                 }
100:•                for (final EAttribute attribute : subclass.getEAllAttributes()) {
101:•                        if (existingFeatures.contains(attribute)) {
102:                                 continue;
103:                         }
104:                         final VFeaturePathDomainModelReference dmr = VViewFactory.eINSTANCE
105:                                 .createFeaturePathDomainModelReference();
106:                         dmr.setDomainModelEFeature(attribute);
107:                         referencesToAdd.add(dmr);
108:
109:                 }
110:                 final EditingDomain editingDomainFor = AdapterFactoryEditingDomain.getEditingDomainFor(object);
111:                 final Command command = AddCommand.create(editingDomainFor, tableDMR,
112:                         VTablePackage.eINSTANCE.getTableDomainModelReference_ColumnDomainModelReferences(), referencesToAdd);
113:                 editingDomainFor.getCommandStack().execute(command);
114:
115:         }
116: }