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