Skip to content

Package: RuleConditionDmrOpenInNewContextStrategyProvider$Strategy

RuleConditionDmrOpenInNewContextStrategyProvider$Strategy

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