1 package org.simantics.spreadsheet.graph.synchronization;
3 import java.io.StringReader;
5 import org.simantics.databoard.adapter.AdaptException;
6 import org.simantics.databoard.binding.mutable.Variant;
7 import org.simantics.spreadsheet.graph.ExcelArrayFormula;
8 import org.simantics.spreadsheet.graph.ExcelFormula;
9 import org.simantics.spreadsheet.graph.SpreadsheetBook;
10 import org.simantics.spreadsheet.graph.SpreadsheetCell;
11 import org.simantics.spreadsheet.graph.SpreadsheetFormula;
12 import org.simantics.spreadsheet.graph.SpreadsheetLine;
13 import org.simantics.spreadsheet.graph.SpreadsheetLines;
14 import org.simantics.spreadsheet.graph.SpreadsheetSCLConstant;
15 import org.simantics.spreadsheet.graph.function.LineContentBean;
16 import org.simantics.spreadsheet.graph.function.LineContentBeanCell;
17 import org.simantics.spreadsheet.graph.parser.SheetFormulaParser;
18 import org.simantics.spreadsheet.graph.parser.ast.AstArrayFormulaReference;
19 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
20 import org.simantics.structural.synchronization.base.CommandBuilder;
21 import org.simantics.structural.synchronization.base.Solver;
23 public class LineCommandBuilder implements CommandBuilder {
26 // private boolean update;
29 public LineCommandBuilder(String name, boolean update) {
31 // this.update = update;
35 public void apply(Solver solver) {
37 SpreadsheetBook book = solver.getConcreteSolver();
39 String path = name.substring(0, name.lastIndexOf("/"));
40 String lineName = name.substring(name.lastIndexOf("/")+1);
41 int row = Integer.parseInt(lineName.substring(3));
43 SpreadsheetLines node = book.ensureSubprocess(path);
44 SpreadsheetLine line = node.lines.get(-row);
46 line = new SpreadsheetLine(node, row);
47 node.lines.put(-row, line);
50 // line.cells.clear();
51 for(int i=0;i<bean.cells.length;i++) {
53 SpreadsheetCell currentCell;
54 if (line.cells.size() > i) {
55 currentCell = line.cells.get(i);
57 currentCell = new SpreadsheetCell(line, i);
58 line.cells.add(currentCell);
61 LineContentBeanCell cell = bean.cells[i];
64 Object content = cell.getContent();
65 if (content instanceof Variant) {
66 Variant cellVariant = (Variant) content;
67 if (cellVariant == LineContentBeanCell.EMPTY) {
68 currentCell.setStyle(cell.getStyleId());
70 currentCell.setContent("");
71 // if (currentCell != SpreadsheetCell.EMPTY) {
72 // line.cells.remove(i);
73 // line.cells.add(i, SpreadsheetCell.EMPTY);
75 } else if(ExcelFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
76 ExcelFormula formula = (ExcelFormula)cellVariant.getValue(ExcelFormula.BINDING);
77 SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
78 AstValue v = p.relation();
79 currentCell.setStyle(cell.getStyleId());
80 SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
81 currentCell.setContent(sformula);
82 } else if (ExcelArrayFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
83 ExcelArrayFormula formula = (ExcelArrayFormula)cellVariant.getValue(ExcelArrayFormula.BINDING);
84 SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
85 AstArrayFormulaReference v = new AstArrayFormulaReference(formula.range, p.relation());
86 currentCell.setStyle(cell.getStyleId());
87 SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
88 currentCell.setContent(sformula);
90 currentCell.setStyle(cell.getStyleId());
91 // DO not update constant values during update
92 currentCell.setContent(cellVariant.getValue());
94 } else if (content instanceof SpreadsheetSCLConstant){
95 currentCell.setStyle(cell.getStyleId());
96 currentCell.setContent(content);
98 } catch (Throwable e) {
99 Object content = cell.getContent();
100 if (content instanceof Variant) {
101 Variant cellVariant = (Variant) content;
102 currentCell.setStyle(cell.getStyleId());
103 currentCell.setContent(content);
105 new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();
106 } catch (AdaptException e1) {
107 e1.printStackTrace();
110 currentCell.setStyle(cell.getStyleId());
111 currentCell.setContent("LCB error happened");
117 @SuppressWarnings("unchecked")
119 public <T> T getConcrete() {