]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/synchronization/LineCommandBuilder.java
Improved exception message
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / synchronization / LineCommandBuilder.java
1 package org.simantics.spreadsheet.graph.synchronization;
2
3 import java.io.StringReader;
4
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;
22
23 public class LineCommandBuilder implements CommandBuilder {
24         
25         private String name;
26 //      private boolean update;
27         LineContentBean bean;
28         
29         public LineCommandBuilder(String name, boolean update) {
30                 this.name = name;
31 //              this.update = update;
32         }
33
34         @Override
35         public void apply(Solver solver) {
36                 
37                 SpreadsheetBook book = solver.getConcreteSolver();
38                 
39                 String path = name.substring(0, name.lastIndexOf("/"));
40                 String lineName = name.substring(name.lastIndexOf("/")+1);
41                 int row = Integer.parseInt(lineName.substring(3));
42                 
43                 SpreadsheetLines node = book.ensureSubprocess(path);
44                 SpreadsheetLine line = node.lines.get(-row);
45                 if(line == null) {
46                         line = new SpreadsheetLine(node, row);
47                         node.lines.put(-row, line);
48                 }
49                 
50 //              line.cells.clear();
51                 for(int i=0;i<bean.cells.length;i++) {
52                         
53                     SpreadsheetCell currentCell;
54                     if (line.cells.size() > i) {
55                         currentCell = line.cells.get(i);
56                     } else {
57                         currentCell = new SpreadsheetCell(line, i);
58                         line.cells.add(currentCell);
59                     }
60                     
61                         LineContentBeanCell cell = bean.cells[i];
62                         
63                         try {
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());
69                                         // Empty content
70                                         currentCell.setContent("");
71 //                                      if (currentCell != SpreadsheetCell.EMPTY) {
72 //                                          line.cells.remove(i);
73 //                                          line.cells.add(i, SpreadsheetCell.EMPTY);
74 //                                      }
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);
89                         } else {
90                         currentCell.setStyle(cell.getStyleId());
91                         // DO not update constant values during update
92                         currentCell.setContent(cellVariant.getValue());
93                         }
94                                 } else if (content instanceof SpreadsheetSCLConstant){
95                     currentCell.setStyle(cell.getStyleId());
96                     currentCell.setContent(content);
97                                 }
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);
104                     try {
105                         new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();
106                     } catch (AdaptException e1) {
107                         e1.printStackTrace();
108                     }
109                 } else {
110                     currentCell.setStyle(cell.getStyleId());
111                     currentCell.setContent("LCB error happened");
112                 }
113                         }
114                 }
115         }
116
117         @SuppressWarnings("unchecked")
118         @Override
119         public <T> T getConcrete() {
120                 return (T)this;
121         }
122         
123 }