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;
9 public class ModuleUpdateContext<T extends ComponentBase<T>> {
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;
18 private int pendingRuleCount;
19 public boolean stateLoadedFromUndo;
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;
28 public void incPendingCount() {
32 public void decPendingCount() {
34 if(pendingRuleCount == 0) {
36 command.apply(getSolver());
38 } catch (Exception e) {
39 handler.reportProblem("Exception while issuing command.", e);
41 if(getModuleId() <= 0) {
42 setModuleId(getSolver().getId(getModuleName()));
43 component.setModuleId(getModuleId());
45 if(postUpdateActions != null) {
46 for(Runnable action : postUpdateActions)
49 } catch(Exception e) {
50 handler.reportProblem("Post update action failed.", e);
52 postUpdateActions = null;
54 handler.resolver.unmarkPending(component);
58 public void addPostUpdateAction(Runnable action) {
59 if(postUpdateActions == null)
60 postUpdateActions = new ArrayList<Runnable>(2);
61 postUpdateActions.add(action);
64 public int getModuleId() {
65 return component.getModuleId();
68 public SynchronizationEventHandlerBase<T> getHandler() {
72 public void setModuleId(int moduleId) {
73 component.setModuleId(moduleId);
76 public Solver getSolver() {
77 return handler.solver;
80 public <S> S getConcreteSolver() {
81 return handler.solver.getConcreteSolver();
84 public String getModuleType() {
85 return updater != null ? updater.moduleType : null;
88 public String getModuleName() {
89 return component.solverComponentName;
92 public void setModuleName(String moduleName) {
93 component.solverComponentName = moduleName;
96 public void reportProblem(String description) {
97 handler.reportProblem(description);
101 public void reportProblem(String description, Exception e) {
102 handler.reportProblem(description, e);
105 public void resolveReference(String connectionPoint, ModuleCallback moduleCallback) {
106 handler.resolver.resolveReference(component, connectionPoint, moduleCallback);
109 public void storeProperty(String name, Variant value) {
110 if(storedProperties == null)
111 storedProperties = new THashMap<String, Variant>();
112 storedProperties.put(name, value);
115 public Variant getStoredProperty(String name) {
116 if(storedProperties == null)
118 return storedProperties.get(name);
121 public void addPostSynchronizationAction(Runnable action) {
122 handler.addPostSynchronizationAction(action);
125 @SuppressWarnings("unchecked")
126 public <C> C getConcreteCommand() {
127 return (C)command.getConcrete();
130 public void setDidChanges() {
131 handler.setDidChanges();
134 ArrayList<ResynchronizeAction> resynchronizeActions;
136 private class ResynchronizeAction {
137 final String connectionPoint;
138 final ModuleCallback callback;
140 public ResynchronizeAction(String connectionPoint,
141 ModuleCallback callback) {
142 this.connectionPoint = connectionPoint;
143 this.callback = callback;
147 public void resynchronize(String connectionPoint, ModuleCallback callback) {
148 if(resynchronizeActions == null) {
149 resynchronizeActions = new ArrayList<ResynchronizeAction>();
150 handler.addPostSynchronizationAction(new Runnable() {
153 ArrayList<ResynchronizeAction> resynchronizeActions = ModuleUpdateContext.this.resynchronizeActions;
154 ModuleUpdateContext.this.resynchronizeActions = null;
156 command = updater.createUpdateCommandBuilder(getModuleName());
157 for(ResynchronizeAction action : resynchronizeActions)
158 resolveReference(action.connectionPoint, action.callback);
160 command.apply(getSolver());
161 } catch (Exception e) {
162 handler.reportProblem("Exception while issuing command.", e);
167 resynchronizeActions.add(new ResynchronizeAction(connectionPoint, callback));