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