]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGSourceRepresentation.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGSourceRepresentation.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.Arrays;
4 import java.util.Map;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.RequestProcessor;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.UniqueRead;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
12 import org.simantics.graph.db.TransferableGraphSource;
13 import org.simantics.utils.datastructures.hints.IHintContext.Key;
14
15 public class TGSourceRepresentation implements Representation {
16
17         protected Resource[] resources;
18         protected boolean ignoreVirtualResources = false;
19         private TransferableGraphSource value = null;
20         private TransferableGraphConfiguration2 configuration;
21         
22         public TGSourceRepresentation(Resource ... resources) {
23                 this.resources = resources;
24         }
25
26         public TGSourceRepresentation(boolean ignoreVirtualResources, Resource ... resources) {
27                 this.ignoreVirtualResources = ignoreVirtualResources;
28                 this.resources = resources;
29         }
30
31         public TGSourceRepresentation(TransferableGraphConfiguration2 configuration) {
32                 this.configuration = configuration;
33         }
34
35         public TGSourceRepresentation(TransferableGraphSource value) {
36                 this.value = value;
37         }
38
39         @Override
40         public Key getKey() {
41                 return SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE;
42         }
43
44         public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
45                 
46                 if(configuration == null) {
47                         configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);
48                         configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
49                 }
50                 
51                 return graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration));
52                 
53         }
54
55         @SuppressWarnings("unchecked")
56         @Override
57         public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {
58                 
59                 if(value == null) {
60                         value = processor.syncRequest(new UniqueRead<TransferableGraphSource>() {
61
62                                 @Override
63                                 public TransferableGraphSource perform(ReadGraph graph) throws DatabaseException {
64                                         return compute(graph, hints);
65                                 }
66
67                         });
68                 }
69                 
70                 return (T)value;
71                 
72         }
73         
74 }