]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java
Bugfixing sheets
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SpreadsheetGraphUtils.java
index 976f2aa07c57ae0dfac018b3507c3ffad4b3a03c..51f292929c440ff6bde32142ec34222633ad565e 100644 (file)
@@ -30,20 +30,29 @@ import org.simantics.db.common.utils.LiteralFileUtil;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.procedure.Listener;
 import org.simantics.db.service.ClusteringSupport;
 import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function;
+import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.scl.runtime.tuple.Tuple2;
 import org.simantics.simulator.toolkit.StandardRealm;
+import org.simantics.spreadsheet.CellEditor;
 import org.simantics.spreadsheet.ExternalRef;
+import org.simantics.spreadsheet.OperationMode;
 import org.simantics.spreadsheet.Range;
 import org.simantics.spreadsheet.Spreadsheets;
+import org.simantics.spreadsheet.Transaction;
 import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler;
 import org.simantics.spreadsheet.resource.SpreadsheetResource;
 import org.simantics.spreadsheet.solver.SheetNode;
 import org.simantics.spreadsheet.solver.SpreadsheetBook;
+import org.simantics.spreadsheet.solver.SpreadsheetCell;
 import org.simantics.spreadsheet.solver.SpreadsheetEngine;
 import org.simantics.spreadsheet.solver.SpreadsheetLine;
 import org.simantics.spreadsheet.solver.SpreadsheetStyle;
@@ -182,12 +191,6 @@ public class SpreadsheetGraphUtils {
                        
         SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
         
-//        System.err.println("sessionName : " + sessionName);
-//        System.err.println("bookResource : " + graph.getURI(bookResource));
-//        System.err.println("configuration : " + configuration.getURI(graph));
-//        System.err.println("realm : " + realm);
-//        System.err.println("book : " + book);
-        
         if (changeFlags == null) {
             synchronizer.fullSynchronization(configuration, handler);
         } else {
@@ -207,14 +210,6 @@ public class SpreadsheetGraphUtils {
             synchronizer.partialSynchronization(configuration, handler, changeFlags);
         }
         
-//        book.accept(new InvalidateAll());
-//        realm.getNodeManager().refreshVariables();
-//        mapping.currentRevision = synchronizer.getHeadRevisionId();
-//        mapping.setTrustUids(true);
-        // Clean up queries
-//        QueryControl qc = g.getService(QueryControl.class);
-//        qc.flush(g);
-//        TimeLogger.log("Finished full synchronization");
         realm.getNodeManager().fireNodeListeners();
         return handler.getDidChanges();
         
@@ -492,7 +487,6 @@ public class SpreadsheetGraphUtils {
     }
     
     public static Variant extRefVariable(ReadGraph graph, Variable var) throws DatabaseException {
-        System.err.println("extRefVariable " + var.getURI(graph));
         return new Variant(Bindings.VOID, new ExternalRefVariable(graph, var));
     }
     
@@ -506,13 +500,11 @@ public class SpreadsheetGraphUtils {
         
         @Override
         public void listen(Object context, ExternalRefListener listener) {
-            System.err.println("listen " + listener);
             Simantics.getSession().asyncRequest(new UnaryRead<String, Variant>(uri) {
 
                 @Override
                 public Variant perform(ReadGraph graph) throws DatabaseException {
                     Variable variable = Variables.getVariable(graph, parameter);
-                    System.err.println("ExternalRef value for " + variable.getURI(graph));
                     return variable.getVariantValue(graph);
                 }
                 
@@ -520,7 +512,6 @@ public class SpreadsheetGraphUtils {
 
                 @Override
                 public void execute(Variant result) {
-                    System.err.println("execute " + result);
                     listener.newValue(result);
                 }
 
@@ -540,7 +531,6 @@ public class SpreadsheetGraphUtils {
     }
 
     public static Variant extRefActiveVariable(ReadGraph graph, Variable var) throws DatabaseException {
-        System.err.println("extRefActiveVariable " + var.getURI(graph));
         return new Variant(Bindings.VOID, new ExternalRefActiveVariable(graph, var));
     }
     
@@ -554,30 +544,26 @@ public class SpreadsheetGraphUtils {
         
         @Override
         public void listen(Object context, ExternalRefListener listener) {
-            System.err.println("listen " + context + " " + listener);
             Simantics.getSession().asyncRequest(new BinaryRead<String, String, Variant>((String)context, uri) {
 
                 @Override
                 public Variant perform(ReadGraph graph) throws DatabaseException {
                     Variable contextVariable = Variables.getVariable(graph, parameter);
-                    System.err.println("extref1 " + contextVariable.getURI(graph));
                     Variable configVariable = Variables.getVariable(graph, parameter2);
-                    System.err.println("extref2 " + configVariable.getURI(graph));
                     Variable activeVariable = Variables.switchPossibleContext(graph, configVariable, contextVariable.getRepresents(graph));
-                    System.err.println("ExternalRef value for " + activeVariable.getURI(graph));
+                    if(activeVariable == null) return Variant.ofInstance("Could not resolve " + configVariable.getURI(graph) + " for " + contextVariable.getURI(graph));
                     return activeVariable.getVariantValue(graph);
                 }
             }, new Listener<Variant>() {
 
                 @Override
                 public void execute(Variant result) {
-                    System.err.println("execute " + result);
                     listener.newValue(result);
                 }
 
                 @Override
                 public void exception(Throwable t) {
-                    LOGGER.error("Error while evaluating variable value", t);
+                    LOGGER.error("Error while evaluating variable value, context = " + context + " uri=" + uri, t);
                 }
 
                 @Override
@@ -590,4 +576,47 @@ public class SpreadsheetGraphUtils {
         
     }
     
+    public static CellEditor cellEditor(ReadGraph graph, Resource sheet) throws DatabaseException {
+        SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
+        Variable sheetVariable = Variables.getVariable(graph, sheet);
+        return sheetVariable.getPropertyValue(graph, SHEET.cellEditor);
+    }
+    
+    public static final String SPREADSHEET_TRANSACTION = "spreadsheetTransaction";
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public static Object syncExec(CellEditor editor, OperationMode mode, Function fun) throws InterruptedException {
+        
+        Transaction tr = editor.startTransaction(mode);
+        
+        SCLContext context = SCLContext.getCurrent();
+        Transaction oldTransaction = (Transaction)context.put(SPREADSHEET_TRANSACTION, tr);
+        
+        Object result = null;
+        
+        try {
+
+            result = fun.apply(Tuple0.INSTANCE);
+            
+        } finally {
+            
+            tr.commit();
+            
+            context.put(SPREADSHEET_TRANSACTION, oldTransaction);
+            
+        }
+        
+        return result;
+        
+    }
+    
+    public static int cellColumn(ReadGraph graph, Variable cell) {
+        if(cell instanceof StandardGraphChildVariable) {
+            StandardGraphChildVariable sgcv = (StandardGraphChildVariable)cell;
+            SpreadsheetCell sc = (SpreadsheetCell)sgcv.node.node;
+            return sc.getColumn();
+        }
+        throw new IllegalStateException("Expected StandardGraphChildVariable, got " + cell.getClass().getName());
+    }
+    
 }