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