]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/SpreadsheetVariable.java
Bumped master version to 1.38.0
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / SpreadsheetVariable.java
1 package org.simantics.spreadsheet.graph.adapter;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.databoard.binding.Binding;
5 import org.simantics.databoard.binding.mutable.Variant;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.ConstantChildVariable;
11 import org.simantics.db.layer0.variable.ExtendedGraphChildVariable;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.VariableSpaceManipulator;
14 import org.simantics.db.layer0.variable.Variables;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.spreadsheet.Range;
17 import org.simantics.spreadsheet.SheetVariables;
18 import org.simantics.spreadsheet.common.matrix.VariantMatrix;
19 import org.simantics.spreadsheet.util.SpreadsheetUtils;
20
21 public class SpreadsheetVariable extends ExtendedGraphChildVariable {
22
23 //    final private Collection<Pair<StringCellParser, GraphCellCreator>> creators = new ArrayList<Pair<StringCellParser, GraphCellCreator>>(); 
24         
25         public SpreadsheetVariable(Variable parent, Resource resource) throws DatabaseException {
26                 
27                 super(parent, resource);
28                 
29 //          creators.add(new Pair<StringCellParser, GraphCellCreator>(Parsers.MATRIX_PARSER, new MatrixCellCreator(resource)));
30 //          creators.add(new Pair<StringCellParser, GraphCellCreator>(Parsers.EXPRESSION_PARSER, new ExpressionCellCreator(resource)));
31 //          creators.add(new Pair<StringCellParser, GraphCellCreator>(Parsers.COMMAND_PARSER, new CommandCellCreator(resource)));
32 //          creators.add(new Pair<StringCellParser, GraphCellCreator>(Parsers.RESOURCE_ARRAY_PARSER, new ResourceArrayCellCreator(resource)));
33 //          creators.add(new Pair<StringCellParser, GraphCellCreator>(Parsers.TEXT_PARSER, new TextCellCreator(resource)));
34             
35         }
36         
37     @SuppressWarnings("unchecked")
38     protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
39                 if(VariableSpaceManipulator.class == clazz) {
40                         
41                         return (T)new VariableSpaceManipulator() {
42
43                                 @Override
44                                 public void apply(WriteGraph graph, Modification modification) throws DatabaseException {
45                                         
46                                         Layer0 L0 = Layer0.getInstance(graph);
47                                         for(String name : modification.removedChildren) {
48                                                 Variable child = getChild(graph, name);
49                                                 Resource represents = child.getRepresents(graph);
50                                                 graph.deny(represents, L0.PartOf);
51                                         }
52                                         
53                                         for(ChildCreationData data : modification.newChildren) {
54                                                 Resource container = getRepresents(graph);
55                                                 Resource cell = graph.newResource();
56                                                 Resource type = graph.getResource(data.type);
57                                                 graph.claim(cell, L0.InstanceOf, null, type);
58                                                 graph.addLiteral(cell, L0.HasName, L0.NameOf, L0.String, data.name, Bindings.STRING);
59                                                 for(PropertyCreationData p : data.properties) {
60                                                         Resource property = graph.getResource(p.name);
61                                                         graph.claimLiteral(cell, property, p.value.getValue());
62                                                 }
63                                                 graph.claim(cell, L0.PartOf, container);
64                                                 
65                                         }
66                                         
67                                 }
68                                 
69                         };
70                         
71                 }
72                 return null;
73         }
74
75         @Override
76         public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
77                 T t = tryAdapt(graph, clazz);
78                 return t != null ? t : super.adapt(graph, clazz);
79         }
80
81         @Override
82         public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
83                 T t = tryAdapt(graph, clazz);
84                 return t != null ? t : super.adaptPossible(graph, clazz);
85         }
86
87         final String[] propertyNames = { SheetVariables.CONTENT, Variables.LABEL, "immutable" }; 
88         final Binding[] bindings = { Bindings.VARIANT, Bindings.STRING, Bindings.BOOLEAN};
89         
90         @Override
91         public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
92                 if(name.contains(":")) {
93                         Range range = SpreadsheetUtils.decodeRange(name, 0, 0);
94                         
95                         VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
96                         
97                         for(int x=range.startColumn;x<=range.endColumn;x++) {
98                                 for(int y=range.startRow;y<=range.endRow;y++) {
99                                         String location = SpreadsheetUtils.cellName(y,x);
100                                         Variable child = getPossibleChild(graph, location);
101                                         Variant value = null;
102                                         if(child != null)
103                                             value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);
104                                         matrix.set(y-range.startRow, x-range.startColumn, value);
105                                 }
106                         }
107                         
108                         return new ConstantChildVariable(this, name, propertyNames, bindings, new Object[] {
109                                         Variant.ofInstance(matrix), null, name, false
110                         });
111                         
112                 } else {
113                         return super.getPossibleSpecialChild(graph, name);
114                 }
115         }
116         
117 }