]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/StringArrayCells.java
Bumped master version to 1.38.0
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / StringArrayCells.java
1 package org.simantics.spreadsheet.graph.adapter;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.ConstantChildVariable;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.db.layer0.variable.VariableFactory;
15 import org.simantics.db.layer0.variable.Variables;
16 import org.simantics.spreadsheet.resource.SpreadsheetResource;
17 import org.simantics.spreadsheet.util.SpreadsheetUtils;
18
19 public class StringArrayCells implements VariableFactory {
20
21         private Resource configuration;
22         
23         public StringArrayCells(Resource configuration) {
24                 this.configuration = configuration;
25         }
26
27         final String[] propertyNames = { Variables.LABEL, "immutable" }; 
28         final Binding[] bindings = { Bindings.STRING, Bindings.BOOLEAN };
29         
30         private Collection<Variable> toVariables(ReadGraph graph, Variable variable, String[] data, int width) throws DatabaseException {
31
32                 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
33                 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
34                 if(location == null) return Collections.emptyList();
35                 
36                 int rows = data.length / width;
37                 
38                 ArrayList<Variable> result = new ArrayList<Variable>();
39                 for(int offset=0,i=0;i<rows;i++) {
40                         for(int j=0;j<width;j++) {
41                                 String value = data[offset++];
42                                 String valueLocation = SpreadsheetUtils.offset(location, i, j);
43                                 result.add(new ConstantChildVariable(variable, valueLocation, propertyNames, bindings, new Object[] { value, true }));
44                         }
45                 }
46                 return result;
47
48         }
49         
50         private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {
51
52                 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
53                 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
54                 if(location == null) return Collections.emptyList();
55                 
56                 return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message })); 
57
58         }
59         
60         @Override
61         public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {
62                 
63                 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
64                 String[] data = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasStringArray, Bindings.STRING_ARRAY);
65                 if(data == null) return error(graph, variable, "No string array data.");
66                 Integer width = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasWidth, Bindings.INTEGER);
67                 if(width == null) return error(graph, variable, "Invalid width for string array.");
68                         
69                 return toVariables(graph, variable, data, width);
70                 
71         }
72         
73 }