--- /dev/null
+package org.simantics.spreadsheet.graph.function;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.ConstantChildVariable;\r
+import org.simantics.db.layer0.variable.ConstantPropertyVariable;\r
+import org.simantics.db.layer0.variable.ConstantPropertyVariableBuilder;\r
+import org.simantics.db.layer0.variable.ProxyChildVariable;\r
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.VariableNode;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.spreadsheet.Range;\r
+import org.simantics.spreadsheet.SheetVariables;\r
+import org.simantics.spreadsheet.common.matrix.VariantMatrix;\r
+import org.simantics.spreadsheet.graph.Ranges;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+\r
+public class SpreadsheetRootVariable extends StandardGraphChildVariable {\r
+\r
+ public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {\r
+ super(parent, node, resource);\r
+ }\r
+ \r
+ @Override\r
+ public String getName(ReadGraph graph) throws DatabaseException {\r
+ return ProxyChildVariable.CONTEXT_END;\r
+ }\r
+ \r
+ @SuppressWarnings("deprecation")\r
+ @Override\r
+ public Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
+ return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);\r
+ }\r
+ \r
+ @Override\r
+ public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
+ \r
+ Variable var = super.getPossibleChild(graph, name);\r
+ if(var == null)\r
+ var = getPossibleRangeChild(graph, this, name);\r
+ if(var == null)\r
+ var = getPossibleDeepChild(graph, this, name);\r
+ return var;\r
+\r
+ }\r
+\r
+ private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {\r
+ \r
+ SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);\r
+ for(Variable range : graph.syncRequest(new Ranges(context), TransientCacheListener.<Collection<Variable>>instance())) {\r
+ String location = range.getPropertyValue(graph, SHEET.Range_location, Bindings.STRING);\r
+ Integer widthBound = range.getPropertyValue(graph, SHEET.Range_widthBound, Bindings.INTEGER);\r
+ Integer heightBound = range.getPropertyValue(graph, SHEET.Range_heightBound, Bindings.INTEGER);\r
+ if(SpreadsheetUtils.isInBounds(location, name, widthBound, heightBound)) {\r
+ Variable cell = range.getPossibleChild(graph, name);\r
+ if(cell != null) return cell;\r
+ }\r
+ }\r
+ return null;\r
+ \r
+ }\r
+\r
+ private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {\r
+ \r
+ final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.RANGE_CELL_NAMES, Variables.LABEL, "immutable" }; \r
+ final Binding[] bindings = { Bindings.VARIANT, null, Bindings.STRING, Bindings.BOOLEAN};\r
+ \r
+ if(name.contains(":")) {\r
+ \r
+ Range range = SpreadsheetUtils.decodeRange(name, 0, 0);\r
+ \r
+ VariantMatrix matrix = new VariantMatrix(range.height(),range.width());\r
+ String rangeNames[][] = new String[range.height()][range.width()];\r
+\r
+ \r
+ for(int x=range.startColumn;x<=range.endColumn;x++) {\r
+ for(int y=range.startRow;y<=range.endRow;y++) {\r
+ String location = SpreadsheetUtils.cellName(y,x);\r
+ Variable child = context.getPossibleChild(graph, location);\r
+ Variant value = null;\r
+ if(child != null)\r
+ value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);\r
+ matrix.set(y-range.startRow, x-range.startColumn, value);\r
+ rangeNames[y-range.startRow][x-range.startColumn] = location;\r
+ }\r
+ }\r
+ \r
+ Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };\r
+ \r
+ ArrayList<ConstantPropertyVariableBuilder> list = new ArrayList<ConstantPropertyVariableBuilder>();\r
+ for(int i = 0; i < propertyNames.length; i++) {\r
+ list.add(new ConstantPropertyVariableBuilder(propertyNames[i], values[i], bindings[i]));\r
+ }\r
+ \r
+ return new ConstantChildVariable(context, name, list);\r
+ \r
+ } else {\r
+ return null;\r
+ } \r
+ \r
+ }\r
+ \r
+}
\ No newline at end of file