]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java
Non-constant external value configuration via procedural UCs.
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / StandardProdeduralPropertyVariable.java
1 package org.simantics.structural2.variables;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.databoard.adapter.AdaptException;
5 import org.simantics.databoard.binding.Binding;
6 import org.simantics.databoard.type.Datatype;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable;
11 import org.simantics.db.layer0.variable.ValueAccessor;
12 import org.simantics.db.layer0.variable.Variable;
13
14 public class StandardProdeduralPropertyVariable extends StandardAssertedGraphPropertyVariable {
15
16         private Object defaultValue;
17
18         public StandardProdeduralPropertyVariable(ReadGraph graph,
19                         StandardProceduralChildVariable parent,
20                         StandardAssertedGraphPropertyVariable assertedVariable, Object value) throws DatabaseException {
21                 super(graph, parent, assertedVariable.node, assertedVariable.parentResource, assertedVariable.property.predicate, assertedVariable.object);
22                 
23                 this.defaultValue = value;
24         }
25         
26         @Override
27         protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
28                 final ValueAccessor valueAccessor = super.getValueAccessor(graph);
29                 return new ValueAccessor() {
30                         @Override
31                         public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
32                                 valueAccessor.setValue(graph, context, value, binding);
33                         }
34                         
35                         @Override
36                         public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
37                                 valueAccessor.setValue(graph, context, value);
38                         }
39                         
40                         @Override
41                         public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
42                                 if (node != null) {
43                                         return valueAccessor.getValue(graph, context, binding);
44                                 } else {
45                                         if (binding.isInstance(defaultValue))
46                                                 return defaultValue;
47                                         else
48                                                 try {
49                                                         return Bindings.adapt(defaultValue, Bindings.OBJECT, binding);
50                                                 } catch (AdaptException e) {
51                                                         throw new DatabaseException("Failed to adapt object " + defaultValue + " to " + binding);
52                                                 }
53                                 }
54                         }
55                         
56                         @Override
57                         public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
58                                 if (node != null) {
59                                         return valueAccessor.getValue(graph, context);
60                                 } else {
61                                         return defaultValue;
62                                 }
63                         }
64                         
65                         @Override
66                         public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
67                                 return valueAccessor.getDatatype(graph, context);
68                         }
69                 };
70         }
71 }