]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineCommandBuilder.java
Adopt spreadsheet changes made in Balas development
[simantics/platform.git] / bundles / org.simantics.spreadsheet / src / org / simantics / spreadsheet / synchronization / LineCommandBuilder.java
1 package org.simantics.spreadsheet.synchronization;
2
3 import java.io.StringReader;
4 import java.util.ArrayList;
5
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;
20
21 public class LineCommandBuilder implements CommandBuilder {
22
23     private String name;
24     LineContentBean bean;
25
26     public LineCommandBuilder(String name, boolean update) {
27         this.name = name;
28     }
29
30     @Override
31     public void apply(Solver solver) {
32
33         SpreadsheetBook book = solver.getConcreteSolver();
34
35         String path = name.substring(0, name.lastIndexOf("/"));
36         String lineName = name.substring(name.lastIndexOf("/")+1);
37         int row = Integer.parseInt(lineName.substring(3));
38
39         SpreadsheetLines node = book.ensureSubprocess(path);
40         SpreadsheetLine line = node.lines.get(-row);
41         if(line == null) {
42             line = new SpreadsheetLine(node, row);
43             node.lines.put(-row, line);
44         }
45         
46         ArrayList<SpreadsheetCell> changes = new ArrayList<>();
47
48         for(int i=0;i<bean.cells.length;i++) {
49
50             SpreadsheetCell currentCell;
51             if (line.cells.size() > i) {
52                 currentCell = line.cells.get(i);
53             } else {
54                 currentCell = new SpreadsheetCell(line, i);
55                 line.cells.add(currentCell);
56             }
57
58             LineContentBeanCell cell = bean.cells[i];
59
60             try {
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());
66                         // Empty content
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();
84                     } else {
85                         currentCell.setStyle(cell.getStyleId());
86                         // DO not update constant values during update
87                         currentCell.setContent(cellVariant.getValue());
88                     }
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);
94                 }
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);
101                     try {
102                         new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();
103                     } catch (AdaptException e1) {
104                         e1.printStackTrace();
105                     }
106                 } else {
107                     currentCell.setStyle(cell.getStyleId());
108                     currentCell.setContent("LCB error happened");
109                 }
110             }
111             
112             changes.add(currentCell);
113             
114         }
115         
116         book.fireChanges(changes);
117         
118     }
119
120     @SuppressWarnings("unchecked")
121     @Override
122     public <T> T getConcrete() {
123         return (T)this;
124     }
125
126 }