]> gerrit.simantics Code Review - simantics/district.git/blob
d6ae2f61dbf133026eb55f38edd3ce0d28cbd4c9
[simantics/district.git] /
1 package org.simantics.district.network.ui.ontology;
2
3 import org.simantics.db.RequestProcessor;
4 import org.simantics.db.Resource;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.request.Read;
7 import org.simantics.db.Session;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.service.QueryControl;
10
11 public class DistrictNetworkUIResource {
12     
13     public final Resource NetworkProperties;
14     public final Resource SelectionTab;
15     public final Resource SelectionTabContribution;
16         
17     public static class URIs {
18         public static final String NetworkProperties = "http://www.simantics.org/DistrictNetworkUI-1.0/NetworkProperties";
19         public static final String SelectionTab = "http://www.simantics.org/DistrictNetworkUI-1.0/SelectionTab";
20         public static final String SelectionTabContribution = "http://www.simantics.org/DistrictNetworkUI-1.0/SelectionTabContribution";
21     }
22     
23     public static Resource getResourceOrNull(ReadGraph graph, String uri) {
24         try {
25             return graph.getResource(uri);
26         } catch(DatabaseException e) {
27             System.err.println(e.getMessage());
28             return null;
29         }
30     }
31     
32     public DistrictNetworkUIResource(ReadGraph graph) {
33         NetworkProperties = getResourceOrNull(graph, URIs.NetworkProperties);
34         SelectionTab = getResourceOrNull(graph, URIs.SelectionTab);
35         SelectionTabContribution = getResourceOrNull(graph, URIs.SelectionTabContribution);
36     }
37     
38     public static DistrictNetworkUIResource getInstance(ReadGraph graph) {
39         Session session = graph.getSession();
40         DistrictNetworkUIResource ret = session.peekService(DistrictNetworkUIResource.class);
41         if(ret == null) {
42             QueryControl qc = graph.getService(QueryControl.class);
43             ret = new DistrictNetworkUIResource(qc.getIndependentGraph(graph));
44             session.registerService(DistrictNetworkUIResource.class, ret);
45         }
46         return ret;
47     }
48     
49     public static DistrictNetworkUIResource getInstance(RequestProcessor session) throws DatabaseException {
50         DistrictNetworkUIResource ret = session.peekService(DistrictNetworkUIResource.class);
51         if(ret == null) {
52             ret = session.syncRequest(new Read<DistrictNetworkUIResource>() {
53                 public DistrictNetworkUIResource perform(ReadGraph graph) throws DatabaseException {
54                     QueryControl qc = graph.getService(QueryControl.class);
55                     return new DistrictNetworkUIResource(qc.getIndependentGraph(graph));
56                 }
57             });
58             session.registerService(DistrictNetworkUIResource.class, ret);
59         }
60         return ret;
61     }
62     
63 }
64