1 package org.simantics.structural.synchronization.base;
3 import gnu.trove.map.hash.THashMap;
5 import java.util.ArrayList;
7 import org.simantics.databoard.binding.mutable.Variant;
8 import org.simantics.structural.synchronization.utils.ComponentBase;
9 import org.simantics.structural.synchronization.utils.Solver;
11 public class ModuleUpdateContext<T extends ComponentBase<T>> {
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;
20 private int pendingRuleCount;
21 public boolean stateLoadedFromUndo;
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;
30 public void incPendingCount() {
34 public void decPendingCount() {
36 if(pendingRuleCount == 0) {
38 command.apply(getSolver());
40 } catch (Exception e) {
41 handler.reportProblem("Exception while issuing command.", e);
43 if(getModuleId() <= 0) {
44 setModuleId(getSolver().getId(getModuleName()));
45 component.setModuleId(getModuleId());
47 if(postUpdateActions != null) {
48 for(Runnable action : postUpdateActions)
51 } catch(Exception e) {
52 handler.reportProblem("Post update action failed.", e);
54 postUpdateActions = null;
56 handler.resolver.unmarkPending(component);
60 public void addPostUpdateAction(Runnable action) {
61 if(postUpdateActions == null)
62 postUpdateActions = new ArrayList<Runnable>(2);
63 postUpdateActions.add(action);
66 public int getModuleId() {
67 return component.getModuleId();
70 public SynchronizationEventHandlerBase<T> getHandler() {
74 public void setModuleId(int moduleId) {
75 component.setModuleId(moduleId);
78 public Solver getSolver() {
79 return handler.solver;
82 public <S> S getConcreteSolver() {
83 return handler.solver.getConcreteSolver();
86 public String getModuleType() {
87 return updater != null ? updater.moduleType : null;
90 public String getModuleName() {
91 return component.solverComponentName;
94 public void setModuleName(String moduleName) {
95 component.solverComponentName = moduleName;
98 public void reportProblem(String description) {
99 handler.reportProblem(description);
103 public void reportProblem(String description, Exception e) {
104 handler.reportProblem(description, e);
107 public void resolveReference(String connectionPoint, ModuleCallback moduleCallback) {
108 handler.resolver.resolveReference(component, connectionPoint, moduleCallback);
111 public void storeProperty(String name, Variant value) {
112 if(storedProperties == null)
113 storedProperties = new THashMap<String, Variant>();
114 storedProperties.put(name, value);
117 public Variant getStoredProperty(String name) {
118 if(storedProperties == null)
120 return storedProperties.get(name);
123 public void addPostSynchronizationAction(Runnable action) {
124 handler.addPostSynchronizationAction(action);
127 @SuppressWarnings("unchecked")
128 public <C> C getConcreteCommand() {
129 return (C)command.getConcrete();
132 public void setDidChanges() {
133 handler.setDidChanges();
136 ArrayList<ResynchronizeAction> resynchronizeActions;
138 private class ResynchronizeAction {
139 final String connectionPoint;
140 final ModuleCallback callback;
142 public ResynchronizeAction(String connectionPoint,
143 ModuleCallback callback) {
144 this.connectionPoint = connectionPoint;
145 this.callback = callback;
149 public void resynchronize(String connectionPoint, ModuleCallback callback) {
150 if(resynchronizeActions == null) {
151 resynchronizeActions = new ArrayList<ResynchronizeAction>();
152 handler.addPostSynchronizationAction(new Runnable() {
155 ArrayList<ResynchronizeAction> resynchronizeActions = ModuleUpdateContext.this.resynchronizeActions;
156 ModuleUpdateContext.this.resynchronizeActions = null;
158 command = updater.createUpdateCommandBuilder(getModuleName());
159 for(ResynchronizeAction action : resynchronizeActions)
160 resolveReference(action.connectionPoint, action.callback);
162 command.apply(getSolver());
163 } catch (Exception e) {
164 handler.reportProblem("Exception while issuing command.", e);
169 resynchronizeActions.add(new ResynchronizeAction(connectionPoint, callback));