Skip to content

Package: AbstractReferenceHelper

AbstractReferenceHelper

nameinstructionbranchcomplexitylinemethod
AbstractReferenceHelper()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getEStructuralFeature(VDomainModelReference)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getLabel(VDomainModelReference)
M: 175 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 44 C: 0
0%
M: 1 C: 0
0%
getRootEClass(EObject)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
handleDatabindingFailedException(DatabindingFailedException)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Stefan Dirix - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.emf2web.util;
15:
16: import java.io.File;
17: import java.io.FileInputStream;
18: import java.io.FileNotFoundException;
19: import java.io.IOException;
20: import java.util.Properties;
21: import java.util.Set;
22:
23: import org.eclipse.core.databinding.observable.value.IObservableValue;
24: import org.eclipse.core.databinding.property.value.IValueProperty;
25: import org.eclipse.core.resources.IResource;
26: import org.eclipse.core.resources.ResourcesPlugin;
27: import org.eclipse.emf.ecore.EClass;
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EStructuralFeature;
30: import org.eclipse.emf.ecp.emf2web.Activator;
31: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
32: import org.eclipse.emf.ecp.view.spi.model.VView;
33: import org.eclipse.emfforms.spi.common.report.AbstractReport;
34: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
35: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
36: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
37: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
38: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
39: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
40: import org.osgi.framework.BundleContext;
41: import org.osgi.framework.ServiceReference;
42:
43: /**
44: * An abstract implementation of {@link ReferenceHelper} using {@link EMFFormsDatabinding}.
45: *
46: * @author Stefan Dirix
47: *
48: */
49: public abstract class AbstractReferenceHelper implements ReferenceHelper {
50:
51:         private final EMFFormsDatabindingEMF dataBinding;
52:
53:         /**
54:          * Constructor.
55:          */
56:         public AbstractReferenceHelper() {
57:                 dataBinding = Activator.getDefault().getEMFFormsDatabinding();
58:         }
59:
60:         /**
61:          * Returns the {@link EStructuralFeature} for the given {@link VDomainModelReference}.
62:          *
63:          * @param reference
64:          * The {@link VDomainModelReference} for which the {@link EStructuralFeature} is to be determined.
65:          * @return
66:          *                 The determined {@link EStructuralFeature}.
67:          */
68:         protected EStructuralFeature getEStructuralFeature(VDomainModelReference reference) {
69:                 try {
70:                         final IValueProperty<?, ?> valueProperty = dataBinding.getValueProperty(reference,
71:                                 getRootEClass(reference));
72:
73:•                        if (valueProperty != null) {
74:                                 return (EStructuralFeature) valueProperty.getValueType();
75:                         }
76:                 } catch (final DatabindingFailedException ex) {
77:                         handleDatabindingFailedException(ex);
78:                 }
79:                 return null;
80:         }
81:
82:         /**
83:          * Get the root {@link EClass} by walking up the containment hierarchy of the given EObject (typically a DMR or
84:          * control).
85:          *
86:          * @param eObject The {@link EObject} whose containment hierarchy is searched for a {@link VView}
87:          * @return The root EClass or <code>null</code> if the given EObject is not contained in a {@link VView}
88:          */
89:         private static EClass getRootEClass(EObject eObject) {
90:                 EObject parent = eObject.eContainer();
91:•                while (parent != null) {
92:•                        if (parent instanceof VView) {
93:                                 return ((VView) parent).getRootEClass();
94:                         }
95:                         parent = parent.eContainer();
96:                 }
97:                 return null;
98:         }
99:
100:         /**
101:          * Handle {@link DatabindingFailedException}s by reporting them to the ECP ReportService.
102:          *
103:          * @param exception
104:          * The handled {@link DatabindingFailedException}.
105:          */
106:         protected void handleDatabindingFailedException(DatabindingFailedException exception) {
107:                 Activator.getDefault().getReportService().report(new DatabindingFailedReport(exception));
108:         }
109:
110:         private static final String DISPLAY_NAME = "_UI_%1$s_%2$s_feature"; //$NON-NLS-1$
111:
112:         @Override
113:         public String getLabel(VDomainModelReference reference) {
114:                 final Set<String> paths = getEcorePaths();
115:•                if (paths == null || paths.isEmpty()) {
116:                         try {
117:                                 final BundleContext bundleContext = Activator.getDefault().getBundleContext();
118:                                 final ServiceReference<EMFFormsLabelProvider> serviceReference = bundleContext
119:                                         .getServiceReference(EMFFormsLabelProvider.class);
120:                                 final EMFFormsLabelProvider labelProvider = bundleContext.getService(serviceReference);
121:                                 final IObservableValue<?> observableValue = labelProvider.getDisplayName(reference,
122:                                         getRootEClass(reference));
123:                                 final String result = (String) observableValue.getValue();
124:                                 observableValue.dispose();
125:                                 bundleContext.ungetService(serviceReference);
126:                                 return result;
127:                         } catch (final NoLabelFoundException ex) {
128:                                 Activator.getDefault().getReportService().report(new AbstractReport(ex));
129:                                 return ""; //$NON-NLS-1$
130:                         }
131:                 }
132:•                for (final String path : paths) {
133:                         // try to find edit bundle
134:                         final String firstPath = path.split("/")[1]; //$NON-NLS-1$
135:                         final String editPath = firstPath + ".edit/plugin.properties"; //$NON-NLS-1$
136:                         final IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(editPath);
137:•                        if (member != null && member.exists()) {
138:                                 final File file = member.getLocation().toFile();
139:                                 final Properties p = new Properties();
140:                                 FileInputStream fis = null;
141:                                 try {
142:                                         fis = new FileInputStream(file);
143:                                         p.load(fis);
144:                                 } catch (final FileNotFoundException ex) {
145:                                         Activator.getDefault().getReportService().report(new AbstractReport(ex));
146:                                 } catch (final IOException ex) {
147:                                         Activator.getDefault().getReportService().report(new AbstractReport(ex));
148:                                 } finally {
149:•                                        if (fis != null) {
150:                                                 try {
151:                                                         fis.close();
152:                                                 } catch (final IOException ex) {
153:                                                         Activator.getDefault().getReportService().report(new AbstractReport(ex));
154:                                                 }
155:                                         }
156:                                 }
157:
158:                                 final EStructuralFeature feature = getEStructuralFeature(reference);
159:•                                if (feature == null) {
160:                                         continue;
161:                                 }
162:                                 final EClass eClass = feature.getEContainingClass();
163:                                 final String key = String.format(DISPLAY_NAME, eClass.getName(), feature.getName());
164:                                 final String result = p.getProperty(key);
165:•                                if (result == null) {
166:                                         return feature.getName();
167:                                 }
168:                                 return result;
169:                         }
170:                 }
171:                 final EStructuralFeature feature = getEStructuralFeature(reference);
172:                 return feature.getName();
173:         }
174:
175:         /**
176:          * Returns the ecore paths of the current view model.
177:          *
178:          * @return The paths to the ecores of the current view
179:          */
180:         protected abstract Set<String> getEcorePaths();
181: }