+package org.simantics.ui.workbench.e4;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.RVI;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.ui.workbench.IResourceEditorInput2;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.22\r
+ */\r
+public class E4ResourceEditorInput2 extends E4ResourceEditorInput implements IResourceEditorInput2 {\r
+\r
+ protected final Resource model;\r
+ protected final String rvi;\r
+ protected transient Variable variable;\r
+\r
+ public E4ResourceEditorInput2(Resource resource, Resource model, String rvi) {\r
+ super(resource);\r
+ this.model = model;\r
+ this.rvi = rvi;\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+ // Nullify to prevent the class from keeping unnecessary references to\r
+ // possible Variable implementation backend resources.\r
+ variable = null;\r
+ }\r
+\r
+ @Override\r
+ public Resource getModel(ReadGraph graph) {\r
+ return model;\r
+ }\r
+\r
+ @Override\r
+ public String getRVI() {\r
+ return rvi;\r
+ }\r
+\r
+ @Override\r
+ public boolean exists(ReadGraph graph) throws DatabaseException {\r
+ if (!super.exists(graph))\r
+ return false;\r
+\r
+ if (model != null) {\r
+ boolean modelExists = graph.hasStatement(model);\r
+ if (!modelExists)\r
+ return false;\r
+ }\r
+\r
+ if(rvi != null) {\r
+ Variable context = Variables.getPossibleConfigurationContext(graph, model);\r
+ if (context == null)\r
+ return false;\r
+ RVI rvi_ = RVI.fromResourceFormat(graph, rvi);\r
+ Variable variable = rvi_.resolvePossible(graph, context);\r
+ if (variable == null)\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public Variable getVariable() throws DatabaseException {\r
+ if (variable != null)\r
+ return variable;\r
+ return Simantics.getSession().syncRequest(new UniqueRead<Variable>() {\r
+ @Override\r
+ public Variable perform(ReadGraph graph) throws DatabaseException {\r
+ return getVariable(graph);\r
+ }\r
+ });\r
+ }\r
+\r
+ protected Variable getVariable0(ReadGraph graph) throws DatabaseException {\r
+ Resource model = getModel(graph);\r
+ String rvi = getRVI();\r
+ // Model + RVI\r
+ if (rvi != null) {\r
+ Variable configuration = Variables.getConfigurationContext(graph, model);\r
+ RVI rrvi = RVI.fromResourceFormat(graph, rvi);\r
+ return rrvi.resolve(graph, configuration);\r
+ }\r
+ // Absolute URI\r
+ else {\r
+ return Variables.getVariable(graph, model);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Variable getVariable(ReadGraph graph) throws DatabaseException {\r
+ Variable v = getVariable0(graph);\r
+ this.variable = v;\r
+ return v;\r
+ }\r
+\r
+}\r