]> gerrit.simantics Code Review - simantics/platform.git/blob
a7bee9d4b5e0daf0af55359e3482cf5616af06f1
[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.SpreadsheetSCLConstant;
25 import org.simantics.spreadsheet.solver.SpreadsheetTypeNode;
26 import org.simantics.structural.stubs.StructuralResource2;
27
28 @SuppressWarnings("rawtypes")
29 public class SpreadsheetNodeManager extends StandardVariableNodeManager<SheetNode, SpreadsheetBook> {
30
31     public SpreadsheetNodeManager(SpreadsheetRealm realm) {
32         super(realm, realm.getEngine());
33         new Exception().printStackTrace();
34         realm.getEngine().registerListener(new SpreadsheetBookListener() {
35             
36             @Override
37             public void cellsChanged(Collection<SpreadsheetCell> cells) {
38                 realm.asyncExec(new Runnable() {
39                     @Override
40                     public void run() {
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));
47                         }
48                     }
49                 });
50             }
51         });
52     }
53
54     static final Set<String> COMPONENT_CLASS = Collections.singleton(StructuralResource2.URIs.Component);
55
56     @Override
57     public Set<String> getClassifications(SheetNode node) throws NodeManagerException {
58         checkThreadAccess();
59         if(isRoot(node))
60             return COMPONENT_CLASS;
61         else
62             return Collections.emptySet();
63     }
64
65     @Override
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;
77         } else {
78             return null;
79         }
80     }
81     
82     //Custom setValue logic for SpreadsheetNodeManager - calls the setValueAndFireSelectedListeners
83     @Override
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);
92         }
93     }
94     
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<>();
98         
99         SpreadsheetCell sscell = null;
100         if(node instanceof SpreadsheetCell) {
101                 sscell = (SpreadsheetCell)node;
102         } else if (node instanceof SpreadsheetCellContent) {
103                 sscell = ((SpreadsheetCellContent)node).cell;
104         }
105                         
106         if(sscell != null) {
107                 Set<SpreadsheetCell> result = ((SpreadsheetRealm)super.getRealm()).getEngine().invalidate(sscell);
108                 dirty.addAll(result);
109         }
110                         
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"));
115         }
116         
117         return dirtyNodeContents;
118     }
119     
120     
121     @Override
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);
129         }
130         return variant;
131     }
132     
133 }