Skip to content

Package: RuleConditionDmrNewModelElementStrategyProvider$Strategy

RuleConditionDmrNewModelElementStrategyProvider$Strategy

nameinstructionbranchcomplexitylinemethod
RuleConditionDmrNewModelElementStrategyProvider.Strategy(RuleConditionDmrNewModelElementStrategyProvider)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createNewModelElement(EObject, EReference)
M: 20 C: 47
70%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 5 C: 9
64%
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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.handler;
15:
16: import java.text.MessageFormat;
17:
18: import org.eclipse.core.runtime.IStatus;
19: import org.eclipse.emf.ecore.EClass;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EReference;
22: import org.eclipse.emf.ecp.ui.view.swt.reference.CreateNewModelElementStrategy;
23: import org.eclipse.emf.ecp.ui.view.swt.reference.CreateNewModelElementStrategy.Provider;
24: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
25: import org.eclipse.emf.ecp.view.spi.editor.controls.EStructuralFeatureSelectionValidator;
26: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
27: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
28: import org.eclipse.emf.ecp.view.spi.rule.RuleConditionDmrUtil;
29: import org.eclipse.emf.ecp.view.spi.rule.model.Condition;
30: import org.eclipse.emfforms.bazaar.Create;
31: import org.eclipse.emfforms.common.Optional;
32: import org.eclipse.emfforms.spi.common.report.AbstractReport;
33: import org.eclipse.emfforms.spi.common.report.ReportService;
34: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
35: import org.eclipse.emfforms.spi.ide.view.segments.ToolingModeUtil;
36: import org.eclipse.jface.wizard.WizardDialog;
37: import org.eclipse.swt.widgets.Display;
38: import org.osgi.service.component.annotations.Component;
39: import org.osgi.service.component.annotations.Reference;
40:
41: /**
42: * Provides a strategy to create and configure a <strong>segment-based</strong> {@link VDomainModelReference} for a rule
43: * {@link Condition}. This provider is only active if the IDE tooling runs in segment mode.
44: * <p>
45: * Overwrite {@link #getSelectionValidator()} to customize valid feature selections for specific condition types.
46: *
47: * @author Lucas Koehler
48: *
49: */
50: @Component(name = "RuleConditionDmrNewModelElementStrategyProvider", property = "service.ranking:Integer=30")
51: public class RuleConditionDmrNewModelElementStrategyProvider
52:         extends ReferenceServiceCustomizationVendor<CreateNewModelElementStrategy> implements Provider {
53:
54:         private EMFFormsDatabindingEMF databinding;
55:         private ReportService reportService;
56:
57:         /**
58:          * Set the {@link EMFFormsDatabindingEMF}.
59:          *
60:          * @param databinding The {@link EMFFormsDatabindingEMF}
61:          */
62:         @Reference(unbind = "-")
63:         protected void setEMFFormsDatabindingEMF(EMFFormsDatabindingEMF databinding) {
64:                 this.databinding = databinding;
65:         }
66:
67:         /**
68:          * Set the {@link ReportService}.
69:          *
70:          * @param reportService The {@link ReportService}
71:          */
72:         @Reference(unbind = "-")
73:         protected void setReportService(ReportService reportService) {
74:                 this.reportService = reportService;
75:         }
76:
77:         @Override
78:         protected boolean handles(EObject owner, EReference reference) {
79:                 return isSegmentToolingEnabled()
80:                         && owner instanceof Condition
81:                         && reference.getEReferenceType() == VViewPackage.Literals.DOMAIN_MODEL_REFERENCE;
82:         }
83:
84:         /**
85:          * @return true if segment based tooling is enabled
86:          */
87:         protected boolean isSegmentToolingEnabled() {
88:                 return ToolingModeUtil.isSegmentToolingEnabled();
89:         }
90:
91:         /**
92:          * Returns the {@link EStructuralFeatureSelectionValidator} used by the DMR creation wizard to validate the
93:          * structural feature selection.
94:          * <p>
95:          * Overwrite this to customize valid selections
96:          *
97:          * @return The {@link EStructuralFeatureSelectionValidator}
98:          */
99:         protected EStructuralFeatureSelectionValidator getSelectionValidator() {
100:                 return feature -> null;
101:         }
102:
103:         /**
104:          * Creates the {@link CreateNewModelElementStrategy}.
105:          *
106:          * @return The created {@link CreateNewModelElementStrategy}
107:          */
108:         @Create
109:         public CreateNewModelElementStrategy createCreateNewModelElementStrategy() {
110:                 return new Strategy();
111:         }
112:
113:         /**
114:          * This strategy allows to create a new segment-based child dmr in a {@link CreateSegmentDmrWizard}.
115:          *
116:          * @author Lucas Koehler
117:          */
118:         class Strategy implements CreateNewModelElementStrategy {
119:
120:                 @Override
121:                 public Optional<EObject> createNewModelElement(EObject owner, EReference reference) {
122:                         final java.util.Optional<EClass> rootEClass = RuleConditionDmrUtil.getDmrRootEClass(databinding,
123:                                 reportService, owner);
124:•                        if (!rootEClass.isPresent()) {
125:                                 final String message = MessageFormat.format(
126:                                         "Could not create a new domain model reference for condition ''{0}'' because no root EClass could be determined.", //$NON-NLS-1$
127:                                         owner);
128:                                 reportService.report(new AbstractReport(message, IStatus.WARNING));
129:                                 return Optional.empty();
130:                         }
131:
132:                         final CreateSegmentDmrWizard dmrWizard = new CreateSegmentDmrWizard(rootEClass.get(),
133:                                 "New Domain Model Reference", null, getSelectionValidator()); //$NON-NLS-1$
134:
135:                         final WizardDialog wizardDialog = new WizardDialog(Display.getDefault().getActiveShell(), dmrWizard);
136:                         wizardDialog.setBlockOnOpen(true);
137:                         wizardDialog.open();
138:
139:                         return Optional.fromJavaOptional(dmrWizard.getDomainModelReference().map(EObject.class::cast));
140:                 }
141:         }
142: }