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