]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCells.java
896fd6602cdaa39ff7721639b9a39d7765555d29
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / DoubleArrayCells.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.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;
22
23 public class DoubleArrayCells implements VariableFactory {
24
25         private Binding RGB = Bindings.getBindingUnchecked(RGBInt8.class);
26 //      private Datatype RGB = Datatypes.getDatatypeUnchecked(RGBInt8.class);
27         
28         private Resource configuration;
29         
30         public DoubleArrayCells(Resource configuration) {
31                 this.configuration = configuration;
32         }
33
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 };
36         
37         private Collection<Variable> toVariables(ReadGraph graph, Variable variable, double[] data, int width) throws DatabaseException {
38
39                 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
40                 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
41                 if(location == null) return Collections.emptyList();
42                 
43                 int rows = data.length / width;
44                 
45                 RGBInt8 fore = new RGBInt8(255, 255, 255);
46                 RGBInt8 back = new RGBInt8(80, 130, 190);
47                 
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 }));
54                         }
55                 }
56                 return result;
57
58         }
59         
60         private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {
61
62                 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
63                 String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);
64                 if(location == null) return Collections.emptyList();
65                 
66                 return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message })); 
67
68         }
69         
70         @Override
71         public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {
72                 
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.");
78                         
79                 return toVariables(graph, variable, data, width);
80                 
81         }
82         
83 }