1 package org.simantics.spreadsheet.graph.adapter;
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;
21 public class SpreadsheetVariable extends ExtendedGraphChildVariable {
23 // final private Collection<Pair<StringCellParser, GraphCellCreator>> creators = new ArrayList<Pair<StringCellParser, GraphCellCreator>>();
25 public SpreadsheetVariable(Variable parent, Resource resource) throws DatabaseException {
27 super(parent, resource);
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)));
37 @SuppressWarnings("unchecked")
38 protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
39 if(VariableSpaceManipulator.class == clazz) {
41 return (T)new VariableSpaceManipulator() {
44 public void apply(WriteGraph graph, Modification modification) throws DatabaseException {
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);
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());
63 graph.claim(cell, L0.PartOf, container);
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);
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);
87 final String[] propertyNames = { SheetVariables.CONTENT, Variables.LABEL, "immutable" };
88 final Binding[] bindings = { Bindings.VARIANT, Bindings.STRING, Bindings.BOOLEAN};
91 public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
92 if(name.contains(":")) {
93 Range range = SpreadsheetUtils.decodeRange(name, 0, 0);
95 VariantMatrix matrix = new VariantMatrix(range.height(),range.width());
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;
103 value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT);
104 matrix.set(y-range.startRow, x-range.startColumn, value);
108 return new ConstantChildVariable(this, name, propertyNames, bindings, new Object[] {
109 Variant.ofInstance(matrix), null, name, false
113 return super.getPossibleSpecialChild(graph, name);