Skip to content

Package: LinkFeatureControlRenderer

LinkFeatureControlRenderer

nameinstructionbranchcomplexitylinemethod
LinkFeatureControlRenderer(VControl, ViewModelContext, ReportService)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getEObject()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getRootEClass(Notifier)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
linkValue(Shell)
M: 6 C: 37
86%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 9
82%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * Alexandra Buzila - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.controls;
15:
16: import java.util.List;
17:
18: import javax.inject.Inject;
19:
20: import org.eclipse.core.databinding.observable.IObserving;
21: import org.eclipse.core.databinding.observable.value.IObservableValue;
22: import org.eclipse.core.runtime.IStatus;
23: import org.eclipse.core.runtime.Status;
24: import org.eclipse.emf.common.notify.AdapterFactory;
25: import org.eclipse.emf.common.notify.Notifier;
26: import org.eclipse.emf.ecore.EClass;
27: import org.eclipse.emf.ecore.EObject;
28: import org.eclipse.emf.ecore.EReference;
29: import org.eclipse.emf.ecore.EStructuralFeature;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
31: import org.eclipse.emf.ecp.view.spi.editor.controls.AbstractFilteredReferenceCommand;
32: import org.eclipse.emf.ecp.view.spi.editor.controls.Helper;
33: import org.eclipse.emf.ecp.view.spi.model.VControl;
34: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
35: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
36: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
37: import org.eclipse.emfforms.spi.common.report.AbstractReport;
38: import org.eclipse.emfforms.spi.common.report.ReportService;
39: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
40: import org.eclipse.jface.viewers.TreePath;
41: import org.eclipse.swt.widgets.Shell;
42:
43: /**
44: * SWT Renderer for linking a {@link EStructuralFeature domain model feature} to a
45: * {@link VFeaturePathDomainModelReference}.
46: */
47: public class LinkFeatureControlRenderer extends EditableEReferenceLabelControlSWTRenderer {
48:
49:         /**
50:          * Default constructor.
51:          *
52:          * @param vElement the view model element to be rendered
53:          * @param viewContext the view context
54:          * @param reportService the {@link ReportService}
55:          */
56:         @Inject
57:         public LinkFeatureControlRenderer(VControl vElement, ViewModelContext viewContext, ReportService reportService) {
58:                 super(vElement, viewContext, reportService);
59:         }
60:
61:         @Override
62:         protected void linkValue(Shell shell) {
63:                 final EObject eObject;
64:                 try {
65:                         eObject = getEObject();
66:                 } catch (final DatabindingFailedException ex) {
67:                         showLinkValueFailedMessageDialog(shell, ex);
68:                         return;
69:                 }
70:                 final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
71:                         new ReflectiveItemProviderAdapterFactory(),
72:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
73:
74:                 final FilteredReferenceCommand referenceCommand = new FilteredReferenceCommand(eObject, adapterFactory, shell);
75:•                if (referenceCommand.canExecute()) {
76:                         referenceCommand.execute();
77:                 }
78:         }
79:
80:         /**
81:          * @return the domain model EObject
82:          * @throws DatabindingFailedException if the
83:          */
84:         private EObject getEObject() throws DatabindingFailedException {
85:                 final IObservableValue observableValue = getEMFFormsDatabinding()
86:                         .getObservableValue(getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
87:                 return (EObject) ((IObserving) observableValue).getObserved();
88:         }
89:
90:         /**
91:          * The command for linking the reference.
92:          */
93:         private class FilteredReferenceCommand extends AbstractFilteredReferenceCommand<EStructuralFeature> {
94:
95:                 FilteredReferenceCommand(final Notifier notifier, ComposedAdapterFactory composedAdapterFactory,
96:                         Shell shell) {
97:
98:                         super(notifier, composedAdapterFactory, shell, getRootEClass(notifier),
99:                                 new ECPSelectionStatusValidator() {
100:
101:                                         @Override
102:                                         public IStatus validate(Object[] selection) {
103:
104:                                                 if (selection.length != 0 && EStructuralFeature.class.isInstance(selection[0])) {
105:                                                         final TreePath treePath = getTreePath();
106:                                                         if (!Helper
107:                                                                 .hasFeaturePropertyDescriptor(EStructuralFeature.class.cast(selection[0])
108:                                                                         .getEContainingClass(), treePath)) {
109:                                                                 // FIXME Hack, for allowing the selection of EStructuralFeatures w/o property
110:                                                                 // descriptors. Should return error.
111:                                                                 return new Status(IStatus.WARNING,
112:                                                                         org.eclipse.emf.ecp.view.internal.editor.controls.Activator.PLUGIN_ID,
113:                                                                         "The selected " + EStructuralFeature.class.getSimpleName() //$NON-NLS-1$
114:                                                                                 + " has no PropertyDescriptor."); //$NON-NLS-1$
115:                                                         }
116:                                                         return Status.OK_STATUS;
117:                                                 }
118:                                                 return new Status(IStatus.ERROR,
119:                                                         org.eclipse.emf.ecp.view.internal.editor.controls.Activator.PLUGIN_ID,
120:                                                         "This is not an " + EStructuralFeature.class.getSimpleName() + "."); //$NON-NLS-1$ //$NON-NLS-2$
121:                                         }
122:                                 }, false);
123:                 }
124:
125:                 @Override
126:                 protected void setSelectedValues(EStructuralFeature selectedFeature, List<EReference> bottomUpPath) {
127:                         try {
128:                                 final VFeaturePathDomainModelReference modelReference = (VFeaturePathDomainModelReference) getEObject();
129:                                 modelReference.setDomainModelEFeature(selectedFeature);
130:                                 modelReference.getDomainModelEReferencePath().clear();
131:                                 modelReference.getDomainModelEReferencePath().addAll(bottomUpPath);
132:                         } catch (final DatabindingFailedException ex) {
133:                                 getReportService().report(new AbstractReport(ex));
134:                         }
135:                 }
136:
137:         }
138:
139:         /**
140:          * Allows to retrieve the root eclass necessary to select the {@link VFeaturePathDomainModelReference}.
141:          *
142:          * @param notifier The {@link Notifier} triggering the selection
143:          * @return The {@link EClass} that should be used as root
144:          */
145:         protected EClass getRootEClass(Notifier notifier) {
146:                 return Helper.getRootEClass((EObject) notifier);
147:         }
148:
149: }