]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4ResourceEditorInput2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4ResourceEditorInput2.java
1 package org.simantics.ui.workbench.e4;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.request.UniqueRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.variable.RVI;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.ui.workbench.IResourceEditorInput2;
12
13 /**
14  * @author Tuukka Lehtonen
15  * @since 1.22
16  */
17 public class E4ResourceEditorInput2 extends E4ResourceEditorInput implements IResourceEditorInput2 {
18
19     protected final Resource model;
20     protected final String rvi;
21     protected transient Variable variable;
22
23     public E4ResourceEditorInput2(Resource resource, Resource model, String rvi) {
24         super(resource);
25         this.model = model;
26         this.rvi = rvi;
27     }
28
29     @Override
30     public void dispose() {
31         // Nullify to prevent the class from keeping unnecessary references to
32         // possible Variable implementation backend resources.
33         variable = null;
34     }
35
36     @Override
37     public Resource getModel(ReadGraph graph) {
38         return model;
39     }
40
41     @Override
42     public String getRVI() {
43         return rvi;
44     }
45
46     @Override
47     public boolean exists(ReadGraph graph) throws DatabaseException {
48         if (!super.exists(graph))
49             return false;
50
51         if (model != null) {
52             boolean modelExists = graph.hasStatement(model);
53             if (!modelExists)
54                 return false;
55         }
56
57         if(rvi != null) {
58             Variable context = Variables.getPossibleConfigurationContext(graph, model);
59             if (context == null)
60                 return false;
61             RVI rvi_ = RVI.fromResourceFormat(graph, rvi);
62             Variable variable = rvi_.resolvePossible(graph, context);
63             if (variable == null)
64                 return false;
65         }
66
67         return true;
68     }
69
70     @Override
71     public Variable getVariable() throws DatabaseException {
72         if (variable != null)
73             return variable;
74         return Simantics.getSession().syncRequest(new UniqueRead<Variable>() {
75             @Override
76             public Variable perform(ReadGraph graph) throws DatabaseException {
77                 return getVariable(graph);
78             }
79         });
80     }
81
82     protected Variable getVariable0(ReadGraph graph) throws DatabaseException {
83         Resource model = getModel(graph);
84         String rvi = getRVI();
85         // Model + RVI
86         if (rvi != null) {
87             Variable configuration = Variables.getConfigurationContext(graph, model);
88             RVI rrvi = RVI.fromResourceFormat(graph, rvi);
89             return rrvi.resolve(graph, configuration);
90         }
91         // Absolute URI
92         else {
93             return Variables.getVariable(graph, model);
94         }
95     }
96
97     @Override
98     public Variable getVariable(ReadGraph graph) throws DatabaseException {
99         Variable v = getVariable0(graph);
100         this.variable = v;
101         return v;
102     }
103
104 }