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