1 package org.simantics.spreadsheet.graph.function;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.databoard.binding.mutable.Variant;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.ConstantChildVariable;
14 import org.simantics.db.layer0.variable.ConstantPropertyVariable;
15 import org.simantics.db.layer0.variable.ConstantPropertyVariableBuilder;
16 import org.simantics.db.layer0.variable.ProxyChildVariable;
17 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.VariableNode;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.spreadsheet.Range;
22 import org.simantics.spreadsheet.SheetVariables;
23 import org.simantics.spreadsheet.common.matrix.VariantMatrix;
24 import org.simantics.spreadsheet.graph.Ranges;
25 import org.simantics.spreadsheet.resource.SpreadsheetResource;
26 import org.simantics.spreadsheet.util.SpreadsheetUtils;
28 public class SpreadsheetRootVariable extends StandardGraphChildVariable {
30 public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {
31 super(parent, node, resource);
35 public String getName(ReadGraph graph) throws DatabaseException {
36 return ProxyChildVariable.CONTEXT_END;
39 @SuppressWarnings("deprecation")
41 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
42 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
46 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
48 Variable var = super.getPossibleChild(graph, name);
50 var = getPossibleRangeChild(graph, this, name);
52 var = getPossibleDeepChild(graph, this, name);
57 private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
59 SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
60 for(Variable range : graph.syncRequest(new Ranges(context), TransientCacheListener.<Collection<Variable>>instance())) {
61 String location = range.getPropertyValue(graph, SHEET.Range_location, Bindings.STRING);
62 Integer widthBound = range.getPropertyValue(graph, SHEET.Range_widthBound, Bindings.INTEGER);
63 Integer heightBound = range.getPropertyValue(graph, SHEET.Range_heightBound, Bindings.INTEGER);
64 if(SpreadsheetUtils.isInBounds(location, name, widthBound, heightBound)) {
65 Variable cell = range.getPossibleChild(graph, name);
66 if(cell != null) return cell;
73 private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
75 final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.RANGE_CELL_NAMES, Variables.LABEL, "immutable" };
76 final Binding[] bindings = { Bindings.VARIANT, null, Bindings.STRING, Bindings.BOOLEAN};
78 if(name.contains(":")) {
80 Range range = SpreadsheetUtils.decodeRange(name, 0, 0);
82 VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
83 String rangeNames[][] = new String[range.height()][range.width()];
86 for(int x=range.startColumn;x<=range.endColumn;x++) {
87 for(int y=range.startRow;y<=range.endRow;y++) {
88 String location = SpreadsheetUtils.cellName(y,x);
89 Variable child = context.getPossibleChild(graph, location);
92 value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);
93 matrix.set(y-range.startRow, x-range.startColumn, value);
94 rangeNames[y-range.startRow][x-range.startColumn] = location;
98 Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };
100 ArrayList<ConstantPropertyVariableBuilder> list = new ArrayList<ConstantPropertyVariableBuilder>();
101 for(int i = 0; i < propertyNames.length; i++) {
102 list.add(new ConstantPropertyVariableBuilder(propertyNames[i], values[i], bindings[i]));
105 return new ConstantChildVariable(context, name, list);