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