X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fsynchronization%2FLineCommandBuilder.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fsynchronization%2FLineCommandBuilder.java;h=bdfc97468b960893e8ad145c16dde184132724d0;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/synchronization/LineCommandBuilder.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/synchronization/LineCommandBuilder.java new file mode 100644 index 000000000..bdfc97468 --- /dev/null +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/synchronization/LineCommandBuilder.java @@ -0,0 +1,123 @@ +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; + } + +} \ No newline at end of file