1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.image.impl;
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;
26 * Lazy initialization of bundle resources.
27 * This class is capable of opening .svg .png .jpg etc files.
29 * @See {@link ImageUtils}
30 * @author Toni Kalajainen
32 public class ImageBundleFactory extends ImageURLFactory implements IFactory<Image> {
34 private static URL _resolveFilename(String filename)
35 throws RuntimeException
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);
43 throw new RuntimeException("File '" + bundlePath + "' not found in bundle '" + bundleId + "'");
47 public ImageBundleFactory(String filename)
48 throws RuntimeException
50 super(_resolveFilename(filename));