From e5871be84f8ba53a1c80be728bcfb67231c29279 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Sat, 4 May 2019 20:21:30 +0300 Subject: [PATCH] ExternalRef corrections Change-Id: I58653d306eee400af86e3732fa1373d00794fbd9 --- .../graph/SpreadsheetGraphUtils.java | 32 ++++++++ .../graph/SpreadsheetNodeManager.java | 2 - .../spreadsheet/graph/function/All.java | 4 +- .../simantics/spreadsheet/ExternalRef.java | 1 + .../solver/ExternalRefConstant.java | 24 ++++++ .../spreadsheet/solver/ExternalRefData.java | 52 +++++++++++++ .../spreadsheet/solver/SpreadsheetBook.java | 78 ++++++++----------- .../spreadsheet/solver/SpreadsheetCell.java | 16 ++-- .../solver/SpreadsheetCellEditable.java | 10 +-- .../solver/SpreadsheetSCLConstant.java | 12 ++- .../synchronization/LineUpdater.java | 2 +- 11 files changed, 166 insertions(+), 67 deletions(-) create mode 100644 bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefConstant.java create mode 100644 bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefData.java diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java index 51f292929..35a05d7aa 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java @@ -26,6 +26,7 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.BinaryRead; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.LiteralFileUtil; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ServiceException; @@ -528,6 +529,20 @@ public class SpreadsheetGraphUtils { }); } + @Override + public void modify(Object context, Variant newValue) { + + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Variable variable = Variables.getVariable(graph, uri); + variable.setValue(graph, newValue); + } + }); + + } + } public static Variant extRefActiveVariable(ReadGraph graph, Variable var) throws DatabaseException { @@ -574,6 +589,23 @@ public class SpreadsheetGraphUtils { }); } + @Override + public void modify(Object context, Variant newValue) { + + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Variable contextVariable = Variables.getVariable(graph, (String)context); + Variable configVariable = Variables.getVariable(graph,uri); + Variable activeVariable = Variables.switchPossibleContext(graph, configVariable, contextVariable.getRepresents(graph)); + if(activeVariable == null) return; + activeVariable.setValue(graph, newValue.getValue(), newValue.getBinding()); + } + }); + + } + } public static CellEditor cellEditor(ReadGraph graph, Resource sheet) throws DatabaseException { diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetNodeManager.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetNodeManager.java index a7bee9d4b..64d002f83 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetNodeManager.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetNodeManager.java @@ -30,7 +30,6 @@ public class SpreadsheetNodeManager extends StandardVariableNodeManager realm = SpreadsheetSessionManager.getInstance().getOrCreateRealm(graph, sessionName); SpreadsheetBook book = realm.getEngine(); SpreadsheetCell sc = book.get(sheet.getName(graph), r.startRow, r.startColumn); - sc.setContent(value); + //sc.setContent(value); realm.asyncExec(new Runnable() { @Override @@ -714,7 +714,6 @@ public class All { if (style != null) styleId = graph.getPossibleRelatedValue(style, SR.Style_id, Bindings.INTEGER); if (styleId == null) { - System.err.println("Style " + style + " has no ID or either cell "+ repr + " has no style attached to it !!"); styleId = SpreadsheetStyle.empty().getStyleId(); } @@ -898,7 +897,6 @@ public class All { } if (runCell != null) { - System.out.println("All.edit " + runCell.getURI(graph)); runCell.setPropertyValue(graph, SHEET.Cell_content, value, Bindings.VARIANT); } } diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/ExternalRef.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/ExternalRef.java index 70486d9d7..327e5aeb6 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/ExternalRef.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/ExternalRef.java @@ -12,5 +12,6 @@ public interface ExternalRef { } public void listen(Object context, ExternalRefListener listener); + public void modify(Object context, Variant newValue); } diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefConstant.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefConstant.java new file mode 100644 index 000000000..279326953 --- /dev/null +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefConstant.java @@ -0,0 +1,24 @@ +package org.simantics.spreadsheet.solver; + +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.spreadsheet.ExternalRef; + +public class ExternalRefConstant implements ExternalRef { + + final private Variant constant; + + public ExternalRefConstant(Variant constant) { + this.constant = constant; + } + + @Override + public void listen(Object context, ExternalRefListener listener) { + listener.newValue(constant); + } + + @Override + public void modify(Object context, Variant newValue) { + } + +} + diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefData.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefData.java new file mode 100644 index 000000000..f561f4b1b --- /dev/null +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefData.java @@ -0,0 +1,52 @@ +package org.simantics.spreadsheet.solver; + +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.spreadsheet.ExternalRef; +import org.simantics.spreadsheet.ExternalRef.ExternalRefListener; + +class ExternalRefData implements ExternalRefListener { + + final private SpreadsheetBook book; + final private long referenceKey; + final private ExternalRef ref; + private boolean isDisposed = false; + private Variant value = SpreadsheetBook.DEFAULT_VALUE; + + public ExternalRefData(SpreadsheetBook book, long referenceKey, ExternalRef ref) { + this.book = book; + this.referenceKey = referenceKey; + this.ref = ref; + ref.listen(book.context, this); + } + + public Variant getContent() { + return value; + } + + public ExternalRef getRef() { + return ref; + } + + @Override + public void newValue(Variant newVariant) { + SpreadsheetCell cell = book.cellByReferenceKey(referenceKey); + if(cell.getContent() instanceof SpreadsheetSCLConstant) { + SpreadsheetSCLConstant ssc = (SpreadsheetSCLConstant)cell.getContent(); + Object content = ssc.getContent(); + if(content.equals(ref)) { + value = newVariant; + book.fireChanges(book.invalidate(cell)); + return; + } + } + isDisposed = true; + } + + @Override + public boolean isDisposed() { + if(isDisposed) + return true; + return book.isDisposed(); + } + +} \ No newline at end of file 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() { diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java index dd05bf370..5b732cf86 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java @@ -35,7 +35,7 @@ public class SpreadsheetCell implements SpreadsheetElement, SheetNode { final private SpreadsheetLine line; final private int column; int style; - Object content; + private Object content; final private Map properties; public SpreadsheetCell(SpreadsheetLine line, int column) { @@ -126,13 +126,15 @@ public class SpreadsheetCell implements SpreadsheetElement, SheetNode { return (T)f.result; } else if (content instanceof SpreadsheetSCLConstant) { SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) content; - if(sclConstant.content instanceof Variant) { - Variant v = (Variant)sclConstant.content; - return (T) sclConstant.content; - } else if (sclConstant.content instanceof ExternalRef) { - return (T)env.getBook().getExternalRefValue(makeReferenceKey(), (ExternalRef)sclConstant.content); + Object c = sclConstant.getContent(); + if(c instanceof Variant) { + Variant v = (Variant)c; + return (T) c; + } else if (c instanceof ExternalRef) { + ExternalRefData erd = env.getBook().getExternalRefValue(makeReferenceKey(), (ExternalRef)c); + return (T)erd; } else { - throw new IllegalStateException(); + throw new IllegalStateException("Unsupported content " + c); } } else { this.inProgress = false; diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCellEditable.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCellEditable.java index d3d57c498..246bd705b 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCellEditable.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCellEditable.java @@ -57,15 +57,15 @@ public class SpreadsheetCellEditable implements SheetNode { } public boolean editable() { - if (cell.content == null || cell.content instanceof SpreadsheetFormula || cell.content instanceof SpreadsheetSCLConstant) + if (cell.getContent() == null || cell.getContent() instanceof SpreadsheetFormula || cell.getContent() instanceof SpreadsheetSCLConstant) return false; - if (cell.content instanceof String) { - String content = (String) cell.content; + if (cell.getContent() instanceof String) { + String content = (String) cell.getContent(); if (content.isEmpty()) return false; } - if (cell.content instanceof Variant) { - Variant content = (Variant) cell.content; + if (cell.getContent() instanceof Variant) { + Variant content = (Variant) cell.getContent(); if (content.getValue() == null) return false; if (content.getValue() instanceof String) { diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetSCLConstant.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetSCLConstant.java index 2fe8f7cae..cc0148edb 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetSCLConstant.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetSCLConstant.java @@ -9,8 +9,8 @@ public class SpreadsheetSCLConstant implements Serializable { private static final long serialVersionUID = 428064772427245449L; - public String expression; - public Object content; + private String expression; + private Object content; public static final Binding BINDING = Bindings.getBindingUnchecked(SpreadsheetSCLConstant.class); @@ -19,6 +19,14 @@ public class SpreadsheetSCLConstant implements Serializable { this.expression = expression; } + public Object getContent() { + return content; + } + + public String getExpression() { + return expression; + } + @Override public String toString() { return super.toString(); diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java index 78a6bd08f..95c601b50 100644 --- a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java @@ -23,7 +23,7 @@ public class LineUpdater extends ModuleUpdaterBase { @Override public void apply(ModuleUpdateContext context, boolean isCreating, Map propertyMap, Map> connectionMap, Variant value) { - System.err.println("LineUpdater.apply " + value); +// System.err.println("LineUpdater.apply " + value); LineCommandBuilder builder = context.getConcreteCommand(); try { LineContentBean valuee = (LineContentBean) value.getValue(LineContentBean.BINDING); -- 2.43.2