]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.spreadsheet.graph.adapter;\r
2 \r
3 import org.simantics.databoard.Bindings;\r
4 import org.simantics.databoard.binding.Binding;\r
5 import org.simantics.db.ReadGraph;\r
6 import org.simantics.db.Resource;\r
7 import org.simantics.db.WriteGraph;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.variable.ConstantChildVariable;\r
10 import org.simantics.db.layer0.variable.Variable;\r
11 import org.simantics.db.layer0.variable.Variables;\r
12 import org.simantics.spreadsheet.Range;\r
13 import org.simantics.spreadsheet.common.cell.VariableCellEditor;\r
14 import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
15 import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
16 \r
17 public class DoubleArrayCellVariable extends ConstantChildVariable {\r
18 \r
19     public DoubleArrayCellVariable(Variable parent, String name, String[] properties, Binding[] bindings, Object ... values) {\r
20         super(parent, name, properties, bindings, values);\r
21     }\r
22     \r
23     @SuppressWarnings("unchecked")\r
24     protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
25         if(VariableCellEditor.class == clazz) {\r
26                 return (T)new VariableCellEditor() {\r
27                                 \r
28                                 @Override\r
29                                 public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {\r
30                                         \r
31                                         SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
32                                         Resource configuration = cell.getPossibleRepresents(graph);\r
33                                         double[] data = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasDoubleArray, Bindings.DOUBLE_ARRAY);\r
34                                         if(data == null) return;\r
35                                         Integer width = graph.getPossibleRelatedValue(configuration, sr.DoubleArrayCell_HasWidth, Bindings.INTEGER);\r
36                                         if(width == null) return;\r
37                                         String baseLocation = graph.getPossibleRelatedValue(configuration, sr.HasLocation, Bindings.STRING);\r
38                                         if(baseLocation == null) return;\r
39                                         String editLocation = cell.getPossiblePropertyValue(graph, Variables.NAME);\r
40                                         \r
41                                         Range baseRange = SpreadsheetUtils.decodeCellAbsolute(baseLocation);\r
42                                         Range editRange = SpreadsheetUtils.decodeCellAbsolute(editLocation);\r
43                                         \r
44                                         int x = editRange.startColumn - baseRange.startColumn;\r
45                                         int y = editRange.startRow - baseRange.startRow;\r
46                                         \r
47                                         int height = data.length / width;\r
48                                         \r
49                                         if(x>=0 && x<width && y>=0 && y<height) {\r
50                                                 data[width*y+x] = Double.parseDouble(text);\r
51                                         }\r
52                                         \r
53                                         graph.claimLiteral(configuration, sr.DoubleArrayCell_HasDoubleArray, data, Bindings.DOUBLE_ARRAY);\r
54                                         \r
55                                 }\r
56                                 \r
57                                 @Override\r
58                                 public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {\r
59                                 }\r
60                                 \r
61                         };\r
62         }\r
63         return null;\r
64     }\r
65 \r
66     @Override\r
67     public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
68         T t = tryAdapt(graph, clazz);\r
69         return t != null ? t : super.adapt(graph, clazz);\r
70     }\r
71 \r
72     @Override\r
73     public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
74         return tryAdapt(graph, clazz);\r
75     }\r
76 \r
77 }\r