Skip to content

Package: EClassLabelProvider

EClassLabelProvider

nameinstructionbranchcomplexitylinemethod
EClassLabelProvider(EMFFormsLocalizationService)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addListener(ILabelProviderListener)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getImage(Object)
M: 1 C: 42
98%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 12
100%
M: 0 C: 1
100%
getText(Object)
M: 0 C: 37
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
isLabelProperty(Object, String)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
removeListener(ILabelProviderListener)
M: 1 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) 2018 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.datatemplate;
15:
16: import java.net.URL;
17: import java.util.Enumeration;
18:
19: import org.eclipse.emf.ecore.EClass;
20: import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
21: import org.eclipse.emfforms.spi.common.BundleResolver;
22: import org.eclipse.emfforms.spi.common.BundleResolver.NoBundleFoundException;
23: import org.eclipse.emfforms.spi.common.BundleResolverFactory;
24: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
25: import org.eclipse.jface.viewers.ILabelProvider;
26: import org.eclipse.jface.viewers.ILabelProviderListener;
27: import org.eclipse.swt.graphics.Image;
28: import org.osgi.framework.Bundle;
29:
30: /**
31: * Create a label provider for {@link EClass}es that doesn't present them as they
32: * are in the Ecore model editor, but instead using their EMF.Edit localized names
33: * and icons.
34: */
35: final class EClassLabelProvider implements ILabelProvider {
36:         private final EMFFormsLocalizationService l10nService;
37:         private final BundleResolver bundleResolver;
38:
39:         /**
40:          * Initializes me with the EMF Forms localization service from which I get
41:          * the EMF.Edit plug-in strings.
42:          *
43:          * @param l10nService the localization service
44:          */
45:         EClassLabelProvider(EMFFormsLocalizationService l10nService) {
46:                 super();
47:
48:                 this.l10nService = l10nService;
49:                 bundleResolver = BundleResolverFactory.createBundleResolver();
50:         }
51:
52:         @Override
53:         public void dispose() {
54:                 // I allocate nothing disposable
55:         }
56:
57:         @Override
58:         public String getText(Object element) {
59:•                if (!(element instanceof EClass)) {
60:                         return String.valueOf(element);
61:                 }
62:
63:                 final EClass eClass = (EClass) element;
64:                 Bundle editBundle;
65:
66:                 try {
67:                         editBundle = bundleResolver.getEditBundle(eClass);
68:                 } catch (final NoBundleFoundException e) {
69:                         editBundle = null;
70:                 }
71:
72:•                return editBundle != null
73:                         ? l10nService.getString(editBundle, String.format("_UI_%s_type", eClass.getName())) //$NON-NLS-1$
74:                         : eClass.getName();
75:         }
76:
77:         @Override
78:         public Image getImage(Object element) {
79:•                if (!(element instanceof EClass)) {
80:                         return null;
81:                 }
82:
83:                 final EClass eClass = (EClass) element;
84:                 Bundle editBundle;
85:
86:                 Enumeration<URL> imageURLs = null;
87:                 try {
88:                         editBundle = bundleResolver.getEditBundle(eClass);
89:                         imageURLs = editBundle.findEntries("icons/full/obj16", //$NON-NLS-1$
90:                                 String.format("%s.*", eClass.getName()), //$NON-NLS-1$
91:                                 false);
92:                 } catch (final NoBundleFoundException e) {
93:                         // Expected
94:                 }
95:
96:                 // Don't keep these in a resource manager for disposal because the images
97:                 // in the EMF image registry must never be disposed: they are shared
98:•                return imageURLs != null && imageURLs.hasMoreElements()
99:                         ? ExtendedImageRegistry.getInstance().getImage(imageURLs.nextElement())
100:                         : null;
101:         }
102:
103:         @Override
104:         public boolean isLabelProperty(Object element, String property) {
105:                 return true;
106:         }
107:
108:         @Override
109:         public void addListener(ILabelProviderListener listener) {
110:                 // I do not notify, so don't need to track listeners
111:         }
112:
113:         @Override
114:         public void removeListener(ILabelProviderListener listener) {
115:                 // I do not notify, so don't need to track listeners
116:         }
117:
118: }