]> gerrit.simantics Code Review - simantics/platform.git/blob
b86544c0dfed7a7e42328334622a84d21c568dbb
[simantics/platform.git] /
1 package org.simantics.spreadsheet.graph;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8
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;
26
27 @SuppressWarnings("rawtypes")
28 public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNode, SpreadsheetBook> {
29
30     public SpreadsheetNodeManager(SpreadsheetRealm realm) {
31         super(realm, realm.getEngine());
32         realm.getEngine().registerListener(new SpreadsheetBookListener() {
33             
34             @Override
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));
40                 }
41             }
42         });
43     }
44
45     static final Set<String> COMPONENT_CLASS = Collections.singleton(StructuralResource2.URIs.Component);
46
47     @Override
48     public Set<String> getClassifications(SheetNode node) throws NodeManagerException {
49         checkThreadAccess();
50         if(isRoot(node))
51             return COMPONENT_CLASS;
52         else
53             return Collections.emptySet();
54     }
55
56     @Override
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;
68         } else {
69             return null;
70         }
71     }
72     
73     //Custom setValue logic for SpreadsheetNodeManager - calls the setValueAndFireSelectedListeners
74     @Override
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);
83         }
84     }
85     
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<>();
89         
90         SpreadsheetCell sscell = null;
91         if(node instanceof SpreadsheetCell) {
92                 sscell = (SpreadsheetCell)node;
93         } else if (node instanceof SpreadsheetCellContent) {
94                 sscell = ((SpreadsheetCellContent)node).cell;
95         }
96                         
97         if(sscell != null) {
98                 Set<SpreadsheetCell> result = ((SpreadsheetRealm)super.getRealm()).getEngine().invalidate(sscell);
99                 dirty.addAll(result);
100         }
101                         
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"));
106         }
107         
108         return dirtyNodeContents;
109     }
110     
111     
112     @Override
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);
120         }
121         return variant;
122     }
123     
124 }