1 package org.simantics.spreadsheet.graph.adapter;
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.common.cell.VariableCellEditor;
14 import org.simantics.spreadsheet.resource.SpreadsheetResource;
15 import org.simantics.spreadsheet.util.SpreadsheetUtils;
17 public class DoubleArrayCellVariable extends ConstantChildVariable {
19 public DoubleArrayCellVariable(Variable parent, String name, String[] properties, Binding[] bindings, Object ... values) {
20 super(parent, name, properties, bindings, values);
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() {
29 public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {
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);
41 Range baseRange = SpreadsheetUtils.decodeCellAbsolute(baseLocation);
42 Range editRange = SpreadsheetUtils.decodeCellAbsolute(editLocation);
44 int x = editRange.startColumn - baseRange.startColumn;
45 int y = editRange.startRow - baseRange.startRow;
47 int height = data.length / width;
49 if(x>=0 && x<width && y>=0 && y<height) {
50 data[width*y+x] = Double.parseDouble(text);
53 graph.claimLiteral(configuration, sr.DoubleArrayCell_HasDoubleArray, data, Bindings.DOUBLE_ARRAY);
58 public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {
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);
73 public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
74 return tryAdapt(graph, clazz);