Skip to content

Package: EMFFormsAbstractLegacyServiceFactory$1

EMFFormsAbstractLegacyServiceFactory$1

nameinstructionbranchcomplexitylinemethod
childContextAdded(VElement, EMFFormsViewContext)
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%
childContextDisposed(EMFFormsViewContext)
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%
contextDispose()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
contextInitialised()
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%
{...}
M: 0 C: 12
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.core.services.legacy;
15:
16: import org.eclipse.emf.ecp.view.spi.context.GlobalViewModelService;
17: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
18: import org.eclipse.emf.ecp.view.spi.context.ViewModelService;
19: import org.eclipse.emf.ecp.view.spi.model.VElement;
20: import org.eclipse.emfforms.spi.common.report.AbstractReport;
21: import org.eclipse.emfforms.spi.common.report.ReportService;
22: import org.eclipse.emfforms.spi.core.services.view.EMFFormsContextListener;
23: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
24: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory;
25:
26: /**
27: * An abstract implementation for the {@link EMFFormsViewServiceFactory}.
28: *
29: * @param <T> The actual type of the service
30: * @author Eugen Neufeld
31: *
32: */
33: public abstract class EMFFormsAbstractLegacyServiceFactory<T extends ViewModelService>
34:         implements EMFFormsViewServiceFactory<T> {
35:
36:         private final Class<T> type;
37:         private final double priority;
38:         private final ReportService reportService;
39:
40:         /**
41:          * Default constructor used to create an {@link EMFFormsLegacyGlobalServiceFactory}.
42:          *
43:          * @param type The type of the service to wrap
44:          * @param priority The priority of the wrapped service
45:          * @param reportService The {@link ReportService} to use for logging
46:          */
47:         public EMFFormsAbstractLegacyServiceFactory(Class<T> type, double priority, ReportService reportService) {
48:                 this.type = type;
49:                 this.priority = priority;
50:                 this.reportService = reportService;
51:         }
52:
53:         /**
54:          * {@inheritDoc}
55:          *
56:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory#getPriority()
57:          */
58:         @Override
59:         public double getPriority() {
60:                 return priority;
61:         }
62:
63:         /**
64:          * {@inheritDoc}
65:          *
66:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory#getType()
67:          */
68:         // needed as we want to return the interface if it is not to generic
69:         @SuppressWarnings("unchecked")
70:         @Override
71:         public Class<T> getType() {
72:                 Class<T> typeToCheck = type;
73:                 while (typeToCheck.getInterfaces().length == 0) {
74:                         typeToCheck = (Class<T>) typeToCheck.getSuperclass();
75:                 }
76:                 for (final Class<?> superTypeInterface : typeToCheck.getInterfaces()) {
77:                         // FIXME : what is a nice way to do this?
78:                         if (!GlobalViewModelService.class.equals(superTypeInterface)
79:                                 && !ViewModelService.class.equals(superTypeInterface)
80:                                 && !EMFFormsContextListener.class.equals(superTypeInterface)) {
81:                                 return (Class<T>) superTypeInterface;
82:                         }
83:                 }
84:                 return type;
85:         }
86:
87:         /**
88:          * {@inheritDoc}
89:          *
90:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory#createService(EMFFormsViewContext)
91:          */
92:         @SuppressWarnings("unchecked")
93:         @Override
94:         public T createService(final EMFFormsViewContext emfFormsViewContext) {
95:                 try {
96:                         final ViewModelService vms = type.newInstance();
97:                         if (ViewModelContext.class.isInstance(emfFormsViewContext)) {
98:                                 vms.instantiate(ViewModelContext.class.cast(emfFormsViewContext));
99:                                 emfFormsViewContext.registerEMFFormsContextListener(new EMFFormsContextListener() {
100:
101:                                         @Override
102:                                         public void contextInitialised() {
103:                                                 // do nothing
104:                                         }
105:
106:                                         @Override
107:                                         public void contextDispose() {
108:                                                 vms.dispose();
109:                                                 emfFormsViewContext.unregisterEMFFormsContextListener(this);
110:                                         }
111:
112:                                         @Override
113:                                         public void childContextDisposed(EMFFormsViewContext childContext) {
114:                                                 // do nothing
115:                                         }
116:
117:                                         @Override
118:                                         public void childContextAdded(VElement parentElement, EMFFormsViewContext childContext) {
119:                                                 // do nothing
120:                                         }
121:                                 });
122:                         }
123:                         return (T) vms;
124:                 } catch (final InstantiationException ex) {
125:                         reportService.report(new AbstractReport(ex));
126:                 } catch (final IllegalAccessException ex) {
127:                         reportService.report(new AbstractReport(ex));
128:                 } catch (final IllegalArgumentException ex) {
129:                         reportService.report(new AbstractReport(ex));
130:                 } catch (final SecurityException ex) {
131:                         reportService.report(new AbstractReport(ex));
132:                 }
133:                 throw new IllegalStateException("Class could not be instantiated. Please check your log!"); //$NON-NLS-1$
134:         }
135:
136: }