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