Skip to content

Package: CellEditorFactory$CellDescriptor

CellEditorFactory$CellDescriptor

nameinstructionbranchcomplexitylinemethod
CellEditorFactory.CellDescriptor(CellEditorFactory, String, Class, ECPCellEditorTester)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getCellEditorClass()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getId()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTester()
M: 0 C: 3
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.view.spi.table.swt;
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.internal.swt.util.PreSetValidationListeners;
28: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
29: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditorTester;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
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.Text;
35: import org.osgi.framework.Bundle;
36:
37: /**
38: * Factory to created new {@link ECPCellEditor cell editors}.
39: *
40: * @since 1.10
41: */
42: public final class CellEditorFactory {
43:         private static final String CONTROL_EXTENSION = "org.eclipse.emf.ecp.edit.swt.cellEditor"; //$NON-NLS-1$
44:
45:         private static final String CLASS_ATTRIBUTE = "class";//$NON-NLS-1$
46:         private static final String ID = "id";//$NON-NLS-1$
47:         private static final String TESTER = "tester";//$NON-NLS-1$
48:
49:         private final Set<CellDescriptor> descriptors = new HashSet<CellEditorFactory.CellDescriptor>();
50:         /** CellEditorFactory instance. */
51:         public static final CellEditorFactory INSTANCE = new CellEditorFactory();
52:
53:         private CellEditorFactory() {
54:                 parseExtensionPoint();
55:         }
56:
57:         private void parseExtensionPoint() {
58:                 final IConfigurationElement[] controls = Platform.getExtensionRegistry().getConfigurationElementsFor(
59:                         CONTROL_EXTENSION);
60:                 for (final IConfigurationElement e : controls) {
61:                         try {
62:                                 final String id = e.getAttribute(ID);
63:                                 final String clazz = e.getAttribute(CLASS_ATTRIBUTE);
64:                                 final Class<? extends CellEditor> resolvedClass = loadClass(e.getContributor().getName(), clazz);
65:                                 final ECPCellEditorTester tester = (ECPCellEditorTester) e.createExecutableExtension(TESTER);
66:                                 descriptors.add(new CellDescriptor(id, resolvedClass, tester));
67:                         } catch (final ClassNotFoundException e1) {
68:                                 Activator.logException(e1);
69:                         } catch (final CoreException e1) {
70:                                 Activator.logException(e1);
71:                         }
72:                 }
73:         }
74:
75:         @SuppressWarnings("unchecked")
76:         private static <T> Class<T> loadClass(String bundleName, String clazz) throws ClassNotFoundException {
77:                 final Bundle bundle = Platform.getBundle(bundleName);
78:                 if (bundle == null) {
79:                         throw new ClassNotFoundException(
80:                         // TODO Grid
81:                         // clazz
82:                         // + LocalizationServiceHelper.getString(CellEditorFactory.class,
83:                         // UtilMessageKeys.CellEditorFactory_CannotBeLoadedBecauseBundle)
84:                         // + bundleName
85:                         // + LocalizationServiceHelper.getString(CellEditorFactory.class,
86:                         // UtilMessageKeys.CellEditorFactory_CannotBeResolved)
87:                         );
88:                 }
89:                 return (Class<T>) bundle.loadClass(clazz);
90:
91:         }
92:
93:         /**
94:          * Returns a new instance of the {@link CellEditor} for the given object.
95:          *
96:          * @param eStructuralFeature the {@link EStructuralFeature} displayed in the cell editor
97:          * @param eObject the {@link EObject}
98:          * @param table the parent composite
99:          * @param viewModelContext the {@link ViewModelContext} used for the current view
100:          * @return the cell editor
101:          */
102:         @SuppressWarnings("restriction")
103:         public CellEditor getCellEditor(final EStructuralFeature eStructuralFeature, final EObject eObject, Composite table,
104:                 ViewModelContext viewModelContext) {
105:                 int bestPriority = -1;
106:                 CellDescriptor bestCandidate = null;
107:                 for (final CellDescriptor descriptor : descriptors) {
108:                         final int priority = descriptor.getTester().isApplicable(eObject, eStructuralFeature, viewModelContext);
109:                         if (priority > bestPriority) {
110:                                 bestCandidate = descriptor;
111:                                 bestPriority = priority;
112:                         }
113:                 }
114:                 CellEditor result = null;
115:                 if (bestCandidate != null) {
116:                         try {
117:                                 final Constructor<? extends CellEditor> constructor = bestCandidate.getCellEditorClass()
118:                                         .getConstructor(
119:                                                 Composite.class);
120:                                 result = constructor.newInstance(table);
121:                                 final ECPCellEditor ecpCellEditor = (ECPCellEditor) result;
122:                                 ecpCellEditor.instantiate(eStructuralFeature, viewModelContext);
123:                         } catch (final SecurityException e) {
124:                                 Activator.logException(e);
125:                         } catch (final NoSuchMethodException e) {
126:                                 Activator.logException(e);
127:                         } catch (final IllegalArgumentException e) {
128:                                 Activator.logException(e);
129:                         } catch (final InstantiationException e) {
130:                                 Activator.logException(e);
131:                         } catch (final IllegalAccessException e) {
132:                                 Activator.logException(e);
133:                         } catch (final InvocationTargetException e) {
134:                                 Activator.logException(e);
135:                         } catch (final ClassCastException e) {
136:                                 Activator.logException(e);
137:                         }
138:                 }
139:
140:                 if (result == null) {
141:                         result = new TextCellEditor(table);
142:                 }
143:
144:                 if (Text.class.isInstance(result.getControl())) {
145:                         final Text text = (Text) result.getControl();
146:                         PreSetValidationListeners.create(viewModelContext).verify(text, eStructuralFeature);
147:                 }
148:
149:                 return result;
150:         }
151:
152:         /**
153:          * Descriptor encapsulating the contributions to the <code>org.eclipse.emf.ecp.edit.swt.cellEditor</code> extension
154:          * point.
155:          */
156:         private class CellDescriptor {
157:                 private final String id;
158:                 private final Class<? extends CellEditor> cellEditorClass;
159:                 private final ECPCellEditorTester tester;
160:
161:                 CellDescriptor(String id, Class<? extends CellEditor> cellEditorClass, ECPCellEditorTester tester) {
162:                         super();
163:                         this.id = id;
164:                         this.cellEditorClass = cellEditorClass;
165:                         this.tester = tester;
166:                 }
167:
168:                 String getId() {
169:                         return id;
170:                 }
171:
172:                 Class<? extends CellEditor> getCellEditorClass() {
173:                         return cellEditorClass;
174:                 }
175:
176:                 ECPCellEditorTester getTester() {
177:                         return tester;
178:                 }
179:         }
180: }