1 package org.simantics.spreadsheet.synchronization;
3 import java.io.StringReader;
4 import java.util.ArrayList;
6 import org.simantics.databoard.adapter.AdaptException;
7 import org.simantics.databoard.binding.mutable.Variant;
8 import org.simantics.spreadsheet.ExternalRef;
9 import org.simantics.spreadsheet.solver.SpreadsheetBook;
10 import org.simantics.spreadsheet.solver.SpreadsheetCell;
11 import org.simantics.spreadsheet.solver.SpreadsheetFormula;
12 import org.simantics.spreadsheet.solver.SpreadsheetLine;
13 import org.simantics.spreadsheet.solver.SpreadsheetLines;
14 import org.simantics.spreadsheet.solver.SpreadsheetSCLConstant;
15 import org.simantics.spreadsheet.solver.formula.parser.SheetFormulaParser;
16 import org.simantics.spreadsheet.solver.formula.parser.ast.AstArrayFormulaReference;
17 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
18 import org.simantics.structural.synchronization.base.CommandBuilder;
19 import org.simantics.structural.synchronization.utils.Solver;
21 public class LineCommandBuilder implements CommandBuilder {
26 public LineCommandBuilder(String name, boolean update) {
31 public void apply(Solver solver) {
33 SpreadsheetBook book = solver.getConcreteSolver();
35 String path = name.substring(0, name.lastIndexOf("/"));
36 String lineName = name.substring(name.lastIndexOf("/")+1);
37 int row = Integer.parseInt(lineName.substring(3));
39 SpreadsheetLines node = book.ensureSubprocess(path);
40 SpreadsheetLine line = node.lines.get(-row);
42 line = new SpreadsheetLine(node, row);
43 node.lines.put(-row, line);
46 ArrayList<SpreadsheetCell> changes = new ArrayList<>();
48 for(int i=0;i<bean.cells.length;i++) {
50 SpreadsheetCell currentCell;
51 if (line.cells.size() > i) {
52 currentCell = line.cells.get(i);
54 currentCell = new SpreadsheetCell(line, i);
55 line.cells.add(currentCell);
58 LineContentBeanCell cell = bean.cells[i];
61 Object content = cell.getContent();
62 if (content instanceof Variant) {
63 Variant cellVariant = (Variant) content;
64 if (cellVariant == LineContentBeanCell.EMPTY) {
65 currentCell.setStyle(cell.getStyleId());
67 currentCell.setContent("");
68 } else if(ExcelFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
69 ExcelFormula formula = (ExcelFormula)cellVariant.getValue(ExcelFormula.BINDING);
70 SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
71 AstValue v = p.relation();
72 currentCell.setStyle(cell.getStyleId());
73 SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
74 currentCell.setContent(sformula);
75 } else if (ExcelArrayFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
76 ExcelArrayFormula formula = (ExcelArrayFormula)cellVariant.getValue(ExcelArrayFormula.BINDING);
77 SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
78 AstArrayFormulaReference v = new AstArrayFormulaReference(formula.range, p.relation());
79 currentCell.setStyle(cell.getStyleId());
80 SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
81 currentCell.setContent(sformula);
82 } else if (cellVariant.getValue() instanceof ExternalRef){
83 throw new IllegalStateException();
85 currentCell.setStyle(cell.getStyleId());
86 // DO not update constant values during update
87 currentCell.setContent(cellVariant.getValue());
89 } else if (content instanceof ExternalRef){
90 throw new IllegalStateException();
91 } else if (content instanceof SpreadsheetSCLConstant){
92 currentCell.setStyle(cell.getStyleId());
93 currentCell.setContent(content);
95 } catch (Throwable e) {
96 Object content = cell.getContent();
97 if (content instanceof Variant) {
98 Variant cellVariant = (Variant) content;
99 currentCell.setStyle(cell.getStyleId());
100 currentCell.setContent(content);
102 new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();
103 } catch (AdaptException e1) {
104 e1.printStackTrace();
107 currentCell.setStyle(cell.getStyleId());
108 currentCell.setContent("LCB error happened");
112 changes.add(currentCell);
116 book.fireChanges(changes);
120 @SuppressWarnings("unchecked")
122 public <T> T getConcrete() {