]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
SCL API for direct access to SpreadsheetBooks 71/3671/1
authorAntti Villberg <antti.villberg@semantum.fi>
Wed, 4 Dec 2019 15:47:12 +0000 (17:47 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 4 Dec 2019 15:47:12 +0000 (17:47 +0200)
gitlab #429

Change-Id: Icb5090d9110034a4c9103cc2413084b222aed20c

12 files changed:
bundles/org.simantics.spreadsheet.graph/scl/Spreadsheet/All.scl
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelImport.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetGraphUtils.java
bundles/org.simantics.spreadsheet/build.properties
bundles/org.simantics.spreadsheet/scl/Spreadsheet/Solver.scl [new file with mode: 0644]
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/Range.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/BinarySearch.java [new file with mode: 0644]
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetCell.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetEngine.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetLine.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetLines.java
bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/synchronization/LineUpdater.java

index aa58a7f73c2f5654dffac51ce7e81accd1eee6be..22f8043ff90d6d82c5a24afd3b6058a48eab22dc 100644 (file)
@@ -3,43 +3,16 @@ include "Simantics/Ontologies"
 include "Simantics/SCL"
 include "Simantics/UI"
 include "Document/All"
 include "Simantics/SCL"
 include "Simantics/UI"
 include "Document/All"
+include "Spreadsheet/Solver"
 include "File"
 
 include "File"
 
-
-effect SpreadsheetTransaction 
-    "spreadsheetTransaction" 
-    "org.simantics.spreadsheet.Transaction"
-
-importJava "org.simantics.spreadsheet.graph.ExternalRef" where
-    data ExternalRef
-
 importJava "org.simantics.spreadsheet.common.TableCell" where
     data TableCell
 
 importJava "org.simantics.spreadsheet.common.TableCell" where
     data TableCell
 
-importJava "java.util.function.Consumer" where
-    data Consumer
-
-importJava "org.simantics.spreadsheet.CellEditor" where
-    data CellEditor
-    startTransaction :: CellEditor -> OperationMode -> <Proc> Transaction
-    @JavaName "edit"
-    editProperty_ :: CellEditor -> String -> String -> a -> Binding a -> Maybe Consumer -> <SpreadsheetTransaction> () 
-    @JavaName "edit"
-    editContent :: CellEditor -> String -> Variant -> Maybe Consumer -> <SpreadsheetTransaction> () 
-
 @inline
 editProperty :: Serializable a => CellEditor -> String -> String -> a -> Maybe Consumer -> <SpreadsheetTransaction> ()
 editProperty editor location property value consumer = editProperty_ editor location property value binding consumer
 
 @inline
 editProperty :: Serializable a => CellEditor -> String -> String -> a -> Maybe Consumer -> <SpreadsheetTransaction> ()
 editProperty editor location property value consumer = editProperty_ editor location property value binding consumer
 
-importJava "org.simantics.spreadsheet.OperationMode" where
-    data OperationMode
-    OPERATION :: OperationMode
-    EDIT_MODE :: OperationMode
-
-importJava "org.simantics.spreadsheet.Transaction" where
-    data Transaction
-    commit :: Transaction -> <Proc> ()
-
 importJava "org.simantics.spreadsheet.common.TreeTableCell" where
     data TreeTableCell
 
 importJava "org.simantics.spreadsheet.common.TreeTableCell" where
     data TreeTableCell
 
@@ -58,9 +31,6 @@ importJava "org.simantics.spreadsheet.common.TreeTableCell" where
     treeTableCellRow :: TreeTableCell -> <Proc> Integer
     @JavaName getColumn
     treeTableCellColumn :: TreeTableCell -> <Proc> Integer
     treeTableCellRow :: TreeTableCell -> <Proc> Integer
     @JavaName getColumn
     treeTableCellColumn :: TreeTableCell -> <Proc> Integer
-    
-importJava "org.simantics.spreadsheet.common.SpreadsheetCell" where
-    data SpreadsheetCell
 
 importJava "org.simantics.spreadsheet.graph.SCL" where
     toVariant :: a -> Variant
 
 importJava "org.simantics.spreadsheet.graph.SCL" where
     toVariant :: a -> Variant
@@ -76,12 +46,13 @@ importJava "org.simantics.spreadsheet.graph.SpreadsheetGraphUtils" where
     invalidateAll :: Variable -> <ReadGraph> ()
     extRefVariable :: Variable -> <ReadGraph> Variant
     extRefActiveVariable :: Variable -> <ReadGraph> Variant
     invalidateAll :: Variable -> <ReadGraph> ()
     extRefVariable :: Variable -> <ReadGraph> Variant
     extRefActiveVariable :: Variable -> <ReadGraph> Variant
+    forRows :: Variable -> String -> Integer -> Integer -> (Variable -> <ReadGraph,Proc> ()) -> <ReadGraph> ()
+    rowCell :: Variable -> Integer -> <ReadGraph> Maybe Variable
+    offsetCell :: Variable -> Integer -> Integer -> <ReadGraph> Maybe Variable
     cellEditor :: Resource -> <ReadGraph> CellEditor
     syncExec :: CellEditor -> OperationMode -> (<SpreadsheetTransaction, Proc> a) -> <Proc> a
     cellColumn :: Variable -> <ReadGraph> Integer
     cellEditor :: Resource -> <ReadGraph> CellEditor
     syncExec :: CellEditor -> OperationMode -> (<SpreadsheetTransaction, Proc> a) -> <Proc> a
     cellColumn :: Variable -> <ReadGraph> Integer
-
-importJava "org.simantics.spreadsheet.Spreadsheets" where
-     cellName :: Integer -> Integer -> String
+    spreadsheetBook :: Variable -> <Proc> SpreadsheetBook
 
 importJava "org.simantics.spreadsheet.util.SpreadsheetUtils" where
     createSheet :: Resource -> String -> <WriteGraph> Resource
 
 importJava "org.simantics.spreadsheet.util.SpreadsheetUtils" where
     createSheet :: Resource -> String -> <WriteGraph> Resource
@@ -102,11 +73,10 @@ importJava "org.simantics.spreadsheet.util.SpreadsheetUtils" where
     @JavaName "sheetRun"
     bookRun :: Resource -> Variable -> <ReadGraph> Variable
 
     @JavaName "sheetRun"
     bookRun :: Resource -> Variable -> <ReadGraph> Variable
 
-sheetRunDefault :: Resource -> <ReadGraph> Variable
-sheetRunDefault sheet = sheetRun sheet (resourceVariable sheet)
+sheetRunDefault = bookRunDefault
 
 
-importJava "org.simantics.spreadsheet.graph.SpreadsheetSessionManager" where
-    removeSpreadsheetSession :: Variable -> <Proc, WriteGraph> ()
+bookRunDefault :: Resource -> <ReadGraph> Variable
+bookRunDefault book = bookRun book (resourceVariable book)
 
 importJava "org.simantics.spreadsheet.graph.ExcelImport" where
     importBook :: Resource -> File -> <Proc> ()
 
 importJava "org.simantics.spreadsheet.graph.ExcelImport" where
     importBook :: Resource -> File -> <Proc> ()
index 24a0a85d0ce15c204e14da3eb7b9ddef85daba48..a8bb342b33a2e8cb8fb5e796c365bbbc4b12be97 100644 (file)
@@ -224,8 +224,12 @@ public class ExcelImport {
                                         } else if(Cell.CELL_TYPE_STRING == val.getCellType()) {
                                             graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);
                                         } else if(Cell.CELL_TYPE_NUMERIC == val.getCellType()) {
                                         } else if(Cell.CELL_TYPE_STRING == val.getCellType()) {
                                             graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);
                                         } else if(Cell.CELL_TYPE_NUMERIC == val.getCellType()) {
-                                            Double value = Double.parseDouble(val.toString());
-                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(value), Bindings.VARIANT);
+                                            try {
+                                                Double value = Double.parseDouble(val.toString());
+                                                graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(value), Bindings.VARIANT);
+                                            } catch (Exception e) {
+                                                graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);
+                                            }
                                         } else {
                                             graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);
                                             System.err.println("Unprocessed cell type " + val.getCellType() + ", SheetName: " + sheetName + ", Row: " + rowN + ", Col:" + i);
                                         } else {
                                             graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);
                                             System.err.println("Unprocessed cell type " + val.getCellType() + ", SheetName: " + sheetName + ", Row: " + rowN + ", Col:" + i);
