]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/function/SpreadsheetRootVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / function / SpreadsheetRootVariable.java
1 package org.simantics.spreadsheet.graph.function;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.databoard.Bindings;\r
7 import org.simantics.databoard.binding.Binding;\r
8 import org.simantics.databoard.binding.mutable.Variant;\r
9 import org.simantics.db.ReadGraph;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.db.layer0.variable.ConstantChildVariable;\r
14 import org.simantics.db.layer0.variable.ConstantPropertyVariable;\r
15 import org.simantics.db.layer0.variable.ConstantPropertyVariableBuilder;\r
16 import org.simantics.db.layer0.variable.ProxyChildVariable;\r
17 import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
18 import org.simantics.db.layer0.variable.Variable;\r
19 import org.simantics.db.layer0.variable.VariableNode;\r
20 import org.simantics.db.layer0.variable.Variables;\r
21 import org.simantics.spreadsheet.Range;\r
22 import org.simantics.spreadsheet.SheetVariables;\r
23 import org.simantics.spreadsheet.common.matrix.VariantMatrix;\r
24 import org.simantics.spreadsheet.graph.Ranges;\r
25 import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
26 import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
27 \r
28 public class SpreadsheetRootVariable extends StandardGraphChildVariable {\r
29 \r
30     public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {\r
31         super(parent, node, resource);\r
32     }\r
33     \r
34     @Override\r
35     public String getName(ReadGraph graph) throws DatabaseException {\r
36         return ProxyChildVariable.CONTEXT_END;\r
37     }\r
38     \r
39     @SuppressWarnings("deprecation")\r
40     @Override\r
41     public Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
42         return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);\r
43     }\r
44     \r
45     @Override\r
46     public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
47         \r
48         Variable var = super.getPossibleChild(graph, name);\r
49         if(var == null)\r
50             var = getPossibleRangeChild(graph, this, name);\r
51         if(var == null)\r
52             var = getPossibleDeepChild(graph, this, name);\r
53         return var;\r
54 \r
55     }\r
56 \r
57     private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {\r
58         \r
59         SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);\r
60         for(Variable range : graph.syncRequest(new Ranges(context), TransientCacheListener.<Collection<Variable>>instance())) {\r
61                 String location = range.getPropertyValue(graph, SHEET.Range_location, Bindings.STRING);\r
62                 Integer widthBound = range.getPropertyValue(graph, SHEET.Range_widthBound, Bindings.INTEGER);\r
63                 Integer heightBound = range.getPropertyValue(graph, SHEET.Range_heightBound, Bindings.INTEGER);\r
64                 if(SpreadsheetUtils.isInBounds(location, name, widthBound, heightBound)) {\r
65                         Variable cell = range.getPossibleChild(graph, name);\r
66                         if(cell != null) return cell;\r
67                 }\r
68         }\r
69         return null;\r
70         \r
71     }\r
72 \r
73     private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {\r
74         \r
75         final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.RANGE_CELL_NAMES, Variables.LABEL, "immutable" }; \r
76         final Binding[] bindings = { Bindings.VARIANT, null, Bindings.STRING, Bindings.BOOLEAN};\r
77         \r
78         if(name.contains(":")) {\r
79                 \r
80             Range range = SpreadsheetUtils.decodeRange(name, 0, 0);\r
81             \r
82             VariantMatrix matrix = new VariantMatrix(range.height(),range.width());\r
83             String rangeNames[][] = new String[range.height()][range.width()];\r
84 \r
85             \r
86             for(int x=range.startColumn;x<=range.endColumn;x++) {\r
87                 for(int y=range.startRow;y<=range.endRow;y++) {\r
88                     String location = SpreadsheetUtils.cellName(y,x);\r
89                     Variable child = context.getPossibleChild(graph, location);\r
90                     Variant value = null;\r
91                     if(child != null)\r
92                         value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);\r
93                     matrix.set(y-range.startRow, x-range.startColumn, value);\r
94                     rangeNames[y-range.startRow][x-range.startColumn] = location;\r
95                 }\r
96             }\r
97             \r
98             Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };\r
99             \r
100             ArrayList<ConstantPropertyVariableBuilder> list = new ArrayList<ConstantPropertyVariableBuilder>();\r
101             for(int i = 0; i < propertyNames.length; i++) {\r
102                 list.add(new ConstantPropertyVariableBuilder(propertyNames[i], values[i], bindings[i]));\r
103             }\r
104             \r
105             return new ConstantChildVariable(context, name, list);\r
106             \r
107         } else {\r
108             return null;\r
109         } \r
110         \r
111     }\r
112     \r
113 }