]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/ImageBundleFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / ImageBundleFactory.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.image.impl;\r
13 \r
14 import java.net.URL;\r
15 \r
16 import org.eclipse.core.runtime.FileLocator;\r
17 import org.eclipse.core.runtime.IPath;\r
18 import org.eclipse.core.runtime.Path;\r
19 import org.eclipse.core.runtime.Platform;\r
20 import org.osgi.framework.Bundle;\r
21 import org.simantics.g2d.image.Image;\r
22 import org.simantics.g2d.image.ImageUtils;\r
23 import org.simantics.utils.datastructures.cache.IFactory;\r
24 \r
25 /**\r
26  * Lazy initialization of bundle resources.\r
27  * This class is capable of opening .svg .png .jpg etc files.\r
28  * \r
29  * @See {@link ImageUtils}\r
30  * @author Toni Kalajainen\r
31  */\r
32 public class ImageBundleFactory extends ImageURLFactory implements IFactory<Image> {\r
33 \r
34         private static URL _resolveFilename(String filename)\r
35         throws RuntimeException\r
36         {\r
37                 IPath p = new Path(filename);\r
38                 String bundleId = p.segment(0);\r
39                 IPath bundlePath = p.removeFirstSegments(1);            \r
40                 Bundle bundle = Platform.getBundle(bundleId);\r
41                 URL url = FileLocator.find(bundle, bundlePath, null);\r
42                 if (url==null) \r
43                 throw new RuntimeException("File '" + bundlePath + "' not found in bundle '" + bundleId + "'");\r
44                 return url;\r
45         }\r
46         \r
47         public ImageBundleFactory(String filename) \r
48         throws RuntimeException\r
49         {\r
50                 super(_resolveFilename(filename));              \r
51         }\r
52         \r
53 }\r