]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ModuleUpdateContext.java
Refactoring of simulator toolkit
[simantics/platform.git] / bundles / org.simantics.structural.synchronization.client / src / org / simantics / structural / synchronization / base / ModuleUpdateContext.java
1 package org.simantics.structural.synchronization.base;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import java.util.ArrayList;
6
7 import org.simantics.databoard.binding.mutable.Variant;
8 import org.simantics.structural.synchronization.utils.ComponentBase;
9 import org.simantics.structural.synchronization.utils.Solver;
10
11 public class ModuleUpdateContext<T extends ComponentBase<T>> {
12
13     private ModuleUpdaterBase<T> updater;
14     private final SynchronizationEventHandlerBase<T> handler;
15     public final T component;
16     public CommandBuilder command;
17     private ArrayList<Runnable> postUpdateActions; 
18     private THashMap<String, Variant> storedProperties;
19     
20     private int pendingRuleCount;
21     public boolean stateLoadedFromUndo;
22
23     public ModuleUpdateContext(SynchronizationEventHandlerBase<T> handler, ModuleUpdaterBase<T> updater, T component) {
24         assert(updater != null);
25         this.handler = handler;
26         this.updater = updater;
27         this.component = component;
28     }
29
30     public void incPendingCount() {
31         ++pendingRuleCount;
32     }
33     
34     public void decPendingCount() {
35         --pendingRuleCount;
36         if(pendingRuleCount == 0) {
37                 try {
38                         command.apply(getSolver());
39                         command = null;
40                 } catch (Exception e) {
41                         handler.reportProblem("Exception while issuing command.", e);
42                 }
43             if(getModuleId() <= 0) {
44                 setModuleId(getSolver().getId(getModuleName()));
45                 component.setModuleId(getModuleId());
46             }
47             if(postUpdateActions != null) {
48                 for(Runnable action : postUpdateActions)
49                     try {
50                         action.run();
51                     } catch(Exception e) {
52                         handler.reportProblem("Post update action failed.", e);
53                     }
54                 postUpdateActions = null;
55             }
56             handler.resolver.unmarkPending(component);
57         }
58     }
59     
60     public void addPostUpdateAction(Runnable action) {
61         if(postUpdateActions == null)
62             postUpdateActions = new ArrayList<Runnable>(2);
63         postUpdateActions.add(action);
64     }
65
66     public int getModuleId() {
67         return component.getModuleId();
68     }
69     
70     public SynchronizationEventHandlerBase<T> getHandler() {
71         return handler;
72     }
73
74     public void setModuleId(int moduleId) {
75         component.setModuleId(moduleId);
76     }
77
78     public Solver getSolver() {
79         return handler.solver;
80     }
81     
82     public <S> S getConcreteSolver() {
83         return handler.solver.getConcreteSolver();
84     }
85
86     public String getModuleType() {
87         return updater != null ? updater.moduleType : null;
88     }
89
90     public String getModuleName() {
91         return component.solverComponentName;
92     }
93
94     public void setModuleName(String moduleName) {
95         component.solverComponentName = moduleName;
96     }
97
98     public void reportProblem(String description) {
99         handler.reportProblem(description);
100        
101     }
102     
103     public void reportProblem(String description, Exception e) {
104         handler.reportProblem(description, e);
105     }
106
107     public void resolveReference(String connectionPoint, ModuleCallback moduleCallback) {
108         handler.resolver.resolveReference(component, connectionPoint, moduleCallback);
109     }
110     
111     public void storeProperty(String name, Variant value) {
112         if(storedProperties == null)
113             storedProperties = new THashMap<String, Variant>();
114         storedProperties.put(name, value);
115     }
116     
117     public Variant getStoredProperty(String name) {
118         if(storedProperties == null)
119             return null;
120         return storedProperties.get(name);
121     }
122
123     public void addPostSynchronizationAction(Runnable action) {
124         handler.addPostSynchronizationAction(action); 
125     }
126     
127     @SuppressWarnings("unchecked")
128         public <C> C getConcreteCommand() {
129         return (C)command.getConcrete();
130     }
131
132     public void setDidChanges() {
133         handler.setDidChanges();
134     }
135     
136     ArrayList<ResynchronizeAction> resynchronizeActions;
137
138     private class ResynchronizeAction {
139         final String connectionPoint;
140         final ModuleCallback callback;
141         
142         public ResynchronizeAction(String connectionPoint,
143                 ModuleCallback callback) {
144             this.connectionPoint = connectionPoint;
145             this.callback = callback;
146         }
147     }
148     
149     public void resynchronize(String connectionPoint, ModuleCallback callback) {
150         if(resynchronizeActions == null) {
151             resynchronizeActions = new ArrayList<ResynchronizeAction>();
152             handler.addPostSynchronizationAction(new Runnable() {
153                 @Override
154                 public void run() {
155                     ArrayList<ResynchronizeAction> resynchronizeActions = ModuleUpdateContext.this.resynchronizeActions;
156                     ModuleUpdateContext.this.resynchronizeActions = null;
157                     
158                     command = updater.createUpdateCommandBuilder(getModuleName());
159                     for(ResynchronizeAction action : resynchronizeActions)
160                         resolveReference(action.connectionPoint, action.callback);
161                     try {
162                         command.apply(getSolver());
163                     } catch (Exception e) {
164                         handler.reportProblem("Exception while issuing command.", e);
165                     }
166                 }
167             });
168         }
169         resynchronizeActions.add(new ResynchronizeAction(connectionPoint, callback));
170     }
171
172 }