]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCells.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / DoubleArrayCells.java
1 package org.simantics.spreadsheet.graph.adapter;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 \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
22 \r
23 public class DoubleArrayCells implements VariableFactory {\r
24 \r
25         private Binding RGB = Bindings.getBindingUnchecked(RGBInt8.class);\r
26 //      private Datatype RGB = Datatypes.getDatatypeUnchecked(RGBInt8.class);\r
27         \r
28         private Resource configuration;\r
29         \r
30         public DoubleArrayCells(Resource configuration) {\r
31                 this.configuration = configuration;\r
32         }\r
33 \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
36         \r
37         private Collection<Variable> toVariables(ReadGraph graph, Variable variable, double[] data, int width) throws DatabaseException {\r
38 \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
42                 \r
43                 int rows = data.length / width;\r
44                 \r
45                 RGBInt8 fore = new RGBInt8(255, 255, 255);\r
46                 RGBInt8 back = new RGBInt8(80, 130, 190);\r
47                 \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
54                         }\r
55                 }\r
56                 return result;\r
57 \r
58         }\r
59         \r
60         private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {\r
61 \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
65                 \r
66                 return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message })); \r
67 \r
68         }\r
69         \r
70         @Override\r
71         public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {\r
72                 \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
78                         \r
79                 return toVariables(graph, variable, data, width);\r
80                 \r
81         }\r
82         \r
83 }\r