-package org.simantics.spreadsheet.graph.adapter;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.ConstantChildVariable;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.VariableFactory;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
-import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
-\r
-public class StringArrayCells implements VariableFactory {\r
-\r
- private Resource configuration;\r
- \r
- public StringArrayCells(Resource configuration) {\r
- this.configuration = configuration;\r
- }\r
-\r
- final String[] propertyNames = { Variables.LABEL, "immutable" }; \r
- final Binding[] bindings = { Bindings.STRING, Bindings.BOOLEAN };\r
- \r
- private Collection<Variable> toVariables(ReadGraph graph, Variable variable, String[] data, int width) throws DatabaseException {\r
-\r
- SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
- String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
- if(location == null) return Collections.emptyList();\r
- \r
- int rows = data.length / width;\r
- \r
- ArrayList<Variable> result = new ArrayList<Variable>();\r
- for(int offset=0,i=0;i<rows;i++) {\r
- for(int j=0;j<width;j++) {\r
- String value = data[offset++];\r
- String valueLocation = SpreadsheetUtils.offset(location, i, j);\r
- result.add(new ConstantChildVariable(variable, valueLocation, propertyNames, bindings, new Object[] { value, true }));\r
- }\r
- }\r
- return result;\r
-\r
- }\r
- \r
- private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {\r
-\r
- SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
- String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
- if(location == null) return Collections.emptyList();\r
- \r
- return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message })); \r
-\r
- }\r
- \r
- @Override\r
- public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {\r
- \r
- SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
- String[] data = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasStringArray, Bindings.STRING_ARRAY);\r
- if(data == null) return error(graph, variable, "No string array data.");\r
- Integer width = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasWidth, Bindings.INTEGER);\r
- if(width == null) return error(graph, variable, "Invalid width for string array.");\r
- \r
- return toVariables(graph, variable, data, width);\r
- \r
- }\r
- \r
-}\r
+package org.simantics.spreadsheet.graph.adapter;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.ConstantChildVariable;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.VariableFactory;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.spreadsheet.resource.SpreadsheetResource;
+import org.simantics.spreadsheet.util.SpreadsheetUtils;
+
+public class StringArrayCells implements VariableFactory {
+
+ private Resource configuration;
+
+ public StringArrayCells(Resource configuration) {
+ this.configuration = configuration;
+ }
+
+ final String[] propertyNames = { Variables.LABEL, "immutable" };
+ final Binding[] bindings = { Bindings.STRING, Bindings.BOOLEAN };
+
+ private Collection<Variable> toVariables(ReadGraph graph, Variable variable, String[] data, int width) throws DatabaseException {
+
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
+ String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
+ if(location == null) return Collections.emptyList();
+
+ int rows = data.length / width;
+
+ ArrayList<Variable> result = new ArrayList<Variable>();
+ for(int offset=0,i=0;i<rows;i++) {
+ for(int j=0;j<width;j++) {
+ String value = data[offset++];
+ String valueLocation = SpreadsheetUtils.offset(location, i, j);
+ result.add(new ConstantChildVariable(variable, valueLocation, propertyNames, bindings, new Object[] { value, true }));
+ }
+ }
+ return result;
+
+ }
+
+ private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {
+
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
+ String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
+ if(location == null) return Collections.emptyList();
+
+ return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message }));
+
+ }
+
+ @Override
+ public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {
+
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
+ String[] data = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasStringArray, Bindings.STRING_ARRAY);
+ if(data == null) return error(graph, variable, "No string array data.");
+ Integer width = graph.getPossibleRelatedValue(configuration, sr.StringArrayCell_HasWidth, Bindings.INTEGER);
+ if(width == null) return error(graph, variable, "Invalid width for string array.");
+
+ return toVariables(graph, variable, data, width);
+
+ }
+
+}