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