Skip to content

Package: ECPTooltipModifierHelper

ECPTooltipModifierHelper

nameinstructionbranchcomplexitylinemethod
getStringModifiers()
M: 0 C: 8
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 3
100%
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%
modifyString(String, EStructuralFeature.Setting)
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
readStringModifiers()
M: 28 C: 51
65%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 14 C: 15
52%
M: 0 C: 1
100%
static {...}
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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 - initial API and implementation
13: * Johannes Faltermeier - Bug 459998
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.spi.provider;
16:
17: import java.lang.reflect.Constructor;
18: import java.lang.reflect.InvocationTargetException;
19: import java.util.Comparator;
20: import java.util.Set;
21: import java.util.SortedSet;
22: import java.util.TreeSet;
23:
24: import org.eclipse.core.runtime.IConfigurationElement;
25: import org.eclipse.core.runtime.IExtensionRegistry;
26: import org.eclipse.core.runtime.Platform;
27: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
28: import org.eclipse.emf.ecp.view.internal.ui.Activator;
29: import org.osgi.framework.Bundle;
30:
31: /**
32: * A helper to ease the access to the string modifiers.
33: *
34: * @author Eugen Neufeld
35: * @author Johannes Faltermeier
36: * @since 1.3
37: */
38: public final class ECPTooltipModifierHelper {
39:         private static final String CLASS_CANNOT_BE_RESOLVED = "%1$s cannot be loaded because bundle %2$s cannot be resolved."; //$NON-NLS-1$
40:         private static final String CLASS = "class"; //$NON-NLS-1$
41:         private static final String EXTENSION_POINT_ID = "org.eclipse.emf.ecp.ui.view.tooltipModifier"; //$NON-NLS-1$
42:
43:         private ECPTooltipModifierHelper() {
44:
45:         }
46:
47:         private static SortedSet<ECPStringModifier> stringModifiers = new TreeSet<ECPStringModifier>(
48:                 new Comparator<ECPStringModifier>() {
49:                         @Override
50:                         public int compare(ECPStringModifier arg0, ECPStringModifier arg1) {
51:                                 if (arg0.getPriority() == arg1.getPriority()) {
52:                                         /* compare would return 0, meaning the modifiers are identical -> 1 service would get lost */
53:                                         return arg0.getClass().getName().compareTo(arg1.getClass().getName());
54:                                 }
55:                                 final double compare = arg0.getPriority() - arg1.getPriority();
56:                                 return compare < 0 ? -1 : 1;
57:                         }
58:                 });
59:
60:         private static synchronized Set<ECPStringModifier> getStringModifiers() {
61:•                if (stringModifiers == null || stringModifiers.isEmpty()) {
62:                         readStringModifiers();
63:                 }
64:                 return stringModifiers;
65:         }
66:
67:         private static void readStringModifiers() {
68:                 final IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
69:•                if (extensionRegistry == null) {
70:                         return;
71:                 }
72:                 final IConfigurationElement[] controls = extensionRegistry
73:                         .getConfigurationElementsFor(
74:                                 EXTENSION_POINT_ID);
75:•                for (final IConfigurationElement e : controls) {
76:                         try {
77:                                 final String clazz = e.getAttribute(CLASS);
78:                                 final Class<? extends ECPStringModifier> resolvedClass = loadClass(e
79:                                         .getContributor().getName(), clazz);
80:                                 final Constructor<? extends ECPStringModifier> controlConstructor = resolvedClass
81:                                         .getConstructor();
82:                                 final ECPStringModifier stringModifier = controlConstructor.newInstance();
83:                                 stringModifiers.add(stringModifier);
84:                         } catch (final ClassNotFoundException ex) {
85:                                 Activator.log(ex);
86:                         } catch (final NoSuchMethodException ex) {
87:                                 Activator.log(ex);
88:                         } catch (final SecurityException ex) {
89:                                 Activator.log(ex);
90:                         } catch (final InstantiationException ex) {
91:                                 Activator.log(ex);
92:                         } catch (final IllegalAccessException ex) {
93:                                 Activator.log(ex);
94:                         } catch (final IllegalArgumentException ex) {
95:                                 Activator.log(ex);
96:                         } catch (final InvocationTargetException ex) {
97:                                 Activator.log(ex);
98:                         }
99:
100:                 }
101:
102:         }
103:
104:         @SuppressWarnings("unchecked")
105:         private static <T> Class<T> loadClass(String bundleName, String clazz)
106:                 throws ClassNotFoundException {
107:                 final Bundle bundle = Platform.getBundle(bundleName);
108:•                if (bundle == null) {
109:                         throw new ClassNotFoundException(String.format(
110:                                 CLASS_CANNOT_BE_RESOLVED, clazz, bundleName));
111:                 }
112:                 return (Class<T>) bundle.loadClass(clazz);
113:
114:         }
115:
116:         /**
117:          * Modifies a string by iterating over all known {@link ECPStringModifier}.
118:          *
119:          * @param text the text to modify
120:          * @param setting the {@link Setting} used to create the text
121:          * @return the modified text
122:          */
123:         public static String modifyString(String text, Setting setting) {
124:                 String returnValue = text;
125:•                for (final ECPStringModifier tooltipModifier : getStringModifiers()) {
126:                         returnValue = tooltipModifier.modifyString(returnValue, setting);
127:                 }
128:                 return returnValue;
129:         }
130:
131: }