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.Spreadsheets;
24 import org.simantics.spreadsheet.common.matrix.VariantMatrix;
25 import org.simantics.spreadsheet.graph.Ranges;
26 import org.simantics.spreadsheet.resource.SpreadsheetResource;
27 import org.simantics.spreadsheet.util.SpreadsheetUtils;
29 public class SpreadsheetRootVariable extends StandardGraphChildVariable {
31 public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {
32 super(parent, node, resource);
36 public String getName(ReadGraph graph) throws DatabaseException {
37 return ProxyChildVariable.CONTEXT_END;
40 @SuppressWarnings("deprecation")
42 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
43 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
47 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
49 Variable var = super.getPossibleChild(graph, name);
51 var = getPossibleRangeChild(graph, this, name);
53 var = getPossibleDeepChild(graph, this, name);
58 private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
60 SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
61 for(Variable range : graph.syncRequest(new Ranges(context), TransientCacheListener.<Collection<Variable>>instance())) {
62 String location = range.getPropertyValue(graph, SHEET.Range_location, Bindings.STRING);
63 Integer widthBound = range.getPropertyValue(graph, SHEET.Range_widthBound, Bindings.INTEGER);
64 Integer heightBound = range.getPropertyValue(graph, SHEET.Range_heightBound, Bindings.INTEGER);
65 if(SpreadsheetUtils.isInBounds(location, name, widthBound, heightBound)) {
66 Variable cell = range.getPossibleChild(graph, name);
67 if(cell != null) return cell;
74 private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
76 final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.RANGE_CELL_NAMES, Variables.LABEL, "immutable" };
77 final Binding[] bindings = { Bindings.VARIANT, null, Bindings.STRING, Bindings.BOOLEAN};
79 if(name.contains(":")) {
81 Range range = Spreadsheets.decodeRange(name, 0, 0);
83 VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
84 String rangeNames[][] = new String[range.height()][range.width()];
87 for(int x=range.startColumn;x<=range.endColumn;x++) {
88 for(int y=range.startRow;y<=range.endRow;y++) {
89 String location = Spreadsheets.cellName(y,x);
90 Variable child = context.getPossibleChild(graph, location);
93 value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);
94 matrix.set(y-range.startRow, x-range.startColumn, value);
95 rangeNames[y-range.startRow][x-range.startColumn] = location;
99 Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };
101 ArrayList<ConstantPropertyVariableBuilder> list = new ArrayList<ConstantPropertyVariableBuilder>();
102 for(int i = 0; i < propertyNames.length; i++) {
103 list.add(new ConstantPropertyVariableBuilder(propertyNames[i], values[i], bindings[i]));
106 return new ConstantChildVariable(context, name, list);