1236f8a3d1ecdaa86a1bdd42d218e75c07618622
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / CellValue.java
1 package org.simantics.spreadsheet.graph;\r
2 \r
3 import org.simantics.databoard.Bindings;\r
4 import org.simantics.databoard.binding.Binding;\r
5 import org.simantics.databoard.binding.error.BindingConstructionException;\r
6 import org.simantics.databoard.binding.mutable.Variant;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.request.VariableRead;\r
10 import org.simantics.db.layer0.variable.Variable;\r
11 import org.simantics.spreadsheet.common.logging.Logger;\r
12 \r
13 public class CellValue extends VariableRead<Object> {\r
14 \r
15         public CellValue(Variable variable) {\r
16                 super(variable);\r
17         }\r
18 \r
19         @Override\r
20         public Object perform(ReadGraph graph) throws DatabaseException {\r
21                 \r
22                 Object value = variable.getValue(graph);\r
23                 String propertyName = variable.getName(graph);\r
24                 \r
25                 if("content".equals(propertyName)) {\r
26                         if(value == null) {\r
27                                 return Variant.ofInstance("Null content: " + variable.getURI(graph));\r
28                         }\r
29                         if(value instanceof Variable) {\r
30                                 Variable var = (Variable)value;\r
31                                 \r
32                                 Object value2 = var.getValue(graph);\r
33                                 if(value2 instanceof Variant) {\r
34                                 return value;\r
35                                 } else {\r
36                                         if(value2 == null) return Variant.ofInstance("Null value from " + var.getURI(graph));\r
37                                 try {\r
38                                         Binding binding = Bindings.getBinding( value2.getClass() );\r
39                                 return new Variant(binding, value2);\r
40                                 } catch (BindingConstructionException e) {\r
41                                         Logger.defaultLogError(e);\r
42                                 return Variant.ofInstance("Unsupported content " + value2 + " from " + var.getURI(graph));\r
43                                 }\r
44                                 }\r
45                         }\r
46                         if(!(value instanceof Variant)) {\r
47                                 try {\r
48                                         Binding binding = Bindings.getBinding( value.getClass() );\r
49                                 return new Variant(binding, value);\r
50                                 } catch (BindingConstructionException e) {\r
51                                         Logger.defaultLogError(e);\r
52                                         return Variant.ofInstance("Unsupported content " + value + " at " + variable.getURI(graph));\r
53                                 }\r
54                         }\r
55                 }\r
56 \r
57                 return value;\r
58                 \r
59         }\r
60         \r
61 }