]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/Activator.java
Merge changes Ib64cf026,I238948da
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / Activator.java
1 package org.simantics.tests.modelled.ui;
2
3 import java.net.URL;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.eclipse.jface.resource.ImageDescriptor;
8 import org.eclipse.swt.graphics.Image;
9 import org.eclipse.swt.graphics.Resource;
10 import org.eclipse.ui.plugin.AbstractUIPlugin;
11 import org.osgi.framework.BundleContext;
12
13 /**
14  * The activator class controls the plug-in life cycle
15  */
16 public class Activator extends AbstractUIPlugin {
17
18         // The plug-in ID
19         public static final String PLUGIN_ID = "org.simantics.tests.modelled.ui"; //$NON-NLS-1$
20
21         // The shared instance
22         private static Activator plugin;
23         
24         /**
25          * The constructor
26          */
27         public Activator() {
28         }
29
30         /*
31          * (non-Javadoc)
32          * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
33          */
34         public void start(BundleContext context) throws Exception {
35                 super.start(context);
36                 plugin = this;
37         }
38
39         /*
40          * (non-Javadoc)
41          * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
42          */
43         public void stop(BundleContext context) throws Exception {
44                 plugin = null;
45                 imagesToDispose.forEach(Resource::dispose);
46                 super.stop(context);
47         }
48
49         /**
50          * Returns the shared instance
51          *
52          * @return the shared instance
53          */
54         public static Activator getDefault() {
55                 return plugin;
56         }
57
58     public static ImageDescriptor getImageDescriptor(String relativePath) {
59         URL url = getDefault().getBundle().getEntry(relativePath);
60         return ImageDescriptor.createFromURL(url);
61     }
62     
63     private static List<Image> imagesToDispose = new ArrayList<>();
64
65     public static Image createManagedImage(ImageDescriptor descriptor) {
66         Image image= descriptor.createImage();
67         if (image == null) {
68             image= ImageDescriptor.getMissingImageDescriptor().createImage();
69         }
70         imagesToDispose.add(image);
71         return image;
72     }
73 }