Skip to content

Package: Activator$1

Activator$1

nameinstructionbranchcomplexitylinemethod
remove(Object)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
removeEldestEntry(Map.Entry)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
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: *******************************************************************************/
15: package org.eclipse.emf.ecp.edit.internal.swt;
16:
17: import java.net.URL;
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20:
21: import org.eclipse.core.runtime.IStatus;
22: import org.eclipse.core.runtime.Plugin;
23: import org.eclipse.core.runtime.Status;
24: import org.eclipse.emf.ecp.edit.spi.ECPControlFactory;
25: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
26: import org.eclipse.jface.action.Action;
27: import org.eclipse.jface.resource.ImageDescriptor;
28: import org.eclipse.swt.graphics.Image;
29: import org.eclipse.swt.graphics.ImageData;
30: import org.osgi.framework.BundleContext;
31: import org.osgi.framework.ServiceReference;
32:
33: /**
34: * The activator class controls the plug-in life cycle.
35: */
36: public class Activator extends Plugin {
37:
38:         /** The plug-in ID. **/
39:         public static final String PLUGIN_ID = "org.eclipse.emf.ecp.edit.swt"; //$NON-NLS-1$
40:
41:         /** The shared instance. **/
42:         private static Activator plugin;
43:
44:         /**
45:          * The constructor.
46:          */
47:         public Activator() {
48:         }
49:
50:         // BEGIN SUPRESS CATCH EXCEPTION
51:         @Override
52:         public void start(BundleContext context) throws Exception {
53:                 super.start(context);
54:                 plugin = this;
55:         }
56:
57:         @Override
58:         public void stop(BundleContext context) throws Exception {
59:                 for (final ImageDescriptorToImage descriptorToImage : imageRegistry.values()) {
60:                         descriptorToImage.getImage().dispose();
61:                 }
62:                 for (final ImageDescriptorToImage descriptorToImage : imageRegistryByAction.values()) {
63:                         descriptorToImage.getImage().dispose();
64:                 }
65:                 if (viewTemplateReference != null) {
66:                         context.ungetService(viewTemplateReference);
67:                 }
68:                 super.stop(context);
69:                 plugin = null;
70:         }
71:
72:         // END SUPRESS CATCH EXCEPTION
73:         /**
74:          * Returns the shared instance.
75:          *
76:          * @return the shared instance
77:          */
78:         public static Activator getDefault() {
79:                 return plugin;
80:         }
81:
82:         /**
83:          * Logs exception.
84:          *
85:          * @param e the {@link Exception} to log
86:          */
87:         public static void logException(Exception e) {
88:                 getDefault().getLog().log(
89:                         new Status(IStatus.ERROR, Activator.getDefault().getBundle().getSymbolicName(), e.getMessage(), e));
90:         }
91:
92:         // TODO check if necessary
93:         private final Map<String, ImageDescriptorToImage> imageRegistry = new LinkedHashMap<String, ImageDescriptorToImage>(
94:                 20, .8F, true) {
95:                 private static final long serialVersionUID = 1L;
96:
97:                 // This method is called just after a new entry has been added
98:                 @Override
99:                 public boolean removeEldestEntry(Map.Entry<String, ImageDescriptorToImage> eldest) {
100:•                        return size() > 20;
101:                 }
102:
103:                 @Override
104:                 public ImageDescriptorToImage remove(Object arg0) {
105:                         final ImageDescriptorToImage image = super.remove(arg0);
106:                         image.getImage().dispose();
107:                         return image;
108:                 }
109:
110:         };
111:
112:         private final Map<Action, ImageDescriptorToImage> imageRegistryByAction = new LinkedHashMap<Action, ImageDescriptorToImage>();
113:
114:         /**
115:          * Loads an image based on the provided path form this bundle.
116:          *
117:          * @param path the bundle specific path to the image
118:          * @return the {@link Image}
119:          */
120:         public static Image getImage(String path) {
121:                 if (!getDefault().imageRegistry.containsKey(path)) {
122:                         getDefault().imageRegistry.put(path,
123:                                 new ImageDescriptorToImage(ImageDescriptor.createFromURL(getDefault().getBundle().getResource(path))));
124:                 }
125:                 return getDefault().imageRegistry.get(path).getImage();
126:
127:         }
128:
129:         /**
130:          * Loads an image for the given Action.
131:          *
132:          * @param action the action
133:          * @return the {@link Image}
134:          */
135:         public static Image getImage(Action action) {
136:                 final String path = action.toString();
137:                 if (!getDefault().imageRegistry.containsKey(path)) {
138:                         getDefault().imageRegistry.put(path,
139:                                 new ImageDescriptorToImage(action.getImageDescriptor()));
140:                 }
141:                 return getDefault().imageRegistry.get(path).getImage();
142:
143:         }
144:
145:         /**
146:          * Loads an image based on the provided {@link URL} form this bundle. The url may be null, then an empty image is
147:          * returned.
148:          *
149:          * @param url the {@link URL} to load the {@link Image} from
150:          * @return the {@link Image}
151:          */
152:         public static Image getImage(URL url) {
153:                 if (!getDefault().imageRegistry.containsKey(url == null ? "NULL" : url.toExternalForm())) { //$NON-NLS-1$
154:
155:                         final ImageDescriptor createFromURL = ImageDescriptor.createFromURL(url);
156:                         final ImageData imageData = createFromURL.getImageData();
157:                         getDefault().imageRegistry.put(url == null ? "NULL" : url.toExternalForm(), new ImageDescriptorToImage( //$NON-NLS-1$
158:                                 createFromURL));
159:                 }
160:                 return getDefault().imageRegistry.get(url == null ? "NULL" : url.toExternalForm()).getImage(); //$NON-NLS-1$
161:
162:         }
163:
164:         /**
165:          * Loads an {@link ImageDescriptor} based on the provided path form this bundle.
166:          *
167:          * @param path the bundle specific path to the {@link ImageDescriptor}
168:          * @return the {@link ImageDescriptor}
169:          */
170:         public static ImageDescriptor getImageDescriptor(String path) {
171:                 if (!getDefault().imageRegistry.containsKey(path)) {
172:                         getDefault().imageRegistry.put(path,
173:                                 new ImageDescriptorToImage(ImageDescriptor.createFromURL(getDefault().getBundle().getResource(path))));
174:                 }
175:                 return getDefault().imageRegistry.get(path).getImageDescriptor();
176:
177:         }
178:
179:         /**
180:          * Loads an {@link ImageData} based on the provided {@link URL}.
181:          *
182:          * @param url the {@link URL} to the {@link ImageData}
183:          * @return the {@link ImageData}
184:          */
185:         public static ImageData getImageData(URL url) {
186:                 return ImageDescriptor.createFromURL(url).getImageData();
187:         }
188:
189:         private ServiceReference<ECPControlFactory> controlFactoryReference;
190:
191:         /**
192:          * Returns the currentInstance of the control factory.
193:          *
194:          * @return the {@link ECPControlFactory}
195:          */
196:         public ECPControlFactory getECPControlFactory() {
197:                 if (controlFactoryReference == null) {
198:                         controlFactoryReference = plugin.getBundle().getBundleContext()
199:                                 .getServiceReference(ECPControlFactory.class);
200:                 }
201:                 return plugin.getBundle().getBundleContext().getService(controlFactoryReference);
202:         }
203:
204:         /**
205:          * Frees the OSGi Service, so that it can be shutdown during runtime.
206:          */
207:         public void ungetECPControlFactory() {
208:                 if (controlFactoryReference == null) {
209:                         return;
210:                 }
211:                 plugin.getBundle().getBundleContext().ungetService(controlFactoryReference);
212:                 controlFactoryReference = null;
213:         }
214:
215:         private ServiceReference<VTViewTemplateProvider> viewTemplateReference;
216:
217:         /**
218:          * Returns the currentInstance of the {@link VTViewTemplateProvider}.
219:          *
220:          * @return the {@link ECPControlFactory}
221:          */
222:         public VTViewTemplateProvider getVTViewTemplateProvider() {
223:                 if (viewTemplateReference == null) {
224:                         viewTemplateReference = plugin.getBundle().getBundleContext()
225:                                 .getServiceReference(VTViewTemplateProvider.class);
226:                 }
227:                 if (viewTemplateReference != null) {
228:                         return plugin.getBundle().getBundleContext().getService(viewTemplateReference);
229:                 }
230:                 return null;
231:         }
232:
233: }