Skip to content

Package: ECPAbstractControl$ViewModelChangeListener

ECPAbstractControl$ViewModelChangeListener

nameinstructionbranchcomplexitylinemethod
ECPAbstractControl.ViewModelChangeListener(ECPAbstractControl, VControl)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notifyAdd(Notifier)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
notifyChange(ModelChangeNotification)
M: 6 C: 27
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 8
89%
M: 0 C: 1
100%
notifyRemove(Notifier)
M: 0 C: 1
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-2013 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.emf.ecp.edit.spi;
15:
16: import java.util.Locale;
17:
18: import org.eclipse.core.databinding.DataBindingContext;
19: import org.eclipse.core.databinding.observable.IObserving;
20: import org.eclipse.core.databinding.observable.value.IObservableValue;
21: import org.eclipse.core.databinding.property.value.IValueProperty;
22: import org.eclipse.emf.common.notify.AdapterFactory;
23: import org.eclipse.emf.common.notify.Notifier;
24: import org.eclipse.emf.common.util.Diagnostic;
25: import org.eclipse.emf.databinding.EMFDataBindingContext;
26: import org.eclipse.emf.ecore.EStructuralFeature;
27: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
28: import org.eclipse.emf.ecore.InternalEObject;
29: import org.eclipse.emf.ecp.view.model.common.edit.provider.CustomReflectiveItemProviderAdapterFactory;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
31: import org.eclipse.emf.ecp.view.spi.model.ModelChangeAddRemoveListener;
32: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
33: import org.eclipse.emf.ecp.view.spi.model.VControl;
34: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
35: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
36: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
37: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
38: import org.eclipse.emf.edit.domain.EditingDomain;
39: import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
40: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
41: import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
42: import org.eclipse.emfforms.spi.common.report.ReportService;
43: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
44: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
45: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
46:
47: /**
48: * The {@link ECPAbstractControl} is the abstract class describing a control.
49: * This class provides the necessary common access methods.
50: *
51: * @author Eugen Neufeld
52: *
53: */
54: @Deprecated
55: public abstract class ECPAbstractControl {
56:
57:         /**
58:          * @author Jonas
59:          *
60:          */
61:         private final class ViewModelChangeListener implements ModelChangeAddRemoveListener {
62:                 private final VControl control;
63:
64:                 /**
65:                  * @param control
66:                  */
67:                 private ViewModelChangeListener(VControl control) {
68:                         this.control = control;
69:                 }
70:
71:                 @Override
72:                 public void notifyRemove(Notifier notifier) {
73:                         // TODO Auto-generated method stub
74:
75:                 }
76:
77:                 @Override
78:                 public void notifyChange(ModelChangeNotification notification) {
79:•                        if (notification.getNotifier() != ECPAbstractControl.this.control) {
80:                                 return;
81:                         }
82:•                        if (notification.getStructuralFeature() == VViewPackage.eINSTANCE
83:                                 .getElement_Diagnostic()) {
84:                                 applyValidation(control.getDiagnostic());
85:
86:                                 // TODO remove asap
87:                                 backwardCompatibleHandleValidation();
88:                         }
89:•                        if (notification.getStructuralFeature() == VViewPackage.eINSTANCE.getElement_Enabled()) {
90:                                 enabledmentChanged(control.isEnabled());
91:                         }
92:                 }
93:
94:                 @Override
95:                 public void notifyAdd(Notifier notifier) {
96:                         // TODO Auto-generated method stub
97:
98:                 }
99:         }
100:
101:         private boolean embedded;
102:         private EMFDataBindingContext dataBindingContext;
103:         private ComposedAdapterFactory composedAdapterFactory;
104:         private AdapterFactoryItemDelegator adapterFactoryItemDelegator;
105:         private ViewModelContext viewModelContext;
106:         private VControl control;
107:         private Setting firstSetting;
108:         private EStructuralFeature firstFeature;
109:         private ModelChangeAddRemoveListener viewChangeListener;
110:
111:         /**
112:          * This method is called by the framework to instantiate the {@link ECPAbstractControl}.
113:          *
114:          * @param viewModelContext the {@link ViewModelContext} to use by this {@link ECPAbstractControl}.
115:          * @param control the {@link VControl} of this control
116:          * @since 1.2
117:          */
118:         public final void init(ViewModelContext viewModelContext, final VControl control) {
119:                 this.viewModelContext = viewModelContext;
120:                 this.control = control;
121:                 composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
122:                         new CustomReflectiveItemProviderAdapterFactory(),
123:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
124:                 adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(composedAdapterFactory);
125:
126:                 viewChangeListener = new ViewModelChangeListener(control);
127:
128:                 viewModelContext.registerViewChangeListener(viewChangeListener);
129:
130:                 postInit();
131:         }
132:
133:         /**
134:          * Overwrite this method to implement control specific operations which must be executed after the init but before
135:          * the rendering.
136:          *
137:          * @since 1.2
138:          */
139:         protected void postInit() {
140:                 // do nothing
141:         }
142:
143:         /**
144:          * Notifies a control, that its enablement state has changed.
145:          *
146:          * @param enabled the new enablement value
147:          * @since 1.2
148:          */
149:         protected void enabledmentChanged(boolean enabled) {
150:                 setEditable(enabled);
151:         }
152:
153:         /**
154:          * Override this method in order to handle validation.
155:          *
156:          * @param diagnostic the current {@link VDiagnostic}
157:          * @since 1.2
158:          */
159:         protected void applyValidation(VDiagnostic diagnostic) {
160:                 // default case do nothing
161:         }
162:
163:         /**
164:          * Return the {@link IItemPropertyDescriptor} describing this {@link Setting}.
165:          *
166:          * @param setting the {@link Setting} to use for identifying the {@link IItemPropertyDescriptor}.
167:          * @return the {@link IItemPropertyDescriptor}
168:          * @since 1.2
169:          */
170:         public IItemPropertyDescriptor getItemPropertyDescriptor(Setting setting) {
171:                 return adapterFactoryItemDelegator.getPropertyDescriptor(setting.getEObject(),
172:                         setting.getEStructuralFeature());
173:         }
174:
175:         /**
176:          * Return the {@link VControl}.
177:          *
178:          * @return the {@link VControl} of this control
179:          * @since 1.2
180:          */
181:         protected final VControl getControl() {
182:                 return control;
183:         }
184:
185:         /**
186:          * Return the {@link ViewModelContext}.
187:          *
188:          * @return the {@link ViewModelContext} of this control
189:          * @since 1.2
190:          */
191:         protected final ViewModelContext getViewModelContext() {
192:                 return viewModelContext;
193:         }
194:
195:         /**
196:          * Returns the first setting for this control.
197:          *
198:          * @return the first Setting or throws an {@link IllegalArgumentException} if no settings can be found
199:          * @since 1.2
200:          */
201:         public final Setting getFirstSetting() {
202:                 if (firstSetting == null) {
203:                         @SuppressWarnings("rawtypes")
204:                         IObservableValue observableValue;
205:                         try {
206:                                 observableValue = getViewModelContext().getService(EMFFormsDatabinding.class)
207:                                         .getObservableValue(control.getDomainModelReference(), getViewModelContext().getDomainModel());
208:                         } catch (final DatabindingFailedException ex) {
209:                                 getViewModelContext().getService(ReportService.class).report(new DatabindingFailedReport(ex));
210:                                 throw new IllegalStateException("The databinding failed due to an incorrect VDomainModelReference: " //$NON-NLS-1$
211:                                         + ex.getMessage());
212:                         }
213:                         final InternalEObject internalEObject = (InternalEObject) ((IObserving) observableValue).getObserved();
214:                         final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
215:                         observableValue.dispose();
216:
217:                         firstSetting = internalEObject.eSetting(structuralFeature);
218:                         return firstSetting;
219:                 }
220:                 return firstSetting;
221:         }
222:
223:         /**
224:          * Return the {@link EStructuralFeature} of this control.
225:          *
226:          * @return the {@link EStructuralFeature}
227:          * @since 1.2
228:          */
229:         public final EStructuralFeature getFirstStructuralFeature() {
230:                 if (firstFeature == null) {
231:                         @SuppressWarnings("rawtypes")
232:                         IValueProperty valueProperty;
233:                         try {
234:                                 valueProperty = getViewModelContext().getService(EMFFormsDatabinding.class)
235:                                         .getValueProperty(control.getDomainModelReference(), viewModelContext.getDomainModel());
236:                         } catch (final DatabindingFailedException ex) {
237:                                 throw new IllegalArgumentException(
238:                                         "The passed VDomainModelReference resolves to no EStructuralFeature."); //$NON-NLS-1$
239:                         }
240:                         firstFeature = (EStructuralFeature) valueProperty.getValueType();
241:                 }
242:                 return firstFeature;
243:         }
244:
245:         /**
246:          * Returns a {@link DataBindingContext} for this control.
247:          *
248:          * @return the {@link DataBindingContext}
249:          * @since 1.1
250:          */
251:         public final DataBindingContext getDataBindingContext() {
252:                 if (dataBindingContext == null) {
253:                         dataBindingContext = new EMFDataBindingContext();
254:                 }
255:                 return dataBindingContext;
256:         }
257:
258:         /**
259:          * Disposes the control.
260:          * A control which needs specific dispose handling must still call super.dispose.
261:          *
262:          * @since 1.1
263:          */
264:         public void dispose() {
265:                 if (composedAdapterFactory != null) {
266:                         composedAdapterFactory.dispose();
267:                 }
268:                 composedAdapterFactory = null;
269:                 adapterFactoryItemDelegator = null;
270:                 if (dataBindingContext != null) {
271:                         dataBindingContext.dispose();
272:                 }
273:                 dataBindingContext = null;
274:                 if (viewModelContext != null) {
275:                         viewModelContext.unregisterViewChangeListener(viewChangeListener);
276:                 }
277:                 viewModelContext = null;
278:
279:                 viewChangeListener = null;
280:
281:                 control = null;
282:                 firstFeature = null;
283:                 firstSetting = null;
284:         }
285:
286:         /**
287:          * Whether a control is embedded. An embedded control can be rendered in an other fashion then an not embedded
288:          * version.
289:          *
290:          * @return true if the control is embedded in another control
291:          */
292:         protected final boolean isEmbedded() {
293:                 return embedded;
294:         }
295:
296:         /**
297:          * Sets whether this control is used as an embedded control.
298:          *
299:          * @param embedded whether the control is used as an embedded control
300:          */
301:         public final void setEmbedded(boolean embedded) {
302:                 this.embedded = embedded;
303:         }
304:
305:         /**
306:          * Returns the {@link EditingDomain} for the provided {@link Setting}.
307:          *
308:          * @param setting the provided {@link Setting}
309:          * @return the {@link EditingDomain} of this {@link Setting}
310:          * @since 1.2
311:          */
312:         protected final EditingDomain getEditingDomain(Setting setting) {
313:                 return AdapterFactoryEditingDomain.getEditingDomainFor(setting.getEObject());
314:         }
315:
316:         /**
317:          * This method allows to get a service from the view model context.
318:          *
319:          * @param serviceClass the type of the service to get
320:          * @return the service instance
321:          * @param <T> the type of the service
322:          * @since 1.2
323:          */
324:         protected final <T> T getService(Class<T> serviceClass) {
325:                 return viewModelContext.getService(serviceClass);
326:         }
327:
328:         // TODO need view model service
329:         /**
330:          * Returns the current Locale.
331:          *
332:          * @return the current {@link Locale}
333:          * @since 1.2
334:          */
335:         protected final Locale getLocale() {
336:                 final ViewLocaleService service = viewModelContext.getService(ViewLocaleService.class);
337:                 if (service != null) {
338:                         return service.getLocale();
339:                 }
340:                 return Locale.getDefault();
341:         }
342:
343:         /**
344:          * Returns the {@link VDomainModelReference} set for this control.
345:          *
346:          * @return the domainModelReference the {@link VDomainModelReference} of this control
347:          * @since 1.2
348:          */
349:         protected final VDomainModelReference getDomainModelReference() {
350:                 return control.getDomainModelReference();
351:         }
352:
353:         /**
354:          * Returns the {@link EditingDomain} for the set {@link VDomainModelReference}.
355:          *
356:          * @return the {@link EditingDomain} for this control
357:          * @since 1.2
358:          * @deprecated
359:          */
360:         @Deprecated
361:         protected final EditingDomain getEditingDomain() {
362:                 return getEditingDomain(getFirstSetting());
363:         }
364:
365:         /**
366:          * Helper method to keep the old validation. Does nothing.
367:          *
368:          * @since 1.2
369:          */
370:         protected void backwardCompatibleHandleValidation() {
371:         }
372:
373:         /**
374:          * Handle live validation.
375:          *
376:          * @param diagnostic of type Diagnostic
377:          * @deprecated
378:          * @since 1.2
379:          **/
380:         @Deprecated
381:         public void handleValidation(Diagnostic diagnostic) {
382:                 // do nothing
383:         }
384:
385:         /**
386:          * Reset the validation status 'ok'.
387:          *
388:          * @deprecated
389:          * @since 1.2
390:          */
391:         @Deprecated
392:         public void resetValidation() {
393:                 // do nothing
394:         }
395:
396:         /**
397:          * Whether a label should be shown for this control.
398:          *
399:          * @return true if a label should be created, false otherwise
400:          * @deprecated use the labelAlignment of the control model element
401:          * @since 1.2
402:          */
403:         @Deprecated
404:         public boolean showLabel() {
405:                 return true;
406:         }
407:
408:         /**
409:          * Sets the state of the widget to be either editable or not.
410:          *
411:          * @param isEditable whether to set the widget editable
412:          * @since 1.2
413:          * @deprecated
414:          */
415:         @Deprecated
416:         public void setEditable(boolean isEditable) {
417:                 // do nothing
418:         }
419:
420: }