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.SpreadsheetSCLConstant;
25 import org.simantics.spreadsheet.solver.SpreadsheetTypeNode;
26 import org.simantics.structural.stubs.StructuralResource2;
28 @SuppressWarnings("rawtypes")
29 public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNode, SpreadsheetBook> {
31 public SpreadsheetNodeManager(SpreadsheetRealm realm) {
32 super(realm, realm.getEngine());
33 new Exception().printStackTrace();
34 realm.getEngine().registerListener(new SpreadsheetBookListener() {
37 public void cellsChanged(Collection<SpreadsheetCell> cells) {
38 realm.asyncExec(new Runnable() {
41 for(SpreadsheetCell cell : cells) {
42 System.err.println("Modification in cell " + cell.getName());
43 refreshVariable(new SpreadsheetCellContent(cell));
44 Object content = cell.getContent();
45 if(content instanceof SpreadsheetFormula || content instanceof SpreadsheetSCLConstant)
46 refreshVariable(new SpreadsheetCellContentExpression(cell));
54 static final Set<String> COMPONENT_CLASS = Collections.singleton(StructuralResource2.URIs.Component);
57 public Set<String> getClassifications(SheetNode node) throws NodeManagerException {
60 return COMPONENT_CLASS;
62 return Collections.emptySet();
66 public String getPropertyURI(SheetNode parent, SheetNode property) {
67 if(property instanceof SpreadsheetCellContent) {
68 return SpreadsheetResource.URIs.Cell_content;
69 } else if(property instanceof SpreadsheetTypeNode) {
70 return Layer0.URIs.typeURI;
71 } else if(property instanceof SpreadsheetCellContentExpression) {
72 return Layer0.URIs.SCLValue_expression;
73 } else if (property instanceof SpreadsheetCellStyle) {
74 return SpreadsheetResource.URIs.Cell_style;
75 } else if (property instanceof SpreadsheetCellEditable){
76 return SpreadsheetResource.URIs.Cell_editable;
82 //Custom setValue logic for SpreadsheetNodeManager - calls the setValueAndFireSelectedListeners
84 public void setValue(SheetNode node, Object value, Binding binding) throws NodeManagerException {
85 Set<SheetNode> dirtyNodeContents = findDirtyNodeContents(node);
86 super.setValueAndFireSelectedListeners(node, value, binding, dirtyNodeContents);
87 if(value instanceof SpreadsheetFormula) {
88 SpreadsheetCellContent scc = (SpreadsheetCellContent)node;
89 SpreadsheetCellContentExpression scce = new SpreadsheetCellContentExpression(scc.cell);
90 // We need to also refresh the expression variable in this case
91 refreshVariable(scce);
95 //Find the cells that are used by this cell and their SpreadsheetContents, so that they can be marked as dirty later
96 public Set<SheetNode> findDirtyNodeContents(SheetNode node){
97 Set<SheetNode> dirty = new HashSet<>();
99 SpreadsheetCell sscell = null;
100 if(node instanceof SpreadsheetCell) {
101 sscell = (SpreadsheetCell)node;
102 } else if (node instanceof SpreadsheetCellContent) {
103 sscell = ((SpreadsheetCellContent)node).cell;
107 Set<SpreadsheetCell> result = ((SpreadsheetRealm)super.getRealm()).getEngine().invalidate(sscell);
108 dirty.addAll(result);
111 Set<SheetNode> dirtyNodeContents = new HashSet<>();
112 for(SheetNode cell : dirty) {
113 Map<String, SheetNode> properties = cell.getProperties();
114 dirtyNodeContents.add((SpreadsheetCellContent)properties.get("content"));
117 return dirtyNodeContents;
122 protected Variant getEngineVariantOrCached(SheetNode node) throws NodeManagerException {
123 Variant variant = valueCache.get(node);
124 if(variant == null) {
125 Object value = realm.getEngine().getEngineValue(node);
126 Binding binding = realm.getEngine().getEngineBinding(node);
127 variant = new Variant(binding, value);
128 valueCache.put(node, variant);