X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fadapter%2FSpreadsheetVariable.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fadapter%2FSpreadsheetVariable.java;h=0244d5c07949fc897b01f9fc37f7ddf3dbe675a1;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=016209a7ea4efb58077b8c34b3b7156be6d688b8;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/SpreadsheetVariable.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/SpreadsheetVariable.java index 016209a7e..0244d5c07 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/SpreadsheetVariable.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/SpreadsheetVariable.java @@ -1,117 +1,117 @@ -package org.simantics.spreadsheet.graph.adapter; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.variable.ConstantChildVariable; -import org.simantics.db.layer0.variable.ExtendedGraphChildVariable; -import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.layer0.variable.VariableSpaceManipulator; -import org.simantics.db.layer0.variable.Variables; -import org.simantics.layer0.Layer0; -import org.simantics.spreadsheet.Range; -import org.simantics.spreadsheet.SheetVariables; -import org.simantics.spreadsheet.common.matrix.VariantMatrix; -import org.simantics.spreadsheet.util.SpreadsheetUtils; - -public class SpreadsheetVariable extends ExtendedGraphChildVariable { - -// final private Collection> creators = new ArrayList>(); - - public SpreadsheetVariable(Variable parent, Resource resource) throws DatabaseException { - - super(parent, resource); - -// creators.add(new Pair(Parsers.MATRIX_PARSER, new MatrixCellCreator(resource))); -// creators.add(new Pair(Parsers.EXPRESSION_PARSER, new ExpressionCellCreator(resource))); -// creators.add(new Pair(Parsers.COMMAND_PARSER, new CommandCellCreator(resource))); -// creators.add(new Pair(Parsers.RESOURCE_ARRAY_PARSER, new ResourceArrayCellCreator(resource))); -// creators.add(new Pair(Parsers.TEXT_PARSER, new TextCellCreator(resource))); - - } - - @SuppressWarnings("unchecked") - protected T tryAdapt(ReadGraph graph, Class clazz) throws DatabaseException { - if(VariableSpaceManipulator.class == clazz) { - - return (T)new VariableSpaceManipulator() { - - @Override - public void apply(WriteGraph graph, Modification modification) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - for(String name : modification.removedChildren) { - Variable child = getChild(graph, name); - Resource represents = child.getRepresents(graph); - graph.deny(represents, L0.PartOf); - } - - for(ChildCreationData data : modification.newChildren) { - Resource container = getRepresents(graph); - Resource cell = graph.newResource(); - Resource type = graph.getResource(data.type); - graph.claim(cell, L0.InstanceOf, null, type); - graph.addLiteral(cell, L0.HasName, L0.NameOf, L0.String, data.name, Bindings.STRING); - for(PropertyCreationData p : data.properties) { - Resource property = graph.getResource(p.name); - graph.claimLiteral(cell, property, p.value.getValue()); - } - graph.claim(cell, L0.PartOf, container); - - } - - } - - }; - - } - return null; - } - - @Override - public T adapt(ReadGraph graph, Class clazz) throws DatabaseException { - T t = tryAdapt(graph, clazz); - return t != null ? t : super.adapt(graph, clazz); - } - - @Override - public T adaptPossible(ReadGraph graph, Class clazz) throws DatabaseException { - T t = tryAdapt(graph, clazz); - return t != null ? t : super.adaptPossible(graph, clazz); - } - - final String[] propertyNames = { SheetVariables.CONTENT, Variables.LABEL, "immutable" }; - final Binding[] bindings = { Bindings.VARIANT, Bindings.STRING, Bindings.BOOLEAN}; - - @Override - public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException { - if(name.contains(":")) { - Range range = SpreadsheetUtils.decodeRange(name, 0, 0); - - VariantMatrix matrix = new VariantMatrix(range.height(),range.width()); - - for(int x=range.startColumn;x<=range.endColumn;x++) { - for(int y=range.startRow;y<=range.endRow;y++) { - String location = SpreadsheetUtils.cellName(y,x); - Variable child = getPossibleChild(graph, location); - Variant value = null; - if(child != null) - value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT); - matrix.set(y-range.startRow, x-range.startColumn, value); - } - } - - return new ConstantChildVariable(this, name, propertyNames, bindings, new Object[] { - Variant.ofInstance(matrix), null, name, false - }); - - } else { - return super.getPossibleSpecialChild(graph, name); - } - } - -} +package org.simantics.spreadsheet.graph.adapter; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.ConstantChildVariable; +import org.simantics.db.layer0.variable.ExtendedGraphChildVariable; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.VariableSpaceManipulator; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; +import org.simantics.spreadsheet.Range; +import org.simantics.spreadsheet.SheetVariables; +import org.simantics.spreadsheet.common.matrix.VariantMatrix; +import org.simantics.spreadsheet.util.SpreadsheetUtils; + +public class SpreadsheetVariable extends ExtendedGraphChildVariable { + +// final private Collection> creators = new ArrayList>(); + + public SpreadsheetVariable(Variable parent, Resource resource) throws DatabaseException { + + super(parent, resource); + +// creators.add(new Pair(Parsers.MATRIX_PARSER, new MatrixCellCreator(resource))); +// creators.add(new Pair(Parsers.EXPRESSION_PARSER, new ExpressionCellCreator(resource))); +// creators.add(new Pair(Parsers.COMMAND_PARSER, new CommandCellCreator(resource))); +// creators.add(new Pair(Parsers.RESOURCE_ARRAY_PARSER, new ResourceArrayCellCreator(resource))); +// creators.add(new Pair(Parsers.TEXT_PARSER, new TextCellCreator(resource))); + + } + + @SuppressWarnings("unchecked") + protected T tryAdapt(ReadGraph graph, Class clazz) throws DatabaseException { + if(VariableSpaceManipulator.class == clazz) { + + return (T)new VariableSpaceManipulator() { + + @Override + public void apply(WriteGraph graph, Modification modification) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + for(String name : modification.removedChildren) { + Variable child = getChild(graph, name); + Resource represents = child.getRepresents(graph); + graph.deny(represents, L0.PartOf); + } + + for(ChildCreationData data : modification.newChildren) { + Resource container = getRepresents(graph); + Resource cell = graph.newResource(); + Resource type = graph.getResource(data.type); + graph.claim(cell, L0.InstanceOf, null, type); + graph.addLiteral(cell, L0.HasName, L0.NameOf, L0.String, data.name, Bindings.STRING); + for(PropertyCreationData p : data.properties) { + Resource property = graph.getResource(p.name); + graph.claimLiteral(cell, property, p.value.getValue()); + } + graph.claim(cell, L0.PartOf, container); + + } + + } + + }; + + } + return null; + } + + @Override + public T adapt(ReadGraph graph, Class clazz) throws DatabaseException { + T t = tryAdapt(graph, clazz); + return t != null ? t : super.adapt(graph, clazz); + } + + @Override + public T adaptPossible(ReadGraph graph, Class clazz) throws DatabaseException { + T t = tryAdapt(graph, clazz); + return t != null ? t : super.adaptPossible(graph, clazz); + } + + final String[] propertyNames = { SheetVariables.CONTENT, Variables.LABEL, "immutable" }; + final Binding[] bindings = { Bindings.VARIANT, Bindings.STRING, Bindings.BOOLEAN}; + + @Override + public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException { + if(name.contains(":")) { + Range range = SpreadsheetUtils.decodeRange(name, 0, 0); + + VariantMatrix matrix = new VariantMatrix(range.height(),range.width()); + + for(int x=range.startColumn;x<=range.endColumn;x++) { + for(int y=range.startRow;y<=range.endRow;y++) { + String location = SpreadsheetUtils.cellName(y,x); + Variable child = getPossibleChild(graph, location); + Variant value = null; + if(child != null) + value = child.getPossiblePropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT); + matrix.set(y-range.startRow, x-range.startColumn, value); + } + } + + return new ConstantChildVariable(this, name, propertyNames, bindings, new Object[] { + Variant.ofInstance(matrix), null, name, false + }); + + } else { + return super.getPossibleSpecialChild(graph, name); + } + } + +}