1 package org.simantics.spreadsheet.graph.function;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\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
28 public class SpreadsheetRootVariable extends StandardGraphChildVariable {
\r
30 public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {
\r
31 super(parent, node, resource);
\r
35 public String getName(ReadGraph graph) throws DatabaseException {
\r
36 return ProxyChildVariable.CONTEXT_END;
\r
39 @SuppressWarnings("deprecation")
\r
41 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
\r
42 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
\r
46 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
\r
48 Variable var = super.getPossibleChild(graph, name);
\r
50 var = getPossibleRangeChild(graph, this, name);
\r
52 var = getPossibleDeepChild(graph, this, name);
\r
57 private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
\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
73 private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
\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
78 if(name.contains(":")) {
\r
80 Range range = SpreadsheetUtils.decodeRange(name, 0, 0);
\r
82 VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
\r
83 String rangeNames[][] = new String[range.height()][range.width()];
\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
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
98 Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };
\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
105 return new ConstantChildVariable(context, name, list);
\r