]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java
SCL API for direct access to SpreadsheetBooks
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SpreadsheetGraphUtils.java
index 35a05d7aa59c7d45980f0a781a8fdd726ea621e3..3938e478b923d768220952d44301eedb449388f1 100644 (file)
@@ -12,6 +12,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import javax.xml.soap.Node;
+
 import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.binding.mutable.Variant;
@@ -33,13 +35,15 @@ 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.VariableNode;
 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.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple;
 import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.scl.runtime.tuple.Tuple2;
 import org.simantics.simulator.toolkit.StandardRealm;
@@ -242,8 +246,31 @@ public class SpreadsheetGraphUtils {
        
     }
 
-    
+    public static void forRows(ReadGraph graph, Variable run, String sheetName, int min, int max, Function1<Variable, Tuple> fn) throws DatabaseException {
+
+        String sessionName = run.getParent(graph).getURI(graph);
+        StandardRealm<SheetNode, SpreadsheetBook> realm = SpreadsheetSessionManager.getInstance().getOrCreateRealm(graph, sessionName);
+        SpreadsheetBook book = realm.getEngine();
+        SpreadsheetEngine engine = book.getEngine(sheetName);
+        if(engine == null) return;
+        
+        engine.forLines(line -> {
+
+            String path = line.getPath();
+            if(path == null) return;
 
+            try {
+                Variable lineVariable = run.browse(graph, path);
+                if(lineVariable != null)
+                    fn.apply(lineVariable);
+            } catch (DatabaseException e) {
+                // This is not reported here
+            }
+
+        } , min, max);
+        
+    }
+    
     public static List<Variable> possibleConfigurationCellVariables(ReadGraph graph, Variable sheet, Range range) throws DatabaseException {
         List<Variable> rowVariables = possibleConfigurationLineVariables(graph, sheet, range);
         List<Variable> result = new ArrayList<>();
@@ -651,4 +678,63 @@ public class SpreadsheetGraphUtils {
         throw new IllegalStateException("Expected StandardGraphChildVariable, got " + cell.getClass().getName());
     }
     
+    private static SpreadsheetCell getCellFromVariable(Variable cell) {
+        StandardGraphChildVariable std =  (StandardGraphChildVariable)cell;
+        return (SpreadsheetCell)std.node.node;
+    }
+    
+    private static SpreadsheetLine getLineFromVariable(Variable cell) {
+        StandardGraphChildVariable std =  (StandardGraphChildVariable)cell;
+        return (SpreadsheetLine)std.node.node;
+    }
+
+    public static Variable linesVariable(ReadGraph graph, Variable sheetVariable) throws DatabaseException {
+        while(!"Lines".equals(sheetVariable.getName(graph)))
+            sheetVariable = sheetVariable.getParent(graph);
+        return sheetVariable;
+    }
+    
+    public static Variable offsetCell(ReadGraph graph, Variable cellVariable, int x, int y) throws DatabaseException {
+        
+        Variable lineVariable = cellVariable.getParent(graph);
+        Variable offsetLine = offsetRow(graph, lineVariable, y);
+        if(offsetLine == null) return null;
+        SpreadsheetCell cell = getCellFromVariable(cellVariable);
+        return rowCell(graph, offsetLine, cell.column + x);
+        
+    }
+
+    public static Variable offsetRow(ReadGraph graph, Variable lineVariable, int offset) throws DatabaseException {
+        
+        if(offset == 0) return lineVariable;
+
+        SpreadsheetLine line = getLineFromVariable(lineVariable);
+        SpreadsheetLine offsetLine = line.possibleOffset(offset);
+        if(offsetLine == null) return null;
+        
+        Variable linesVariable = linesVariable(graph, lineVariable);
+        String path = offsetLine.getLinesPath();
+        return linesVariable.browsePossible(graph, path);
+
+    }
+    
+    public static Variable rowCell(ReadGraph graph, Variable lineVariable, int column) throws DatabaseException {
+        
+        SpreadsheetLine line = getLineFromVariable(lineVariable);
+        
+        return lineVariable.getPossibleChild(graph, Spreadsheets.cellName(line.row, column));
+        
+    }
+    
+    public static SpreadsheetBook spreadsheetBook(Variable variable) {
+        if(variable instanceof StandardGraphChildVariable) {
+            VariableNode<Node> node = ((StandardGraphChildVariable)variable).node;
+            if(node != null) {
+                if(node.node instanceof SpreadsheetBook)
+                    return (SpreadsheetBook) node.node;
+            }
+        }
+        return null;
+    }
+    
 }