]> gerrit.simantics Code Review - simantics/platform.git/blob
1615aaf2ba35e8e4cd66c1cd009897e2005988fc
[simantics/platform.git] /
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 getModuleType() {
85         return updater != null ? updater.moduleType : null;
86     }
87
88     public String getModuleName() {
89         return component.solverComponentName;
90     }
91
92     public void setModuleName(String moduleName) {
93         component.solverComponentName = moduleName;
94     }
95
96     public void reportProblem(String description) {
97         handler.reportProblem(description);
98        
99     }
100     
101     public void reportProblem(String description, Exception e) {
102         handler.reportProblem(description, e);
103     }
104
105     public void resolveReference(String connectionPoint, ModuleCallback moduleCallback) {
106         handler.resolver.resolveReference(component, connectionPoint, moduleCallback);
107     }
108     
109     public void storeProperty(String name, Variant value) {
110         if(storedProperties == null)
111             storedProperties = new THashMap<String, Variant>();
112         storedProperties.put(name, value);
113     }
114     
115     public Variant getStoredProperty(String name) {
116         if(storedProperties == null)
117             return null;
118         return storedProperties.get(name);
119     }
120
121     public void addPostSynchronizationAction(Runnable action) {
122         handler.addPostSynchronizationAction(action); 
123     }
124     
125     @SuppressWarnings("unchecked")
126         public <C> C getConcreteCommand() {
127         return (C)command.getConcrete();
128     }
129
130     public void setDidChanges() {
131         handler.setDidChanges();
132     }
133     
134     ArrayList<ResynchronizeAction> resynchronizeActions;
135
136     private class ResynchronizeAction {
137         final String connectionPoint;
138         final ModuleCallback callback;
139         
140         public ResynchronizeAction(String connectionPoint,
141                 ModuleCallback callback) {
142             this.connectionPoint = connectionPoint;
143             this.callback = callback;
144         }
145     }
146     
147     public void resynchronize(String connectionPoint, ModuleCallback callback) {
148         if(resynchronizeActions == null) {
149             resynchronizeActions = new ArrayList<ResynchronizeAction>();
150             handler.addPostSynchronizationAction(new Runnable() {
151                 @Override
152                 public void run() {
153                     ArrayList<ResynchronizeAction> resynchronizeActions = ModuleUpdateContext.this.resynchronizeActions;
154                     ModuleUpdateContext.this.resynchronizeActions = null;
155                     
156                     command = updater.createUpdateCommandBuilder(getModuleName());
157                     for(ResynchronizeAction action : resynchronizeActions)
158                         resolveReference(action.connectionPoint, action.callback);
159                     try {
160                         command.apply(getSolver());
161                     } catch (Exception e) {
162                         handler.reportProblem("Exception while issuing command.", e);
163                     }
164                 }
165             });
166         }
167         resynchronizeActions.add(new ResynchronizeAction(connectionPoint, callback));
168     }
169
170 }