X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fsolver%2FSpreadsheetBook.java;fp=bundles%2Forg.simantics.spreadsheet%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fsolver%2FSpreadsheetBook.java;h=686243ff1e10a09fafaf7f21b57c09aec35e2988;hb=e5871be84f8ba53a1c80be728bcfb67231c29279;hp=8f6b9d80c676185989e4c1f0f82dd274f57c3ae4;hpb=fb89ec6bace6b3eef93a4c437171bb173fc861b6;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetBook.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetBook.java index 8f6b9d80c..686243ff1 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetBook.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetBook.java @@ -18,7 +18,6 @@ import org.simantics.databoard.binding.mutable.Variant; import org.simantics.simulator.toolkit.StandardNodeManagerSupport; import org.simantics.simulator.variable.exceptions.NodeManagerException; import org.simantics.spreadsheet.ExternalRef; -import org.simantics.spreadsheet.ExternalRef.ExternalRefListener; import org.simantics.spreadsheet.SpreadsheetCellStyle; import org.simantics.spreadsheet.SpreadsheetVisitor; import org.simantics.spreadsheet.solver.formula.SpreadsheetEvaluationEnvironment; @@ -56,7 +55,7 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S private SpreadsheetMapping mapping; - private String context; + String context; private int idCounter = 1; @@ -119,6 +118,7 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S Object content = scc.cell.evaluate(SpreadsheetEvaluationEnvironment.getInstance(this)); if(content == null) return Variant.ofInstance(""); if(content instanceof Variant) return content; + if(content instanceof ExternalRefData) return ((ExternalRefData)content).getContent(); else return Variant.ofInstance(content); } catch (Throwable t) { t.printStackTrace(); @@ -126,18 +126,18 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S } } else if (node instanceof SpreadsheetCellContentExpression) { SpreadsheetCellContentExpression scce = (SpreadsheetCellContentExpression)node; - if (scce.cell.content instanceof SpreadsheetFormula) { - SpreadsheetFormula formula = (SpreadsheetFormula)scce.cell.content; + if (scce.cell.getContent() instanceof SpreadsheetFormula) { + SpreadsheetFormula formula = (SpreadsheetFormula)scce.cell.getContent(); return formula.expression; - } else if (scce.cell.content instanceof SpreadsheetSCLConstant) { - SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) scce.cell.content; - return "=" + sclConstant.expression; + } else if (scce.cell.getContent() instanceof SpreadsheetSCLConstant) { + SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) scce.cell.getContent(); + return "=" + sclConstant.getExpression(); } else { - System.out.println("Broken SpreadsheetCellContentExpression possibly due to overwriting an existing expression with a constant or something else (current content is " + scce.cell.content + ")"); - if (scce.cell.content instanceof Variant) { - return scce.cell.content; + System.out.println("Broken SpreadsheetCellContentExpression possibly due to overwriting an existing expression with a constant or something else (current content is " + scce.cell.getContent() + ")"); + if (scce.cell.getContent() instanceof Variant) { + return scce.cell.getContent(); } else { - return Variant.ofInstance(scce.cell.content); + return Variant.ofInstance(scce.cell.getContent()); } } } else if (node instanceof SpreadsheetTypeNode) { @@ -162,6 +162,20 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S @Override public void setEngineValue(SheetNode node, Object value) { + + SpreadsheetCellContent scc = (SpreadsheetCellContent)node; + + Object content = scc.cell.evaluate(SpreadsheetEvaluationEnvironment.getInstance(this)); + System.err.println("content2: " + content); + if (content instanceof Variant) { + scc.cell.setContent(value); + } else if (content instanceof ExternalRefData) { + ExternalRefData erd = (ExternalRefData)content; + erd.getRef().modify(context, (Variant)value); + } else { + throw new IllegalStateException("Unable to set cell value"); + } + } @Override @@ -379,7 +393,7 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S v.visit(this); } - private SpreadsheetCell cellByReferenceKey(long ref) { + SpreadsheetCell cellByReferenceKey(long ref) { long sheet = ref >> 40; long row = (ref >> 20) & 0xFFFFF; long col = (ref) & 0xFFFFF; @@ -458,57 +472,27 @@ public class SpreadsheetBook implements StandardNodeManagerSupport, S return iterationEnabled; } - static Variant DEFAULT = Variant.ofInstance("Pending external reference"); - - class ExternalRefData { - private Variant value = DEFAULT; - public ExternalRefData(long referenceKey, ExternalRef ref) { - ref.listen(context, new ExternalRefListener() { - - boolean isDisposed = false; - - @Override - public void newValue(Variant newVariant) { - SpreadsheetCell cell = cellByReferenceKey(referenceKey); - if(cell.content instanceof SpreadsheetSCLConstant) { - SpreadsheetSCLConstant ssc = (SpreadsheetSCLConstant)cell.content; - if(ssc.content.equals(ref)) { - value = newVariant; - fireChanges(invalidate(cell)); - return; - } - } - isDisposed = true; - } - @Override - public boolean isDisposed() { - if(isDisposed) - return true; - return SpreadsheetBook.this.isDisposed(); - } - }); - } - } + static Variant DEFAULT_VALUE = Variant.ofInstance("Pending external reference"); private Map externalRefMap = new HashMap<>(); void registerListening(long referenceKey, ExternalRef ref) { ExternalRefData data = externalRefMap.get(ref); if(data == null) { - data = new ExternalRefData(referenceKey, ref); + data = new ExternalRefData(this, referenceKey, ref); externalRefMap.put(ref, data); } else { // Already registered } } - Variant getExternalRefValue(long referenceKey, ExternalRef ref) { + ExternalRefData getExternalRefValue(long referenceKey, ExternalRef ref) { ExternalRefData data = externalRefMap.get(ref); if(data == null) { registerListening(referenceKey, ref); - return DEFAULT; + return new ExternalRefData(this, referenceKey, new ExternalRefConstant(DEFAULT_VALUE)); } - return data.value; + return data; } public boolean isDisposed() {