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