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