]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCells.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCells.java
new file mode 100644 (file)
index 0000000..a6733ac
--- /dev/null
@@ -0,0 +1,83 @@
+package org.simantics.spreadsheet.graph.adapter;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.ConstantChildVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.VariableFactory;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.spreadsheet.SheetVariables;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+import org.simantics.utils.datastructures.datatype.RGBInt8;\r
+\r
+public class DoubleArrayCells implements VariableFactory {\r
+\r
+       private Binding RGB = Bindings.getBindingUnchecked(RGBInt8.class);\r
+//     private Datatype RGB = Datatypes.getDatatypeUnchecked(RGBInt8.class);\r
+       \r
+       private Resource configuration;\r
+       \r
+       public DoubleArrayCells(Resource configuration) {\r
+               this.configuration = configuration;\r
+       }\r
+\r
+       final String[] propertyNames = { SheetVariables.CONTENT, SheetVariables.FOREGROUND, SheetVariables.BACKGROUND, Variables.LABEL, "immutable" }; \r
+       final Binding[] bindings = { Bindings.VARIANT, RGB, RGB, Bindings.STRING, Bindings.BOOLEAN };\r
+       \r
+       private Collection<Variable> toVariables(ReadGraph graph, Variable variable, double[] data, int width) throws DatabaseException {\r
+\r
+               SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+               String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
+               if(location == null) return Collections.emptyList();\r
+               \r
+               int rows = data.length / width;\r
+               \r
+               RGBInt8 fore = new RGBInt8(255, 255, 255);\r
+               RGBInt8 back = new RGBInt8(80, 130, 190);\r
+               \r
+               ArrayList<Variable> result = new ArrayList<Variable>();\r
+               for(int offset=0,i=0;i<rows;i++) {\r
+                       for(int j=0;j<width;j++) {\r
+                               double value = data[offset++];\r
+                               String valueLocation = SpreadsheetUtils.offset(location, i, j);\r
+                               result.add(new DoubleArrayCellVariable(variable, valueLocation, propertyNames, bindings, new Object[] { Variant.ofInstance(value), fore, back, configuration, String.valueOf(value), false }));\r
+                       }\r
+               }\r
+               return result;\r
+\r
+       }\r
+       \r
+       private Collection<Variable> error(ReadGraph graph, Variable variable, String message) throws DatabaseException {\r
+\r
+               SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+               String location = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
+               if(location == null) return Collections.emptyList();\r
+               \r
+               return Collections.<Variable>singletonList(new ConstantChildVariable(variable, location, propertyNames, bindings, new Object[] { message })); \r
+\r
+       }\r
+       \r
+       @Override\r
+       public Collection<Variable> evaluate(ReadGraph graph, Variable variable) throws DatabaseException {\r
+               \r
+               SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+               double[] data = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasDoubleArray, Bindings.DOUBLE_ARRAY);\r
+               if(data == null) return error(graph, variable, "No double array data.");\r
+               Integer width = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasWidth, Bindings.INTEGER);\r
+               if(width == null) return error(graph, variable, "Invalid width for double array.");\r
+                       \r
+               return toVariables(graph, variable, data, width);\r
+               \r
+       }\r
+       \r
+}\r