X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FSpreadsheetGraphUtils.java;h=a2d2141f797403ac773028f0fd3a8ef83e106dd6;hb=38d133f2a39bab76deed5d047fbabee4479b5373;hp=35a05d7aa59c7d45980f0a781a8fdd726ea621e3;hpb=e3446802a81064942a7cbfea0c577e21ae5ae658;p=simantics%2Fplatform.git 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 35a05d7aa..a2d2141f7 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 @@ -33,13 +33,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 +244,31 @@ public class SpreadsheetGraphUtils { } - + public static void forRows(ReadGraph graph, Variable run, String sheetName, int min, int max, Function1 fn) throws DatabaseException { + + String sessionName = run.getParent(graph).getURI(graph); + StandardRealm 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 possibleConfigurationCellVariables(ReadGraph graph, Variable sheet, Range range) throws DatabaseException { List rowVariables = possibleConfigurationLineVariables(graph, sheet, range); List result = new ArrayList<>(); @@ -651,4 +676,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 = ((StandardGraphChildVariable)variable).node; + if(node != null) { + if(node.node instanceof SpreadsheetBook) + return (SpreadsheetBook) node.node; + } + } + return null; + } + }