]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/BundleIcon.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / BundleIcon.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.net.URL;
15
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;
26
27 /**
28  * @author Antti Villberg
29  */
30 public class BundleIcon implements ModelledIcon {
31
32     private final Resource       configuration;
33     private Pair<String, String> location;
34
35     public BundleIcon(ReadGraph graph, Resource configuration) throws DatabaseException {
36         this.configuration = configuration;
37         this.location = graph.syncRequest(new Location(configuration));
38     }
39
40     @Override
41     public ImageDescriptor create() throws DatabaseException {
42         Pair<String, String> bundleAndPath = Simantics.getSession().syncRequest(new Location(configuration));
43         if (bundleAndPath == null)
44             return null;
45
46         Bundle bundle = Platform.getBundle(bundleAndPath.first);
47         if (bundle == null)
48             return null;
49         URL url = bundle.getEntry(bundleAndPath.second);
50         return ImageDescriptor.createFromURL(url);
51     }
52
53     @Override
54     public String toString() {
55         return super.toString() + "[" + location + "]";
56     }
57
58     static class Location implements Read<Pair<String, String>> {
59         Resource configuration;
60         public Location(Resource configuration) {
61             this.configuration = configuration;
62         }
63         @Override
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);
67             if (bundle == null)
68                 return null;
69             String path = graph.getPossibleRelatedValue(configuration, BRO.BundleIcon_Path);
70             if (path == null)
71                 return null;
72             return Pair.make(bundle, path);
73         }
74     }
75
76 }