]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/BundleUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / BundleUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 import java.net.MalformedURLException;\r
17 import java.net.URL;\r
18 \r
19 import org.eclipse.core.runtime.FileLocator;\r
20 import org.eclipse.core.runtime.Path;\r
21 import org.eclipse.core.runtime.Platform;\r
22 import org.eclipse.jface.resource.ImageDescriptor;\r
23 import org.osgi.framework.Bundle;\r
24 \r
25 public final class BundleUtils {\r
26 \r
27     /**\r
28      * Returns an image descriptor for the image file at the given\r
29      * plug-in relative path.\r
30      *\r
31      * @param pluginId the plugin to search\r
32      * @param imageFilePath the path\r
33      * @return the image descriptor\r
34      */\r
35     public static ImageDescriptor getImageDescriptorFromBundle(Bundle bundle, String imageFilePath) {\r
36         if (bundle == null) {\r
37             throw new IllegalArgumentException("null bundle");\r
38         }\r
39         if (imageFilePath == null) {\r
40             throw new IllegalArgumentException("null image path");\r
41         }\r
42 \r
43         // if the bundle is not ready then there is no image\r
44         if (!isReady(bundle))\r
45             return null;\r
46 \r
47         // look for the image (this will check both the plugin and fragment folders\r
48         URL fullPathString = FileLocator.find(bundle, new Path(imageFilePath), null);\r
49         if (fullPathString == null) {\r
50             try {\r
51                 fullPathString = new URL(imageFilePath);\r
52             } catch (MalformedURLException e) {\r
53                 return null;\r
54             }\r
55         }\r
56 \r
57         return ImageDescriptor.createFromURL(fullPathString);\r
58     }\r
59 \r
60     /**\r
61      * Returns an image descriptor for the image file at the given\r
62      * plug-in relative path.\r
63      *\r
64      * @param pluginId the plugin to search\r
65      * @param imageFilePath the path\r
66      * @return the image descriptor\r
67      */\r
68     public static ImageDescriptor getImageDescriptorFromPlugin(String pluginId, String imageFilePath) {\r
69         if (pluginId == null || imageFilePath == null) {\r
70             throw new IllegalArgumentException();\r
71         }\r
72         Bundle bundle = Platform.getBundle(pluginId);\r
73         return getImageDescriptorFromBundle(bundle, imageFilePath);\r
74     }\r
75 \r
76     public static boolean isReady(Bundle bundle) {\r
77         return bundle != null && isReady(bundle.getState());\r
78     }\r
79     \r
80     public static boolean isReady(int bundleState) {\r
81         return (bundleState & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING)) != 0;\r
82     }\r
83 \r
84     public static URL find(Bundle bundle, String path) {\r
85         if (bundle == null)\r
86             return null;\r
87         return FileLocator.find(bundle, new Path(path), null);\r
88     }\r
89 \r
90     public static URL find(String bundleId, String path) {\r
91         return find(Platform.getBundle(bundleId), path);\r
92     }\r
93         \r
94         public static File findFile(String bundleId, String path) throws IOException {\r
95                 URL url = find(bundleId, path);\r
96                 if (url == null)\r
97                     return null;\r
98                 url = FileLocator.toFileURL(url);\r
99                 return new File(url.getPath());\r
100         }\r
101     \r
102 }\r