-package org.simantics.spreadsheet.graph.synchronization;\r
-\r
-import java.io.StringReader;\r
-\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.spreadsheet.graph.ExcelArrayFormula;\r
-import org.simantics.spreadsheet.graph.ExcelFormula;\r
-import org.simantics.spreadsheet.graph.SpreadsheetBook;\r
-import org.simantics.spreadsheet.graph.SpreadsheetCell;\r
-import org.simantics.spreadsheet.graph.SpreadsheetFormula;\r
-import org.simantics.spreadsheet.graph.SpreadsheetLine;\r
-import org.simantics.spreadsheet.graph.SpreadsheetLines;\r
-import org.simantics.spreadsheet.graph.SpreadsheetSCLConstant;\r
-import org.simantics.spreadsheet.graph.function.LineContentBean;\r
-import org.simantics.spreadsheet.graph.function.LineContentBeanCell;\r
-import org.simantics.spreadsheet.graph.parser.SheetFormulaParser;\r
-import org.simantics.spreadsheet.graph.parser.ast.AstArrayFormulaReference;\r
-import org.simantics.spreadsheet.graph.parser.ast.AstValue;\r
-import org.simantics.structural.synchronization.base.CommandBuilder;\r
-import org.simantics.structural.synchronization.base.Solver;\r
-\r
-public class LineCommandBuilder implements CommandBuilder {\r
- \r
- private String name;\r
-// private boolean update;\r
- LineContentBean bean;\r
- \r
- public LineCommandBuilder(String name, boolean update) {\r
- this.name = name;\r
-// this.update = update;\r
- }\r
-\r
- @Override\r
- public void apply(Solver solver) {\r
- \r
- SpreadsheetBook book = solver.getConcreteSolver();\r
- \r
- String path = name.substring(0, name.lastIndexOf("/"));\r
- String lineName = name.substring(name.lastIndexOf("/")+1);\r
- int row = Integer.parseInt(lineName.substring(3));\r
- \r
- SpreadsheetLines node = book.ensureSubprocess(path);\r
- SpreadsheetLine line = node.lines.get(-row);\r
- if(line == null) {\r
- line = new SpreadsheetLine(node, row);\r
- node.lines.put(-row, line);\r
- }\r
- \r
-// line.cells.clear();\r
- for(int i=0;i<bean.cells.length;i++) {\r
- \r
- SpreadsheetCell currentCell;\r
- if (line.cells.size() > i) {\r
- currentCell = line.cells.get(i);\r
- } else {\r
- currentCell = new SpreadsheetCell(line, i);\r
- line.cells.add(currentCell);\r
- }\r
- \r
- LineContentBeanCell cell = bean.cells[i];\r
- \r
- try {\r
- Object content = cell.getContent();\r
- if (content instanceof Variant) {\r
- Variant cellVariant = (Variant) content;\r
- if (cellVariant == LineContentBeanCell.EMPTY) {\r
- currentCell.setStyle(cell.getStyleId());\r
- // Empty content\r
- currentCell.setContent("");\r
-// if (currentCell != SpreadsheetCell.EMPTY) {\r
-// line.cells.remove(i);\r
-// line.cells.add(i, SpreadsheetCell.EMPTY);\r
-// }\r
- } else if(ExcelFormula.BINDING.type().equals(cellVariant.getBinding().type())) {\r
- ExcelFormula formula = (ExcelFormula)cellVariant.getValue(ExcelFormula.BINDING);\r
- SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));\r
- AstValue v = p.relation();\r
- currentCell.setStyle(cell.getStyleId());\r
- SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);\r
- currentCell.setContent(sformula);\r
- } else if (ExcelArrayFormula.BINDING.type().equals(cellVariant.getBinding().type())) {\r
- ExcelArrayFormula formula = (ExcelArrayFormula)cellVariant.getValue(ExcelArrayFormula.BINDING);\r
- SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));\r
- AstArrayFormulaReference v = new AstArrayFormulaReference(formula.range, p.relation());\r
- currentCell.setStyle(cell.getStyleId());\r
- SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);\r
- currentCell.setContent(sformula);\r
- } else {\r
- currentCell.setStyle(cell.getStyleId());\r
- // DO not update constant values during update\r
- currentCell.setContent(cellVariant.getValue());\r
- }\r
- } else if (content instanceof SpreadsheetSCLConstant){\r
- currentCell.setStyle(cell.getStyleId());\r
- currentCell.setContent(content);\r
- }\r
- } catch (Throwable e) {\r
- Object content = cell.getContent();\r
- if (content instanceof Variant) {\r
- Variant cellVariant = (Variant) content;\r
- currentCell.setStyle(cell.getStyleId());\r
- currentCell.setContent(content);\r
- try {\r
- new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();\r
- } catch (AdaptException e1) {\r
- e1.printStackTrace();\r
- }\r
- } else {\r
- currentCell.setStyle(cell.getStyleId());\r
- currentCell.setContent("LCB error happened");\r
- }\r
- }\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <T> T getConcrete() {\r
- return (T)this;\r
- }\r
- \r
+package org.simantics.spreadsheet.graph.synchronization;
+
+import java.io.StringReader;
+
+import org.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.spreadsheet.graph.ExcelArrayFormula;
+import org.simantics.spreadsheet.graph.ExcelFormula;
+import org.simantics.spreadsheet.graph.SpreadsheetBook;
+import org.simantics.spreadsheet.graph.SpreadsheetCell;
+import org.simantics.spreadsheet.graph.SpreadsheetFormula;
+import org.simantics.spreadsheet.graph.SpreadsheetLine;
+import org.simantics.spreadsheet.graph.SpreadsheetLines;
+import org.simantics.spreadsheet.graph.SpreadsheetSCLConstant;
+import org.simantics.spreadsheet.graph.function.LineContentBean;
+import org.simantics.spreadsheet.graph.function.LineContentBeanCell;
+import org.simantics.spreadsheet.graph.parser.SheetFormulaParser;
+import org.simantics.spreadsheet.graph.parser.ast.AstArrayFormulaReference;
+import org.simantics.spreadsheet.graph.parser.ast.AstValue;
+import org.simantics.structural.synchronization.base.CommandBuilder;
+import org.simantics.structural.synchronization.base.Solver;
+
+public class LineCommandBuilder implements CommandBuilder {
+
+ private String name;
+// private boolean update;
+ LineContentBean bean;
+
+ public LineCommandBuilder(String name, boolean update) {
+ this.name = name;
+// this.update = update;
+ }
+
+ @Override
+ public void apply(Solver solver) {
+
+ SpreadsheetBook book = solver.getConcreteSolver();
+
+ String path = name.substring(0, name.lastIndexOf("/"));
+ String lineName = name.substring(name.lastIndexOf("/")+1);
+ int row = Integer.parseInt(lineName.substring(3));
+
+ SpreadsheetLines node = book.ensureSubprocess(path);
+ SpreadsheetLine line = node.lines.get(-row);
+ if(line == null) {
+ line = new SpreadsheetLine(node, row);
+ node.lines.put(-row, line);
+ }
+
+// line.cells.clear();
+ for(int i=0;i<bean.cells.length;i++) {
+
+ SpreadsheetCell currentCell;
+ if (line.cells.size() > i) {
+ currentCell = line.cells.get(i);
+ } else {
+ currentCell = new SpreadsheetCell(line, i);
+ line.cells.add(currentCell);
+ }
+
+ LineContentBeanCell cell = bean.cells[i];
+
+ try {
+ Object content = cell.getContent();
+ if (content instanceof Variant) {
+ Variant cellVariant = (Variant) content;
+ if (cellVariant == LineContentBeanCell.EMPTY) {
+ currentCell.setStyle(cell.getStyleId());
+ // Empty content
+ currentCell.setContent("");
+// if (currentCell != SpreadsheetCell.EMPTY) {
+// line.cells.remove(i);
+// line.cells.add(i, SpreadsheetCell.EMPTY);
+// }
+ } else if(ExcelFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
+ ExcelFormula formula = (ExcelFormula)cellVariant.getValue(ExcelFormula.BINDING);
+ SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
+ AstValue v = p.relation();
+ currentCell.setStyle(cell.getStyleId());
+ SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
+ currentCell.setContent(sformula);
+ } else if (ExcelArrayFormula.BINDING.type().equals(cellVariant.getBinding().type())) {
+ ExcelArrayFormula formula = (ExcelArrayFormula)cellVariant.getValue(ExcelArrayFormula.BINDING);
+ SheetFormulaParser p = new SheetFormulaParser(new StringReader(formula.expression));
+ AstArrayFormulaReference v = new AstArrayFormulaReference(formula.range, p.relation());
+ currentCell.setStyle(cell.getStyleId());
+ SpreadsheetFormula sformula = new SpreadsheetFormula(v, formula.expression);
+ currentCell.setContent(sformula);
+ } else {
+ currentCell.setStyle(cell.getStyleId());
+ // DO not update constant values during update
+ currentCell.setContent(cellVariant.getValue());
+ }
+ } else if (content instanceof SpreadsheetSCLConstant){
+ currentCell.setStyle(cell.getStyleId());
+ currentCell.setContent(content);
+ }
+ } catch (Throwable e) {
+ Object content = cell.getContent();
+ if (content instanceof Variant) {
+ Variant cellVariant = (Variant) content;
+ currentCell.setStyle(cell.getStyleId());
+ currentCell.setContent(content);
+ try {
+ new Exception("failed: " + ((ExcelFormula)(cellVariant.getValue(ExcelFormula.BINDING))).expression, e).printStackTrace();
+ } catch (AdaptException e1) {
+ e1.printStackTrace();
+ }
+ } else {
+ currentCell.setStyle(cell.getStyleId());
+ currentCell.setContent("LCB error happened");
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getConcrete() {
+ return (T)this;
+ }
+
}
\ No newline at end of file