]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/function/SpreadsheetRootVariable.java
Move platform to only work with JDK's >= 11
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / function / SpreadsheetRootVariable.java
1 package org.simantics.spreadsheet.graph.function;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
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;
28
29 public class SpreadsheetRootVariable extends StandardGraphChildVariable {
30
31     public SpreadsheetRootVariable(Variable parent, VariableNode<?> node, Resource resource) {
32         super(parent, node, resource);
33     }
34     
35     @Override
36     public String getName(ReadGraph graph) throws DatabaseException {
37         return ProxyChildVariable.CONTEXT_END;
38     }
39     
40     @SuppressWarnings("deprecation")
41     @Override
42     public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
43         return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
44     }
45     
46     @Override
47     public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
48         
49         Variable var = super.getPossibleChild(graph, name);
50         if(var == null)
51             var = getPossibleRangeChild(graph, this, name);
52         if(var == null)
53             var = getPossibleDeepChild(graph, this, name);
54         return var;
55
56     }
57
58     private Variable getPossibleDeepChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
59         
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;
68                 }
69         }
70         return null;
71         
72     }
73
74     private Variable getPossibleRangeChild(ReadGraph graph, Variable context, String name) throws DatabaseException {
75         
76         final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.RANGE_CELL_NAMES, Variables.LABEL, "immutable" }; 
77         final Binding[] bindings = { Bindings.VARIANT, null, Bindings.STRING, Bindings.BOOLEAN};
78         
79         if(name.contains(":")) {
80                 
81             Range range = Spreadsheets.decodeRange(name, 0, 0);
82             
83             VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
84             String rangeNames[][] = new String[range.height()][range.width()];
85
86             
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);
91                     Variant value = null;
92                     if(child != null)
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;
96                 }
97             }
98             
99             Object[] values = new Object[] { Variant.ofInstance(matrix), rangeNames, null, name, Boolean.FALSE };
100             
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]));
104             }
105             
106             return new ConstantChildVariable(context, name, list);
107             
108         } else {
109             return null;
110         } 
111         
112     }
113     
114 }