]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/BundleIcon.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / BundleIcon.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import java.net.URL;\r
15 \r
16 import org.eclipse.core.runtime.Platform;\r
17 import org.eclipse.jface.resource.ImageDescriptor;\r
18 import org.osgi.framework.Bundle;\r
19 import org.simantics.Simantics;\r
20 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.request.Read;\r
25 import org.simantics.utils.datastructures.Pair;\r
26 \r
27 /**\r
28  * @author Antti Villberg\r
29  */\r
30 public class BundleIcon implements ModelledIcon {\r
31 \r
32     private final Resource       configuration;\r
33     private Pair<String, String> location;\r
34 \r
35     public BundleIcon(ReadGraph graph, Resource configuration) throws DatabaseException {\r
36         this.configuration = configuration;\r
37         this.location = graph.syncRequest(new Location(configuration));\r
38     }\r
39 \r
40     @Override\r
41     public ImageDescriptor create() throws DatabaseException {\r
42         Pair<String, String> bundleAndPath = Simantics.getSession().syncRequest(new Location(configuration));\r
43         if (bundleAndPath == null)\r
44             return null;\r
45 \r
46         Bundle bundle = Platform.getBundle(bundleAndPath.first);\r
47         if (bundle == null)\r
48             return null;\r
49         URL url = bundle.getEntry(bundleAndPath.second);\r
50         return ImageDescriptor.createFromURL(url);\r
51     }\r
52 \r
53     @Override\r
54     public String toString() {\r
55         return super.toString() + "[" + location + "]";\r
56     }\r
57 \r
58     static class Location implements Read<Pair<String, String>> {\r
59         Resource configuration;\r
60         public Location(Resource configuration) {\r
61             this.configuration = configuration;\r
62         }\r
63         @Override\r
64         public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
65             BrowsingResource BRO = BrowsingResource.getInstance(graph);\r
66             String bundle = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Bundle);\r
67             if (bundle == null)\r
68                 return null;\r
69             String path = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Path);\r
70             if (path == null)\r
71                 return null;\r
72             return Pair.make(bundle, path);\r
73         }\r
74     }\r
75 \r
76 }\r