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