Skip to content

Package: CellEditorFactory

CellEditorFactory

nameinstructionbranchcomplexitylinemethod
CellEditorFactory()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getCellEditor(EStructuralFeature, EObject, Table, ViewModelContext)
M: 112 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 36 C: 0
0%
M: 1 C: 0
0%
loadClass(String, String)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
parseExtensionPoint()
M: 59 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.internal.swt.util;
15:
16: import java.lang.reflect.Constructor;
17: import java.lang.reflect.InvocationTargetException;
18: import java.util.HashSet;
19: import java.util.Set;
20:
21: import org.eclipse.core.runtime.CoreException;
22: import org.eclipse.core.runtime.IConfigurationElement;
23: import org.eclipse.core.runtime.Platform;
24: import org.eclipse.emf.ecore.EObject;
25: import org.eclipse.emf.ecore.EStructuralFeature;
26: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
27: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
28: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditorTester;
29: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
30: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
31: import org.eclipse.jface.viewers.CellEditor;
32: import org.eclipse.jface.viewers.TextCellEditor;
33: import org.eclipse.swt.widgets.Composite;
34: import org.eclipse.swt.widgets.Table;
35: import org.eclipse.swt.widgets.Text;
36: import org.osgi.framework.Bundle;
37:
38: public final class CellEditorFactory {
39:         private static final String CONTROL_EXTENSION = "org.eclipse.emf.ecp.edit.swt.cellEditor"; //$NON-NLS-1$
40:
41:         private static final String CLASS_ATTRIBUTE = "class";//$NON-NLS-1$
42:         private static final String ID = "id";//$NON-NLS-1$
43:         private static final String TESTER = "tester";//$NON-NLS-1$
44:
45:         private final Set<CellDescriptor> descriptors = new HashSet<CellEditorFactory.CellDescriptor>();
46:
47:         public static final CellEditorFactory INSTANCE = new CellEditorFactory();
48:
49:         private CellEditorFactory() {
50:                 parseExtensionPoint();
51:         }
52:
53:         private void parseExtensionPoint() {
54:                 final IConfigurationElement[] controls = Platform.getExtensionRegistry().getConfigurationElementsFor(
55:                         CONTROL_EXTENSION);
56:•                for (final IConfigurationElement e : controls) {
57:                         try {
58:                                 final String id = e.getAttribute(ID);
59:                                 final String clazz = e.getAttribute(CLASS_ATTRIBUTE);
60:                                 final Class<? extends CellEditor> resolvedClass = loadClass(e.getContributor().getName(), clazz);
61:                                 final ECPCellEditorTester tester = (ECPCellEditorTester) e.createExecutableExtension(TESTER);
62:                                 descriptors.add(new CellDescriptor(id, resolvedClass, tester));
63:                         } catch (final ClassNotFoundException e1) {
64:                                 Activator.logException(e1);
65:                         } catch (final CoreException e1) {
66:                                 Activator.logException(e1);
67:                         }
68:                 }
69:         }
70:
71:         @SuppressWarnings("unchecked")
72:         private static <T> Class<T> loadClass(String bundleName, String clazz) throws ClassNotFoundException {
73:                 final Bundle bundle = Platform.getBundle(bundleName);
74:•                if (bundle == null) {
75:                         throw new ClassNotFoundException(clazz
76:                                 + LocalizationServiceHelper.getString(CellEditorFactory.class,
77:                                         UtilMessageKeys.CellEditorFactory_CannotBeLoadedBecauseBundle)
78:                                 + bundleName
79:                                 + LocalizationServiceHelper.getString(CellEditorFactory.class,
80:                                         UtilMessageKeys.CellEditorFactory_CannotBeResolved));
81:                 }
82:                 return (Class<T>) bundle.loadClass(clazz);
83:
84:         }
85:
86:         public CellEditor getCellEditor(EStructuralFeature eStructuralFeature, EObject eObject, Table table,
87:                 ViewModelContext viewModelContext) {
88:                 int bestPriority = -1;
89:                 CellDescriptor bestCandidate = null;
90:•                for (final CellDescriptor descriptor : descriptors) {
91:                         final int priority = descriptor.getTester().isApplicable(eObject, eStructuralFeature, viewModelContext);
92:•                        if (priority > bestPriority) {
93:                                 bestCandidate = descriptor;
94:                                 bestPriority = priority;
95:                         }
96:                 }
97:                 CellEditor result = null;
98:•                if (bestCandidate != null) {
99:                         try {
100:                                 final Constructor<? extends CellEditor> constructor = bestCandidate.getCellEditorClass()
101:                                         .getConstructor(
102:                                                 Composite.class);
103:                                 result = constructor.newInstance(table);
104:                                 final ECPCellEditor ecpCellEditor = (ECPCellEditor) result;
105:                                 ecpCellEditor.instantiate(eStructuralFeature, viewModelContext);
106:                         } catch (final SecurityException e) {
107:                                 Activator.logException(e);
108:                         } catch (final NoSuchMethodException e) {
109:                                 Activator.logException(e);
110:                         } catch (final IllegalArgumentException e) {
111:                                 Activator.logException(e);
112:                         } catch (final InstantiationException e) {
113:                                 Activator.logException(e);
114:                         } catch (final IllegalAccessException e) {
115:                                 Activator.logException(e);
116:                         } catch (final InvocationTargetException e) {
117:                                 Activator.logException(e);
118:                         } catch (final ClassCastException e) {
119:                                 Activator.logException(e);
120:                         }
121:                 }
122:•                if (result == null) {
123:                         result = new TextCellEditor(table);
124:                 }
125:
126:•                if (Text.class.isInstance(result.getControl())) {
127:                         PreSetValidationListeners.create(viewModelContext).verify(
128:                                 Text.class.cast(result.getControl()),
129:                                 eStructuralFeature);
130:                 }
131:
132:                 return result;
133:         }
134:
135:         private class CellDescriptor {
136:                 private final String id;
137:                 private final Class<? extends CellEditor> cellEditorClass;
138:                 private final ECPCellEditorTester tester;
139:
140:                 CellDescriptor(String id, Class<? extends CellEditor> cellEditorClass, ECPCellEditorTester tester) {
141:                         super();
142:                         this.id = id;
143:                         this.cellEditorClass = cellEditorClass;
144:                         this.tester = tester;
145:                 }
146:
147:                 String getId() {
148:                         return id;
149:                 }
150:
151:                 Class<? extends CellEditor> getCellEditorClass() {
152:                         return cellEditorClass;
153:                 }
154:
155:                 ECPCellEditorTester getTester() {
156:                         return tester;
157:                 }
158:         }
159: }