X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FSpreadsheetGraphUtils.java;h=3938e478b923d768220952d44301eedb449388f1;hp=35a05d7aa59c7d45980f0a781a8fdd726ea621e3;hb=c9a552af1020b5e6d4cf0da0a00bd758de772c2a;hpb=526f4a68350df5a272586f7d794603ecb4525132 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..3938e478b 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 @@ -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 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 +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 = ((StandardGraphChildVariable)variable).node; + if(node != null) { + if(node.node instanceof SpreadsheetBook) + return (SpreadsheetBook) node.node; + } + } + return null; + } + }