]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCellVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / DoubleArrayCellVariable.java
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCellVariable.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/DoubleArrayCellVariable.java
new file mode 100644 (file)
index 0000000..d2aa76f
--- /dev/null
@@ -0,0 +1,77 @@
+package org.simantics.spreadsheet.graph.adapter;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\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.Variables;\r
+import org.simantics.spreadsheet.Range;\r
+import org.simantics.spreadsheet.common.cell.VariableCellEditor;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+\r
+public class DoubleArrayCellVariable extends ConstantChildVariable {\r
+\r
+    public DoubleArrayCellVariable(Variable parent, String name, String[] properties, Binding[] bindings, Object ... values) {\r
+       super(parent, name, properties, bindings, values);\r
+    }\r
+    \r
+    @SuppressWarnings("unchecked")\r
+    protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+       if(VariableCellEditor.class == clazz) {\r
+               return (T)new VariableCellEditor() {\r
+                               \r
+                               @Override\r
+                               public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {\r
+                                       \r
+                                       SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+                                       Resource configuration = cell.getPossibleRepresents(graph);\r
+                                       double[] data = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasDoubleArray, Bindings.DOUBLE_ARRAY);\r
+                                       if(data == null) return;\r
+                                       Integer width = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasWidth, Bindings.INTEGER);\r
+                                       if(width == null) return;\r
+                                       String baseLocation = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
+                                       if(baseLocation == null) return;\r
+                                       String editLocation = cell.getPossiblePropertyValue(graph, Variables.NAME);\r
+                                       \r
+                                       Range baseRange = SpreadsheetUtils.decodeCellAbsolute(baseLocation);\r
+                                       Range editRange = SpreadsheetUtils.decodeCellAbsolute(editLocation);\r
+                                       \r
+                                       int x = editRange.startColumn - baseRange.startColumn;\r
+                                       int y = editRange.startRow - baseRange.startRow;\r
+                                       \r
+                                       int height = data.length / width;\r
+                                       \r
+                                       if(x>=0 && x<width && y>=0 && y<height) {\r
+                                               data[width*y+x] = Double.parseDouble(text);\r
+                                       }\r
+                                       \r
+                                       graph.claimLiteral(configuration, sr.DoubleArrayCell_HasDoubleArray, data, Bindings.DOUBLE_ARRAY);\r
+                                       \r
+                               }\r
+                               \r
+                               @Override\r
+                               public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {\r
+                               }\r
+                               \r
+                       };\r
+       }\r
+       return null;\r
+    }\r
+\r
+    @Override\r
+    public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+        T t = tryAdapt(graph, clazz);\r
+        return t != null ? t : super.adapt(graph, clazz);\r
+    }\r
+\r
+    @Override\r
+    public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+        return tryAdapt(graph, clazz);\r
+    }\r
+\r
+}\r