]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
ExternalRef corrections private/spreadsheet_integration
authorAntti Villberg <antti.villberg@semantum.fi>
Sat, 4 May 2019 17:21:30 +0000 (20:21 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 16 May 2019 12:29:52 +0000 (15:29 +0300)
Change-Id: I58653d306eee400af86e3732fa1373d00794fbd9

bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetNodeManager.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/function/All.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/ExternalRef.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefConstant.java [new file with mode: 0644]
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/ExternalRefData.java [new file with mode: 0644]
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetBook.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCellEditable.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetSCLConstant.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java

index 51f292929c440ff6bde32142ec34222633ad565e..35a05d7aa59c7d45980f0a781a8fdd726ea621e3 100644 (file)
@@ -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 {
index a7bee9d4b5e0daf0af55359e3482cf5616af06f1..64d002f831d81c7198406b877ef21b7cb46cd3e9 100644 (file)
@@ -30,7 +30,6 @@ public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNod
 
     public SpreadsheetNodeManager(SpreadsheetRealm realm) {
         super(realm, realm.getEngine());
-        new Exception().printStackTrace();
         realm.getEngine().registerListener(new SpreadsheetBookListener() {
             
             @Override
@@ -39,7 +38,6 @@ public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNod
                     @Override
                     public void run() {
                         for(SpreadsheetCell cell : cells) {
-                            System.err.println("Modification in cell " + cell.getName());
                             refreshVariable(new SpreadsheetCellContent(cell));
                             Object content = cell.getContent();
                             if(content instanceof SpreadsheetFormula || content instanceof SpreadsheetSCLConstant)
index bc034086c7d1841eddc76d778b0d9c12e3b80171..0f6f422eb982c8229ce37fa200b66c8e8ba70a43 100644 (file)
@@ -208,7 +208,7 @@ public class All {
             StandardRealm<SheetNode, SpreadsheetBook> 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);
                 }
             }
index 70486d9d78f6d7b536b5e6f3c4728daf4577aa74..327e5aeb661d8941ef051fc6179dcc93a5e173fb 100644 (file)
@@ -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 (file)
index 0000000..2793269
--- /dev/null
@@ -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 (file)
index 0000000..f561f4b
--- /dev/null
@@ -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
index 8f6b9d80c676185989e4c1f0f82dd274f57c3ae4..686243ff1e10a09fafaf7f21b57c09aec35e2988 100644 (file)
@@ -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<SheetNode>, S
 
     private SpreadsheetMapping mapping;
     
-    private String context;
+    String context;
 
     private int idCounter = 1;
     
@@ -119,6 +118,7 @@ public class SpreadsheetBook implements StandardNodeManagerSupport<SheetNode>, 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<SheetNode>, 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<SheetNode>, 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<SheetNode>, 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<SheetNode>, 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<ExternalRef,ExternalRefData> 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() {
index dd05bf370fa92af0ddc41a2f1ba5f16bc8633c54..5b732cf864b9f768fd90435ca641fcb3e1e204c8 100644 (file)
@@ -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<String, SheetNode> 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;
index d3d57c4988709d04cbf43eed31ca2840c9f6a8a1..246bd705b7a4cb0e585a992711f68a07e1603dd6 100644 (file)
@@ -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) {
index 2fe8f7cae69a4d1fe374ecd2396956d5a2b621c5..cc0148edbd8a4c73f33422cd4d61ce8e0bbad296 100644 (file)
@@ -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();
index 78a6bd08fbc9f1a312853b95981780fd5f03c595..95c601b503a5bd4718afe31f4a140ff2d312986b 100644 (file)
@@ -23,7 +23,7 @@ public class LineUpdater extends ModuleUpdaterBase<SheetLineComponent> {
             @Override
             public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating,
                     Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
-                System.err.println("LineUpdater.apply " + value);
+//                System.err.println("LineUpdater.apply " + value);
                 LineCommandBuilder builder = context.<LineCommandBuilder>getConcreteCommand();
                 try {
                     LineContentBean valuee = (LineContentBean) value.getValue(LineContentBean.BINDING);