Skip to content

Package: Activator

Activator

nameinstructionbranchcomplexitylinemethod
Activator()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getDefault()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getImage(URL)
M: 6 C: 30
83%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 0 C: 4
100%
M: 0 C: 1
100%
getVTViewTemplateProvider()
M: 2 C: 21
91%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
start(BundleContext)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
stop(BundleContext)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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: * Lucas Koehler - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emfforms.internal.swt.core;
16:
17: import java.net.URL;
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20:
21: import org.eclipse.core.runtime.Plugin;
22: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
23: import org.eclipse.jface.resource.ImageDescriptor;
24: import org.eclipse.swt.graphics.Image;
25: import org.osgi.framework.BundleContext;
26: import org.osgi.framework.ServiceReference;
27:
28: /**
29: * The activator class controls the plug-in life cycle.
30: */
31: public class Activator extends Plugin {
32:         private static final String NULL = "NULL"; //$NON-NLS-1$
33:
34:         /** The plug-in ID. **/
35:         public static final String PLUGIN_ID = "org.eclipse.emfforms.swt.core"; //$NON-NLS-1$
36:
37:         /** The shared instance. **/
38:         private static Activator plugin;
39:
40:         /**
41:          * The constructor.
42:          */
43:         public Activator() {
44:         }
45:
46:         @Override
47:         public void start(BundleContext context) throws Exception {
48:                 super.start(context);
49:                 plugin = this;
50:         }
51:
52:         @Override
53:         public void stop(BundleContext context) throws Exception {
54:                 imageRegistry.values().forEach(Image::dispose);
55:•                if (viewTemplateReference != null) {
56:                         context.ungetService(viewTemplateReference);
57:                 }
58:                 super.stop(context);
59:                 plugin = null;
60:         }
61:
62:         /**
63:          * Returns the shared instance.
64:          *
65:          * @return the shared instance
66:          */
67:         public static Activator getDefault() {
68:                 return plugin;
69:         }
70:
71:         private final Map<String, Image> imageRegistry = new LinkedHashMap<String, Image>(20, .8F, true) {
72:                 private static final long serialVersionUID = 1L;
73:
74:                 // This method is called just after a new entry has been added
75:                 @Override
76:                 public boolean removeEldestEntry(Map.Entry<String, Image> eldest) {
77:                         return size() > 20;
78:                 }
79:
80:                 @Override
81:                 public Image remove(Object arg0) {
82:                         final Image image = super.remove(arg0);
83:                         image.dispose();
84:                         return image;
85:                 }
86:
87:         };
88:
89:         /**
90:          * Loads an image based on the provided {@link URL} form this bundle. The url may be null, then an empty image is
91:          * returned.
92:          *
93:          * @param url the {@link URL} to load the {@link Image} from
94:          * @return the {@link Image}
95:          */
96:         public static Image getImage(URL url) {
97:•                if (!getDefault().imageRegistry.containsKey(url == null ? NULL : url.toExternalForm())) {
98:                         final ImageDescriptor createFromURL = ImageDescriptor.createFromURL(url);
99:•                        getDefault().imageRegistry.put(url == null ? NULL : url.toExternalForm(), createFromURL.createImage());
100:                 }
101:•                return getDefault().imageRegistry.get(url == null ? NULL : url.toExternalForm());
102:
103:         }
104:
105:         private ServiceReference<VTViewTemplateProvider> viewTemplateReference;
106:
107:         /**
108:          * Returns the currentInstance of the {@link VTViewTemplateProvider}.
109:          *
110:          * @return the {@link VTViewTemplateProvider}
111:          */
112:         public VTViewTemplateProvider getVTViewTemplateProvider() {
113:•                if (viewTemplateReference == null) {
114:                         viewTemplateReference = plugin.getBundle().getBundleContext()
115:                                 .getServiceReference(VTViewTemplateProvider.class);
116:                 }
117:•                if (viewTemplateReference != null) {
118:                         return plugin.getBundle().getBundleContext().getService(viewTemplateReference);
119:                 }
120:                 return null;
121:         }
122: }