1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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.browsing.ui.swt;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.osgi.framework.Bundle;
19 import org.simantics.Simantics;
20 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.request.Read;
25 import org.simantics.utils.datastructures.Pair;
28 * @author Antti Villberg
30 public class BundleIcon implements ModelledIcon {
32 private final Resource configuration;
33 private Pair<String, String> location;
35 public BundleIcon(ReadGraph graph, Resource configuration) throws DatabaseException {
36 this.configuration = configuration;
37 this.location = graph.syncRequest(new Location(configuration));
41 public ImageDescriptor create() throws DatabaseException {
42 Pair<String, String> bundleAndPath = Simantics.getSession().syncRequest(new Location(configuration));
43 if (bundleAndPath == null)
46 Bundle bundle = Platform.getBundle(bundleAndPath.first);
49 URL url = bundle.getEntry(bundleAndPath.second);
50 return ImageDescriptor.createFromURL(url);
54 public String toString() {
55 return super.toString() + "[" + location + "]";
58 static class Location implements Read<Pair<String, String>> {
59 Resource configuration;
60 public Location(Resource configuration) {
61 this.configuration = configuration;
64 public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {
65 BrowsingResource BRO = BrowsingResource.getInstance(graph);
66 String bundle = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Bundle);
69 String path = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Path);
72 return Pair.make(bundle, path);