1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui;
15 import java.io.IOException;
16 import java.net.MalformedURLException;
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;
25 public final class BundleUtils {
28 * Returns an image descriptor for the image file at the given
29 * plug-in relative path.
31 * @param pluginId the plugin to search
32 * @param imageFilePath the path
33 * @return the image descriptor
35 public static ImageDescriptor getImageDescriptorFromBundle(Bundle bundle, String imageFilePath) {
37 throw new IllegalArgumentException("null bundle");
39 if (imageFilePath == null) {
40 throw new IllegalArgumentException("null image path");
43 // if the bundle is not ready then there is no image
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) {
51 fullPathString = new URL(imageFilePath);
52 } catch (MalformedURLException e) {
57 return ImageDescriptor.createFromURL(fullPathString);
61 * Returns an image descriptor for the image file at the given
62 * plug-in relative path.
64 * @param pluginId the plugin to search
65 * @param imageFilePath the path
66 * @return the image descriptor
68 public static ImageDescriptor getImageDescriptorFromPlugin(String pluginId, String imageFilePath) {
69 if (pluginId == null || imageFilePath == null) {
70 throw new IllegalArgumentException();
72 Bundle bundle = Platform.getBundle(pluginId);
73 return getImageDescriptorFromBundle(bundle, imageFilePath);
76 public static boolean isReady(Bundle bundle) {
77 return bundle != null && isReady(bundle.getState());
80 public static boolean isReady(int bundleState) {
81 return (bundleState & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING)) != 0;
84 public static URL find(Bundle bundle, String path) {
87 return FileLocator.find(bundle, new Path(path), null);
90 public static URL find(String bundleId, String path) {
91 return find(Platform.getBundle(bundleId), path);
94 public static File findFile(String bundleId, String path) throws IOException {
95 URL url = find(bundleId, path);
98 url = FileLocator.toFileURL(url);
99 return new File(url.getPath());
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());