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