index 35a05d7aa59c7d45980f0a781a8fdd726ea621e3..3938e478b923d768220952d44301eedb449388f1 100644 (file)
@@ -12,6 +12,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
 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;
 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.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.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.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;
 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<>();
     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());
     }
     
         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;
+    }
+    
 }
 }
index dfa46571bba26df766d0c2656b9133051b8a9ca5..3e4e0e1f137b58f213cd90680ae94ae0d8cbf313 100644 (file)
@@ -12,4 +12,5 @@
 source.. = src/
 output.. = bin/
 bin.includes = META-INF/,\
 source.. = src/
 output.. = bin/
 bin.includes = META-INF/,\
-               .
+               .,\
+               scl/
diff --git a/bundles/org.simantics.spreadsheet/scl/Spreadsheet/Solver.scl b/bundles/org.simantics.spreadsheet/scl/Spreadsheet/Solver.scl
new file mode 100644 (file)
index 0000000..629767c
--- /dev/null
@@ -0,0 +1,55 @@
+
+importJava "org.simantics.spreadsheet.ExternalRef" where
+    data ExternalRef
+    
+importJava "org.simantics.spreadsheet.solver.SpreadsheetBook" where
+    data SpreadsheetBook
+    @JavaName "getEngine"
+    spreadsheetBookEngine :: SpreadsheetBook -> String -> <Proc> SpreadsheetEngine
+
+importJava "org.simantics.spreadsheet.solver.SpreadsheetEngine" where
+    data SpreadsheetEngine
+    @JavaName "getLines"
+    spreadsheetEngineLines :: SpreadsheetEngine -> Integer -> Integer -> <Proc> [SpreadsheetLine]
+
+importJava "org.simantics.spreadsheet.solver.SpreadsheetLine" where
+    data SpreadsheetLine
+    @JavaName "getRow"
+    spreadsheetLineRow :: SpreadsheetLine -> Integer
+    @JavaName "getCells"
+    spreadsheetLineCells :: SpreadsheetLine -> Integer -> Integer -> <Proc> [SpreadsheetCell]
+
+importJava "org.simantics.spreadsheet.solver.SpreadsheetCell" where
+    data SpreadsheetCell
+    @JavaName "getColumn"
+    spreadsheetCellColumn :: SpreadsheetCell -> Integer
+    @JavaName "getContentVariant"
+    spreadsheetCellContentVariant :: SpreadsheetCell -> SpreadsheetBook -> <Proc> Maybe Variant
+
+importJava "java.util.function.Consumer" where
+    data Consumer
+
+importJava "org.simantics.spreadsheet.Spreadsheets" where
+     cellName :: Integer -> Integer -> String
+
+importJava "org.simantics.spreadsheet.OperationMode" where
+    data OperationMode
+    OPERATION :: OperationMode
+    EDIT_MODE :: OperationMode
+
+importJava "org.simantics.spreadsheet.Transaction" where
+    data Transaction
+    commit :: Transaction -> <Proc> ()
+
+effect SpreadsheetTransaction 
+    "spreadsheetTransaction" 
+    "org.simantics.spreadsheet.Transaction"
+
+importJava "org.simantics.spreadsheet.CellEditor" where
+    data CellEditor
+    startTransaction :: CellEditor -> OperationMode -> <Proc> Transaction
+    @JavaName "edit"
+    editProperty_ :: CellEditor -> String -> String -> a -> Binding a -> Maybe Consumer -> <SpreadsheetTransaction> () 
+    @JavaName "edit"
+    editContent :: CellEditor -> String -> Variant -> Maybe Consumer -> <SpreadsheetTransaction> () 
+
index b4a31c316f65a794614ec197e93a7172401472d3..5ac98e084d06777fb50ee6e0ea175aa82fbaa2f5 100644 (file)
@@ -39,6 +39,10 @@ public class Range {
         this.startColumn = copy.startColumn;
         this.endColumn = copy.endColumn;
     }
         this.startColumn = copy.startColumn;
         this.endColumn = copy.endColumn;
     }
