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