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 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 getConcrete() { return (T)this; } }