1 package org.simantics.spreadsheet.graph.adapter;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.ConstantChildVariable;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.db.layer0.variable.VariableFactory;
16 import org.simantics.db.layer0.variable.Variables;
17 import org.simantics.spreadsheet.SheetVariables;
18 import org.simantics.spreadsheet.Spreadsheets;
19 import org.simantics.spreadsheet.resource.SpreadsheetResource;
20 import org.simantics.utils.datastructures.datatype.RGBInt8;
22 public class DoubleArrayCells implements VariableFactory {
24 private Binding RGB = Bindings.getBindingUnchecked(RGBInt8.class);
25 // private Datatype RGB = Datatypes.getDatatypeUnchecked(RGBInt8.class);
27 private Resource configuration;
29 public DoubleArrayCells(Resource configuration) {
30 this.configuration = configuration;
33 final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.FOREGROUND, SheetVariables.BACKGROUND, Variables.LABEL, "immutable" };
34 final Binding[] bindings = { Bindings.VARIANT, RGB, RGB, Bindings.STRING, Bindings.BOOLEAN };
36 private Collection<Variable> toVariables(ReadGraph graph, Variable variable, double[] data, int width) throws DatabaseException {
38 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
39 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
40 if(location == null) return Collections.emptyList();
42 int rows = data.length / width;
44 RGBInt8 fore = new RGBInt8(255, 255, 255);
45 RGBInt8 back = new RGBInt8(80, 130, 190);
47 ArrayList<Variable> result = new ArrayList<Variable>();
48 for(int offset=0,i=0;i<rows;i++) {
49 for(int j=0;j<width;j++) {
50 double value = data[offset++];
51 String valueLocation = Spreadsheets.offset(location, i, j);
52 result.add(new DoubleArrayCellVariable(variable, valueLocation, propertyNames, bindings, new Object[] { Variant.ofInstance(value), fore, back, configuration, String.valueOf(value), false }));
59 private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {
61 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
62 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
63 if(location == null) return Collections.emptyList();
65 return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message }));
70 public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {
72 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
73 double[] data = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasDoubleArray, Bindings.DOUBLE_ARRAY);
74 if(data == null) return error(graph, variable, "No double array data.");
75 Integer width = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasWidth, Bindings.INTEGER);
76 if(width == null) return error(graph, variable, "Invalid width for double array.");
78 return toVariables(graph, variable, data, width);