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.Datatypes;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.ConstantChildVariable;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.db.layer0.variable.VariableFactory;
17 import org.simantics.db.layer0.variable.Variables;
18 import org.simantics.spreadsheet.SheetVariables;
19 import org.simantics.spreadsheet.resource.SpreadsheetResource;
20 import org.simantics.spreadsheet.util.SpreadsheetUtils;
21 import org.simantics.utils.datastructures.datatype.RGBInt8;
23 public class DoubleArrayCells implements VariableFactory {
25 private Binding RGB = Bindings.getBindingUnchecked(RGBInt8.class);
26 // private Datatype RGB = Datatypes.getDatatypeUnchecked(RGBInt8.class);
28 private Resource configuration;
30 public DoubleArrayCells(Resource configuration) {
31 this.configuration = configuration;
34 final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.FOREGROUND, SheetVariables.BACKGROUND, Variables.LABEL, "immutable" };
35 final Binding[] bindings = { Bindings.VARIANT, RGB, RGB, Bindings.STRING, Bindings.BOOLEAN };
37 private Collection<Variable> toVariables(ReadGraph graph, Variable variable, double[] data, int width) throws DatabaseException {
39 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
40 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
41 if(location == null) return Collections.emptyList();
43 int rows = data.length / width;
45 RGBInt8 fore = new RGBInt8(255, 255, 255);
46 RGBInt8 back = new RGBInt8(80, 130, 190);
48 ArrayList<Variable> result = new ArrayList<Variable>();
49 for(int offset=0,i=0;i<rows;i++) {
50 for(int j=0;j<width;j++) {
51 double value = data[offset++];
52 String valueLocation = SpreadsheetUtils.offset(location, i, j);
53 result.add(new DoubleArrayCellVariable(variable, valueLocation, propertyNames, bindings, new Object[] { Variant.ofInstance(value), fore, back, configuration, String.valueOf(value), false }));
60 private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {
62 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
63 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
64 if(location == null) return Collections.emptyList();
66 return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message }));
71 public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {
73 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
74 double[] data = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasDoubleArray, Bindings.DOUBLE_ARRAY);
75 if(data == null) return error(graph, variable, "No double array data.");
76 Integer width = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasWidth, Bindings.INTEGER);
77 if(width == null) return error(graph, variable, "Invalid width for double array.");
79 return toVariables(graph, variable, data, width);