--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import java.net.URL;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.osgi.framework.Bundle;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.stubs.BrowsingResource;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class BundleIcon implements ModelledIcon {\r
+\r
+ private final Resource configuration;\r
+ private Pair<String, String> location;\r
+\r
+ public BundleIcon(ReadGraph graph, Resource configuration) throws DatabaseException {\r
+ this.configuration = configuration;\r
+ this.location = graph.syncRequest(new Location(configuration));\r
+ }\r
+\r
+ @Override\r
+ public ImageDescriptor create() throws DatabaseException {\r
+ Pair<String, String> bundleAndPath = Simantics.getSession().syncRequest(new Location(configuration));\r
+ if (bundleAndPath == null)\r
+ return null;\r
+\r
+ Bundle bundle = Platform.getBundle(bundleAndPath.first);\r
+ if (bundle == null)\r
+ return null;\r
+ URL url = bundle.getEntry(bundleAndPath.second);\r
+ return ImageDescriptor.createFromURL(url);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return super.toString() + "[" + location + "]";\r
+ }\r
+\r
+ static class Location implements Read<Pair<String, String>> {\r
+ Resource configuration;\r
+ public Location(Resource configuration) {\r
+ this.configuration = configuration;\r
+ }\r
+ @Override\r
+ public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
+ BrowsingResource BRO = BrowsingResource.getInstance(graph);\r
+ String bundle = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Bundle);\r
+ if (bundle == null)\r
+ return null;\r
+ String path = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Path);\r
+ if (path == null)\r
+ return null;\r
+ return Pair.make(bundle, path);\r
+ }\r
+ }\r
+\r
+}\r