Skip to content

Package: CellEditorFactory

CellEditorFactory

nameinstructionbranchcomplexitylinemethod
CellEditorFactory()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createCellEditor(EAttribute, EObject, Table, ViewModelContext)
M: 5 C: 41
89%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 10
91%
M: 0 C: 1
100%
createCellEditor(EStructuralFeature, Table, ViewModelContext, CellEditorFactory.CellEditorDescriptor, CellEditor)
M: 34 C: 30
47%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 13 C: 8
38%
M: 0 C: 1
100%
loadClass(String, String)
M: 16 C: 9
36%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 3
60%
M: 0 C: 1
100%
parseExtensionPoint()
M: 9 C: 49
84%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 3 C: 10
77%
M: 0 C: 1
100%
reportException(Throwable)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.view.control.multiattribute.celleditor;
15:
16: import java.lang.reflect.Constructor;
17: import java.lang.reflect.InvocationTargetException;
18: import java.text.MessageFormat;
19: import java.util.LinkedHashSet;
20: import java.util.Set;
21:
22: import org.eclipse.core.runtime.CoreException;
23: import org.eclipse.core.runtime.IConfigurationElement;
24: import org.eclipse.core.runtime.Platform;
25: import org.eclipse.emf.ecore.EAttribute;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.EStructuralFeature;
28: import org.eclipse.emf.ecp.edit.internal.swt.util.PreSetValidationListeners;
29: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
31: import org.eclipse.emfforms.common.RankingHelper;
32: import org.eclipse.emfforms.spi.common.report.AbstractReport;
33: import org.eclipse.emfforms.spi.common.report.ReportService;
34: import org.eclipse.emfforms.spi.view.control.multiattribute.celleditor.MultiAttributeSWTRendererCellEditorTester;
35: import org.eclipse.jface.viewers.CellEditor;
36: import org.eclipse.jface.viewers.TextCellEditor;
37: import org.eclipse.swt.widgets.Composite;
38: import org.eclipse.swt.widgets.Table;
39: import org.eclipse.swt.widgets.Text;
40: import org.osgi.framework.Bundle;
41: import org.osgi.framework.BundleContext;
42: import org.osgi.framework.FrameworkUtil;
43: import org.osgi.framework.ServiceReference;
44:
45: /**
46: * Factory to created new {@link ECPCellEditor cell editors} for the
47: * {@link org.eclipse.emfforms.spi.view.control.multiattribute.MultiAttributeSWTRenderer MultiAttributeSWTRenderer}.
48: *
49: * @author Johannes Faltermeier
50: *
51: */
52: public final class CellEditorFactory {
53:
54:         /**
55:          * The singleton instance.
56:          */
57:         public static final CellEditorFactory INSTANCE = new CellEditorFactory();
58:
59:         private static final String POINT_ID = "org.eclipse.emfforms.swt.control.multiattribute.cellEditor"; //$NON-NLS-1$
60:         private static final String CLAZZ = "class";//$NON-NLS-1$
61:         private static final String TESTER = "tester";//$NON-NLS-1$
62:
63:         private final Set<CellEditorDescriptor> descriptors = new LinkedHashSet<CellEditorDescriptor>();
64:
65:         private static final RankingHelper<CellEditorDescriptor> RANKING_HELPER = //
66:                 new RankingHelper<CellEditorDescriptor>(
67:                         CellEditorDescriptor.class,
68:                         -Double.MIN_VALUE,
69:                         MultiAttributeSWTRendererCellEditorTester.NOT_APPLICABLE);
70:
71:         private CellEditorFactory() {
72:                 parseExtensionPoint();
73:         }
74:
75:         private void parseExtensionPoint() {
76:                 final IConfigurationElement[] editors = Platform.getExtensionRegistry().getConfigurationElementsFor(
77:                         POINT_ID);
78:•                for (final IConfigurationElement editor : editors) {
79:                         try {
80:                                 final String clazz = editor.getAttribute(CLAZZ);
81:                                 final Class<? extends CellEditor> resolvedClass = loadClass(editor.getContributor().getName(), clazz);
82:                                 final MultiAttributeSWTRendererCellEditorTester tester = MultiAttributeSWTRendererCellEditorTester.class
83:                                         .cast(editor.createExecutableExtension(TESTER));
84:                                 descriptors.add(new CellEditorDescriptor(resolvedClass, tester));
85:                         } catch (final ClassNotFoundException e) {
86:                                 reportException(e);
87:                         } catch (final CoreException e) {
88:                                 reportException(e);
89:                         }
90:                 }
91:         }
92:
93:         @SuppressWarnings("unchecked")
94:         private static <T> Class<T> loadClass(String bundleName, String clazz) throws ClassNotFoundException {
95:                 final Bundle bundle = Platform.getBundle(bundleName);
96:•                if (bundle == null) {
97:                         throw new ClassNotFoundException(MessageFormat
98:                                 .format("Class {0} cannot be loaded because bundle {1} cannot be resolved", clazz, bundleName)); //$NON-NLS-1$
99:                 }
100:                 return (Class<T>) bundle.loadClass(clazz);
101:         }
102:
103:         /**
104:          * Creates a new {@link CellEditor} for the given arguments. If no fitting {@link CellEditor} is found at the
105:          * extension point or if there was an exception during the creation a {@link TextCellEditor} will be returned as a
106:          * default.
107:          *
108:          * @param multiAttribute the feature which is displayed by the
109:          * {@link org.eclipse.emfforms.spi.view.control.multiattribute.MultiAttributeSWTRenderer
110:          * MultiAttributeSWTRenderer}
111:          * @param eObject the {@link EObject}
112:          * @param table the {@link Table}
113:          * @param viewModelContext the {@link ViewModelContext}
114:          * @return a {@link ECPCellEditor} or a {@link TextCellEditor} as a fallback
115:          */
116:         public CellEditor createCellEditor(final EAttribute multiAttribute, final EObject eObject, Table table,
117:                 final ViewModelContext viewModelContext) {
118:
119:                 final CellEditorDescriptor bestCandidate = RANKING_HELPER.getHighestRankingElement(
120:                         descriptors,
121:                         new RankingHelper.RankTester<CellEditorDescriptor>() {
122:
123:                                 @Override
124:                                 public double getRank(final CellEditorDescriptor descriptor) {
125:                                         return descriptor.getTester().isApplicable(eObject, multiAttribute, viewModelContext);
126:                                 }
127:
128:                         });
129:
130:                 CellEditor result = null;
131:•                if (bestCandidate != null) {
132:                         result = createCellEditor(multiAttribute, table, viewModelContext, bestCandidate, result);
133:                 }
134:•                if (result == null) {
135:                         result = new TextCellEditor(table);
136:                 }
137:
138:•                if (Text.class.isInstance(result.getControl())) {
139:                         PreSetValidationListeners.create(viewModelContext).verify((Text) result.getControl(), multiAttribute);
140:                 }
141:
142:                 return result;
143:         }
144:
145:         private CellEditor createCellEditor(EStructuralFeature eStructuralFeature, Table table,
146:                 ViewModelContext viewModelContext, CellEditorDescriptor bestCandidate, CellEditor result) {
147:                 try {
148:                         final Constructor<? extends CellEditor> constructor = bestCandidate.getCellEditorClass()
149:                                 .getConstructor(
150:                                         Composite.class);
151:                         result = constructor.newInstance(table);
152:                         final ECPCellEditor ecpCellEditor = (ECPCellEditor) result;
153:                         ecpCellEditor.instantiate(eStructuralFeature, viewModelContext);
154:                 } catch (final SecurityException e) {
155:                         reportException(e);
156:                 } catch (final NoSuchMethodException e) {
157:                         reportException(e);
158:                 } catch (final IllegalArgumentException e) {
159:                         reportException(e);
160:                 } catch (final InstantiationException e) {
161:                         reportException(e);
162:                 } catch (final IllegalAccessException e) {
163:                         reportException(e);
164:                 } catch (final InvocationTargetException e) {
165:                         reportException(e);
166:                 } catch (final ClassCastException e) {
167:                         reportException(e);
168:                 }
169:                 return result;
170:         }
171:
172:         private void reportException(Throwable throwable) {
173:                 final Bundle bundle = FrameworkUtil.getBundle(CellEditorFactory.class);
174:                 final BundleContext bundleContext = bundle.getBundleContext();
175:                 final ServiceReference<ReportService> serviceReference = bundleContext.getServiceReference(ReportService.class);
176:                 final ReportService reportService = bundleContext.getService(serviceReference);
177:                 reportService.report(new AbstractReport(throwable));
178:                 bundleContext.ungetService(serviceReference);
179:         }
180:
181:         /**
182:          * Descriptor for the elements registered at the extension point.
183:          *
184:          * @author Johannes Faltermeier
185:          *
186:          */
187:         private static final class CellEditorDescriptor {
188:
189:                 private final Class<? extends CellEditor> cellEditorClass;
190:                 private final MultiAttributeSWTRendererCellEditorTester tester;
191:
192:                 private CellEditorDescriptor(Class<? extends CellEditor> cellEditorClass,
193:                         MultiAttributeSWTRendererCellEditorTester tester) {
194:                         this.cellEditorClass = cellEditorClass;
195:                         this.tester = tester;
196:                 }
197:
198:                 private Class<? extends CellEditor> getCellEditorClass() {
199:                         return cellEditorClass;
200:                 }
201:
202:                 private MultiAttributeSWTRendererCellEditorTester getTester() {
203:                         return tester;
204:                 }
205:         }
206:
207: }