1 package org.simantics.spreadsheet.graph;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.simulator.toolkit.db.StandardVariableNodeManager;
13 import org.simantics.simulator.variable.exceptions.NodeManagerException;
14 import org.simantics.spreadsheet.SpreadsheetCellStyle;
15 import org.simantics.spreadsheet.resource.SpreadsheetResource;
16 import org.simantics.spreadsheet.solver.SheetNode;
17 import org.simantics.spreadsheet.solver.SpreadsheetBook;
18 import org.simantics.spreadsheet.solver.SpreadsheetBook.SpreadsheetBookListener;
19 import org.simantics.spreadsheet.solver.SpreadsheetCell;
20 import org.simantics.spreadsheet.solver.SpreadsheetCellContent;
21 import org.simantics.spreadsheet.solver.SpreadsheetCellContentExpression;
22 import org.simantics.spreadsheet.solver.SpreadsheetCellEditable;
23 import org.simantics.spreadsheet.solver.SpreadsheetFormula;
24 import org.simantics.spreadsheet.solver.SpreadsheetTypeNode;
25 import org.simantics.structural.stubs.StructuralResource2;
27 @SuppressWarnings("rawtypes")
28 public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNode, SpreadsheetBook> {
30 public SpreadsheetNodeManager(SpreadsheetRealm realm) {
31 super(realm, realm.getEngine());
32 realm.getEngine().registerListener(new SpreadsheetBookListener() {
35 public void cellsChanged(Collection<SpreadsheetCell> cells) {
36 for(SpreadsheetCell cell : cells) {
37 System.err.println("Modification in cell " + cell);
38 refreshVariable(new SpreadsheetCellContent(cell));
39 refreshVariable(new SpreadsheetCellContentExpression(cell));
45 static final Set<String> COMPONENT_CLASS = Collections.singleton(StructuralResource2.URIs.Component);
48 public Set<String> getClassifications(SheetNode node) throws NodeManagerException {
51 return COMPONENT_CLASS;
53 return Collections.emptySet();
57 public String getPropertyURI(SheetNode parent, SheetNode property) {
58 if(property instanceof SpreadsheetCellContent) {
59 return SpreadsheetResource.URIs.Cell_content;
60 } else if(property instanceof SpreadsheetTypeNode) {
61 return Layer0.URIs.typeURI;
62 } else if(property instanceof SpreadsheetCellContentExpression) {
63 return Layer0.URIs.SCLValue_expression;
64 } else if (property instanceof SpreadsheetCellStyle) {
65 return SpreadsheetResource.URIs.Cell_style;
66 } else if (property instanceof SpreadsheetCellEditable){
67 return SpreadsheetResource.URIs.Cell_editable;
73 //Custom setValue logic for SpreadsheetNodeManager - calls the setValueAndFireSelectedListeners
75 public void setValue(SheetNode node, Object value, Binding binding) throws NodeManagerException {
76 Set<SheetNode> dirtyNodeContents = findDirtyNodeContents(node);
77 super.setValueAndFireSelectedListeners(node, value, binding, dirtyNodeContents);
78 if(value instanceof SpreadsheetFormula) {
79 SpreadsheetCellContent scc = (SpreadsheetCellContent)node;
80 SpreadsheetCellContentExpression scce = new SpreadsheetCellContentExpression(scc.cell);
81 // We need to also refresh the expression variable in this case
82 refreshVariable(scce);
86 //Find the cells that are used by this cell and their SpreadsheetContents, so that they can be marked as dirty later
87 public Set<SheetNode> findDirtyNodeContents(SheetNode node){
88 Set<SheetNode> dirty = new HashSet<>();
90 SpreadsheetCell sscell = null;
91 if(node instanceof SpreadsheetCell) {
92 sscell = (SpreadsheetCell)node;
93 } else if (node instanceof SpreadsheetCellContent) {
94 sscell = ((SpreadsheetCellContent)node).cell;
98 Set<SpreadsheetCell> result = ((SpreadsheetRealm)super.getRealm()).getEngine().invalidate(sscell);
102 Set<SheetNode> dirtyNodeContents = new HashSet<>();
103 for(SheetNode cell : dirty) {
104 Map<String, SheetNode> properties = cell.getProperties();
105 dirtyNodeContents.add((SpreadsheetCellContent)properties.get("content"));
108 return dirtyNodeContents;
113 protected Variant getEngineVariantOrCached(SheetNode node) throws NodeManagerException {
114 Variant variant = valueCache.get(node);
115 if(variant == null) {
116 Object value = realm.getEngine().getEngineValue(node);
117 Binding binding = realm.getEngine().getEngineBinding(node);
118 variant = new Variant(binding, value);
119 valueCache.put(node, variant);