Skip to content

Package: LocalizationViewModelService$1

LocalizationViewModelService$1

nameinstructionbranchcomplexitylinemethod
notifyChange(ModelChangeNotification)
M: 0 C: 60
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 9
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.view.model.localization;
15:
16: import org.eclipse.emf.common.notify.Adapter;
17: import org.eclipse.emf.common.notify.Notification;
18: import org.eclipse.emf.common.util.TreeIterator;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
21: import org.eclipse.emf.ecp.view.spi.context.ViewModelService;
22: import org.eclipse.emf.ecp.view.spi.model.LocalizationAdapter;
23: import org.eclipse.emf.ecp.view.spi.model.ModelChangeListener;
24: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
25: import org.eclipse.emf.ecp.view.spi.model.VElement;
26: import org.eclipse.emf.ecp.view.spi.model.VHasTooltip;
27: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
28: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleChangeListener;
29: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
30: import org.eclipse.emfforms.spi.common.report.AbstractReport;
31: import org.eclipse.emfforms.spi.common.report.ReportService;
32: import org.osgi.framework.Bundle;
33: import org.osgi.framework.BundleContext;
34: import org.osgi.framework.FrameworkUtil;
35: import org.osgi.framework.ServiceReference;
36:
37: /**
38: * LocalizationViewModelService which will localize the view model.
39: *
40: * @author Eugen Neufeld
41: *
42: */
43: public class LocalizationViewModelService implements ViewModelService, EMFFormsLocaleChangeListener {
44:
45:         private EMFFormsLocaleProvider localeProvider;
46:         private ServiceReference<EMFFormsLocaleProvider> localeServiceReference;
47:         private BundleContext bundleContext;
48:         private VElement view;
49:         private ReportService reportService;
50:         private ServiceReference<ReportService> reportServiceReference;
51:
52:         /**
53:          * Default constructor.
54:          */
55:         public LocalizationViewModelService() {
56:                 super();
57:                 final Bundle bundle = FrameworkUtil.getBundle(getClass());
58:                 if (bundle == null) {
59:                         return;
60:                 }
61:                 bundleContext = bundle.getBundleContext();
62:                 if (bundleContext == null) {
63:                         return;
64:                 }
65:                 localeServiceReference = bundleContext.getServiceReference(EMFFormsLocaleProvider.class);
66:                 if (localeServiceReference == null) {
67:                         return;
68:                 }
69:                 localeProvider = bundleContext.getService(localeServiceReference);
70:
71:                 reportServiceReference = bundleContext.getServiceReference(ReportService.class);
72:                 reportService = bundleContext.getService(reportServiceReference);
73:
74:                 localeProvider.addEMFFormsLocaleChangeListener(this);
75:         }
76:
77:         /**
78:          * {@inheritDoc}
79:          *
80:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#instantiate(org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
81:          */
82:         @Override
83:         public void instantiate(ViewModelContext context) {
84:                 view = context.getViewModel();
85:                 final LocalizationAdapter adapter = getLocalizationAdapter(view);
86:                 localizeView(adapter, view);
87:
88:                 context.registerViewChangeListener(new ModelChangeListener() {
89:
90:                         @Override
91:                         public void notifyChange(ModelChangeNotification notification) {
92:•                                if (notification.getRawNotification().getFeature() == VViewPackage.eINSTANCE.getElement_Name()) {
93:                                         localize(adapter, (VElement) notification.getNotifier());
94:•                                } else if (notification.getRawNotification().getEventType() == Notification.ADD
95:•                                        || notification.getRawNotification().getEventType() == Notification.ADD_MANY) {
96:•                                        for (final EObject eObject : notification.getNewEObjects()) {
97:•                                                if (VElement.class.isInstance(eObject)) {
98:                                                         localize(adapter, VElement.class.cast(eObject));
99:                                                         checkContents(adapter, VElement.class.cast(eObject));
100:                                                 }
101:                                         }
102:                                 }
103:                         }
104:                 });
105:         }
106:
107:         private void localizeView(LocalizationAdapter adapter, final VElement view) {
108:                 localize(adapter, view);
109:                 checkContents(adapter, view);
110:         }
111:
112:         private LocalizationAdapter getLocalizationAdapter(VElement view) {
113:                 for (final Adapter adapter : view.eAdapters()) {
114:                         if (LocalizationAdapter.class.isInstance(adapter)) {
115:                                 return (LocalizationAdapter) adapter;
116:                         }
117:                 }
118:                 return null;
119:         }
120:
121:         private void checkContents(final LocalizationAdapter adapter, final VElement vElementRoot) {
122:                 final TreeIterator<EObject> eAllContents = vElementRoot.eAllContents();
123:                 while (eAllContents.hasNext()) {
124:                         final EObject eObject = eAllContents.next();
125:                         if (VElement.class.isInstance(eObject)) {
126:                                 final VElement vElement = VElement.class.cast(eObject);
127:                                 localize(adapter, vElement);
128:                         }
129:                 }
130:         }
131:
132:         /**
133:          * The actual method localizing a {@link VElement}.
134:          *
135:          * @param localizationAdapter The LocalizationAdapter to use for localization
136:          * @param vElement The {@link VElement} to localize
137:          */
138:         protected void localize(LocalizationAdapter localizationAdapter, final VElement vElement) {
139:                 if (vElement.getName() == null) {
140:                         vElement.setLabel(""); //$NON-NLS-1$
141:                 } else if (vElement.getName().startsWith("%")) { //$NON-NLS-1$
142:                         if (localizationAdapter != null) {
143:                                 vElement.setLabel(localizationAdapter.localize(vElement.getName().substring(1)));
144:                         } else {
145:                                 reportAboutMissingLocalizationAdapter();
146:                                 vElement.setLabel(vElement.getName());
147:                         }
148:                 } else {
149:                         vElement.setLabel(vElement.getName());
150:                 }
151:
152:                 if (vElement instanceof VHasTooltip) {
153:                         final VHasTooltip tooltip = VHasTooltip.class.cast(vElement);
154:                         if (tooltip.getTooltip() != null && tooltip.getTooltip().startsWith("%")) { //$NON-NLS-1$
155:                                 if (localizationAdapter != null) {
156:                                         tooltip.setTooltip(localizationAdapter.localize(tooltip.getTooltip().substring(1)));
157:                                 } else {
158:                                         reportAboutMissingLocalizationAdapter();
159:                                 }
160:                         }
161:                 }
162:         }
163:
164:         private void reportAboutMissingLocalizationAdapter() {
165:                 reportService.report(new AbstractReport(
166:                         "No LocalizationAdapter found for the current view:" + view.toString())); //$NON-NLS-1$
167:         }
168:
169:         /**
170:          * {@inheritDoc}
171:          *
172:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#dispose()
173:          */
174:         @Override
175:         public void dispose() {
176:                 if (localeProvider != null) {
177:                         localeProvider.removeEMFFormsLocaleChangeListener(this);
178:                 }
179:                 if (bundleContext != null && localeServiceReference != null) {
180:                         bundleContext.ungetService(localeServiceReference);
181:                 }
182:                 if (bundleContext != null && reportServiceReference != null) {
183:                         bundleContext.ungetService(reportServiceReference);
184:                 }
185:         }
186:
187:         /**
188:          * {@inheritDoc}
189:          *
190:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#getPriority()
191:          */
192:         @Override
193:         public int getPriority() {
194:                 return -100;
195:         }
196:
197:         /**
198:          * {@inheritDoc}
199:          *
200:          * @see org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleChangeListener#notifyLocaleChange()
201:          */
202:         @Override
203:         public void notifyLocaleChange() {
204:                 final LocalizationAdapter adapter = getLocalizationAdapter(view);
205:                 localizeView(adapter, view);
206:         }
207:
208: }