+    
+    public static Range parse(String identifier) {
+        return Spreadsheets.decodeCellAbsolute(identifier);
+    }
 
     public static Range combine(Range from, Range to) {
         return new Range(from.startRow, to.endRow, from.startColumn, to.endColumn);
 
     public static Range combine(Range from, Range to) {
         return new Range(from.startRow, to.endRow, from.startColumn, to.endColumn);
diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/BinarySearch.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/BinarySearch.java
new file mode 100644 (file)
index 0000000..e005cab
--- /dev/null
@@ -0,0 +1,9 @@
+package org.simantics.spreadsheet.solver;
+
+public class BinarySearch {
+  public int column;
+  BinarySearch() {}
+  BinarySearch(int column) {
+      this.column = column;
+  }
+}
index 5b732cf864b9f768fd90435ca641fcb3e1e204c8..71566482f1f8e2752ac652a7bd7c22f38f61c781 100644 (file)
@@ -17,7 +17,7 @@ import org.simantics.spreadsheet.solver.formula.SpreadsheetEvaluationEnvironment
 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
 
 @SuppressWarnings("rawtypes")
 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
 
 @SuppressWarnings("rawtypes")
-public class SpreadsheetCell implements SpreadsheetElement, SheetNode {
+public class SpreadsheetCell extends BinarySearch implements SpreadsheetElement, SheetNode {
 
     private static final long serialVersionUID = 6616793596542239339L;
 
 
     private static final long serialVersionUID = 6616793596542239339L;
 
@@ -33,15 +33,14 @@ public class SpreadsheetCell implements SpreadsheetElement, SheetNode {
     private int iterations = 0;
 
     final private SpreadsheetLine line;
     private int iterations = 0;
 
     final private SpreadsheetLine line;
-    final private int column;
     int style;
     int style;
-    private Object content;
+    Object content;
     final private Map<String, SheetNode> properties;
 
     public SpreadsheetCell(SpreadsheetLine line, int column) {
     final private Map<String, SheetNode> properties;
 
     public SpreadsheetCell(SpreadsheetLine line, int column) {
+        super(column);
         this.properties = createProperties();
         this.line = line;
         this.properties = createProperties();
         this.line = line;
-        this.column = column;
     }
 
     //All SpreadsheetCells have these properties - create them when object is created
     }
 
     //All SpreadsheetCells have these properties - create them when object is created
@@ -219,6 +218,24 @@ public class SpreadsheetCell implements SpreadsheetElement, SheetNode {
     public Object getContent() {
         return content;
     }
     public Object getContent() {
         return content;
     }
+    
+    public Variant getContentVariant(SpreadsheetBook book) {
+        
+        try {
+            Object content = evaluate(SpreadsheetEvaluationEnvironment.getInstance(book));
+            if(content == null) return Variant.ofInstance("");
+            if(content instanceof Variant) return (Variant)content;
+            if(content instanceof ExternalRefData) return ((ExternalRefData)content).getContent();
+            else return Variant.ofInstance(content);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            return Variant.ofInstance(t.toString());
+        }
+    }
+    
+    public SpreadsheetLine getLine() {
+        return line;
+    }
 
     public static SpreadsheetCell empty(SpreadsheetLine line, int column) {
         SpreadsheetCell cell =  new SpreadsheetCell(line, column);
 
     public static SpreadsheetCell empty(SpreadsheetLine line, int column) {
         SpreadsheetCell cell =  new SpreadsheetCell(line, column);
index 9ccbc6d1e709511db07a9e4c2e2e8509831863a9..a562b9d62039b25427d3e9c14d9ee7e2f1c85987 100644 (file)
@@ -1,10 +1,12 @@
 package org.simantics.spreadsheet.solver;
 
 package org.simantics.spreadsheet.solver;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Consumer;
 
 import org.simantics.spreadsheet.Range;
 import org.simantics.spreadsheet.SpreadsheetVisitor;
 
 import org.simantics.spreadsheet.Range;
 import org.simantics.spreadsheet.SpreadsheetVisitor;
@@ -96,6 +98,18 @@ public class SpreadsheetEngine implements SpreadsheetElement, SheetNode {
         return root.getLine(row);
     }
 
         return root.getLine(row);
     }
 
+    public void forLines(Consumer<SpreadsheetLine> consumer, int min, int max) {
+        assert(lines.nodes.size() == 1);
+        SpreadsheetLines root = lines.nodes.values().iterator().next();
+        root.forLines(consumer, min, max);
+    }
+    
+    public List<SpreadsheetLine> getLines(int min, int max) {
+        ArrayList<SpreadsheetLine> result = new ArrayList<>();
+        forLines(line -> result.add(line), min, max);
+        return result;
+    }
+
     @Override
     public void accept(SpreadsheetVisitor v) {
         v.visit(this);
     @Override
     public void accept(SpreadsheetVisitor v) {
         v.visit(this);
index d11fdc6d253d417596948ffe5ce18bbc29df358d..43df74c1027fb7f02204a7365a2bf135aaff382e 100644 (file)
@@ -1,11 +1,16 @@
 package org.simantics.spreadsheet.solver;
 
 package org.simantics.spreadsheet.solver;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Consumer;
 
 
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple;
 import org.simantics.spreadsheet.Range;
 import org.simantics.spreadsheet.SpreadsheetVisitor;
 import org.simantics.spreadsheet.Spreadsheets;
 import org.simantics.spreadsheet.Range;
 import org.simantics.spreadsheet.SpreadsheetVisitor;
 import org.simantics.spreadsheet.Spreadsheets;
@@ -36,6 +41,45 @@ public class SpreadsheetLine implements SpreadsheetElement<SpreadsheetCell, Spre
     public SpreadsheetEngine getEngine() {
         return ((SpreadsheetLines)parent).getEngine();
     }
     public SpreadsheetEngine getEngine() {
         return ((SpreadsheetLines)parent).getEngine();
     }
+    
+    public int getRow() {
+        return row;
+    }
+
+    public SpreadsheetLine possibleOffset(int offset) {
+        return getEngine().getLine(row+offset);
+    }
+    
+    public SpreadsheetCell cellAt(int column) {
+        int index = Collections.binarySearch(cells, new BinarySearch(column), new Comparator<BinarySearch>() {
+            @Override
+            public int compare(BinarySearch bs1, BinarySearch bs2) {
+                return Integer.compare(bs1.column, bs2.column);
+            }
+        });
+        if(index >= 0)
+            return cells.get(index);
+        else
+            return null;
+    }
+
+    public String getLinesPath() {
+        return "/" + ((SpreadsheetLines)parent).getLinesPath() + "/" + getName();
+    }
+
+    public void forCells(Consumer<SpreadsheetCell> consumer, int min, int max) {
+        for(int i=min;i<cells.size() && i<max;i++) {
+            SpreadsheetCell cell = cells.get(i);
+            if(SpreadsheetCell.EMPTY == cell) continue;
+            consumer.accept(cell);
+        }
+    }
+
+    public List<SpreadsheetCell> getCells(int min, int max) {
+        ArrayList<SpreadsheetCell> result = new ArrayList<>();
+        forCells(cell -> result.add(cell), min, max);
+        return result;
+    }
 
     @Override
     public String getName() {
 
     @Override
     public String getName() {
index e53d7cb61624109dd53b87f119604c1fd6c982c5..b42e722df53f00d10fd16ed220c9150cacec2b32 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Consumer;
 
 import org.simantics.spreadsheet.SpreadsheetVisitor;
 import org.simantics.spreadsheet.Spreadsheets;
 
 import org.simantics.spreadsheet.SpreadsheetVisitor;
 import org.simantics.spreadsheet.Spreadsheets;
@@ -140,6 +141,35 @@ public class SpreadsheetLines implements SpreadsheetElement<SpreadsheetLine, Spr
 
     }
 
 
     }
 
+    public String getLinesPath() {
+        return getName();
+    }
+    
+    public void forLines(Consumer<SpreadsheetLine> consumer, int min, int max) {
+
+        int i=1;
+        int n = (keys.length - 1) / 2;
+
+        // Smaller keys
+        while(i <= n && min > getKey(i-1)) i++;
+        
+        while(i <= n && max > getKey(i-1)) {
+
+            int nodeName = getChild(i-1);
+            
+            SpreadsheetLines node = nodes.get(nodeName);
+            if(node == null) {
+                consumer.accept(lines.get(-getKey(i-1)));
+            } else {
+                node.forLines(consumer, min, max);
+            }
+            
+            i++;
+            
+        }
+        
+    }
+
     public int getMaxRow() {
         // if keys == null then this is the root of BTree which has only one child 
         if (keys == null) {
     public int getMaxRow() {
         // if keys == null then this is the root of BTree which has only one child 
         if (keys == null) {
index 95c601b503a5bd4718afe31f4a140ff2d312986b..d752479df19bee52cfd074da9074ab29195d51aa 100644 (file)
@@ -23,7 +23,6 @@ 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) {
             @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);
                 LineCommandBuilder builder = context.<LineCommandBuilder>getConcreteCommand();
                 try {
                     LineContentBean valuee = (LineContentBean) value.getValue(LineContentBean.BINDING);
                 LineCommandBuilder builder = context.<LineCommandBuilder>getConcreteCommand();
                 try {
                     LineContentBean valuee = (LineContentBean) value.getValue(LineContentBean.BINDING);