--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.osgi.framework.Bundle;\r
+\r
+public final class BundleUtils {\r
+\r
+ /**\r
+ * Returns an image descriptor for the image file at the given\r
+ * plug-in relative path.\r
+ *\r
+ * @param pluginId the plugin to search\r
+ * @param imageFilePath the path\r
+ * @return the image descriptor\r
+ */\r
+ public static ImageDescriptor getImageDescriptorFromBundle(Bundle bundle, String imageFilePath) {\r
+ if (bundle == null) {\r
+ throw new IllegalArgumentException("null bundle");\r
+ }\r
+ if (imageFilePath == null) {\r
+ throw new IllegalArgumentException("null image path");\r
+ }\r
+\r
+ // if the bundle is not ready then there is no image\r
+ if (!isReady(bundle))\r
+ return null;\r
+\r
+ // look for the image (this will check both the plugin and fragment folders\r
+ URL fullPathString = FileLocator.find(bundle, new Path(imageFilePath), null);\r
+ if (fullPathString == null) {\r
+ try {\r
+ fullPathString = new URL(imageFilePath);\r
+ } catch (MalformedURLException e) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ return ImageDescriptor.createFromURL(fullPathString);\r
+ }\r
+\r
+ /**\r
+ * Returns an image descriptor for the image file at the given\r
+ * plug-in relative path.\r
+ *\r
+ * @param pluginId the plugin to search\r
+ * @param imageFilePath the path\r
+ * @return the image descriptor\r
+ */\r
+ public static ImageDescriptor getImageDescriptorFromPlugin(String pluginId, String imageFilePath) {\r
+ if (pluginId == null || imageFilePath == null) {\r
+ throw new IllegalArgumentException();\r
+ }\r
+ Bundle bundle = Platform.getBundle(pluginId);\r
+ return getImageDescriptorFromBundle(bundle, imageFilePath);\r
+ }\r
+\r
+ public static boolean isReady(Bundle bundle) {\r
+ return bundle != null && isReady(bundle.getState());\r
+ }\r
+ \r
+ public static boolean isReady(int bundleState) {\r
+ return (bundleState & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING)) != 0;\r
+ }\r
+\r
+ public static URL find(Bundle bundle, String path) {\r
+ if (bundle == null)\r
+ return null;\r
+ return FileLocator.find(bundle, new Path(path), null);\r
+ }\r
+\r
+ public static URL find(String bundleId, String path) {\r
+ return find(Platform.getBundle(bundleId), path);\r
+ }\r
+ \r
+ public static File findFile(String bundleId, String path) throws IOException {\r
+ URL url = find(bundleId, path);\r
+ if (url == null)\r
+ return null;\r
+ url = FileLocator.toFileURL(url);\r
+ return new File(url.getPath());\r
+ }\r
+ \r
+}\r