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