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 getModuleName() {
85 return component.solverComponentName;
88 public void setModuleName(String moduleName) {
89 component.solverComponentName = moduleName;
92 public void reportProblem(String description) {
93 handler.reportProblem(description);
97 public void reportProblem(String description, Exception e) {
98 handler.reportProblem(description, e);
101 public void resolveReference(String connectionPoint, ModuleCallback moduleCallback) {
102 handler.resolver.resolveReference(component, connectionPoint, moduleCallback);
105 public void storeProperty(String name, Variant value) {
106 if(storedProperties == null)
107 storedProperties = new THashMap<String, Variant>();
108 storedProperties.put(name, value);
111 public Variant getStoredProperty(String name) {
112 if(storedProperties == null)
114 return storedProperties.get(name);
117 public void addPostSynchronizationAction(Runnable action) {
118 handler.addPostSynchronizationAction(action);
121 @SuppressWarnings("unchecked")
122 public <C> C getConcreteCommand() {
123 return (C)command.getConcrete();
126 public void setDidChanges() {
127 handler.setDidChanges();
130 ArrayList<ResynchronizeAction> resynchronizeActions;
132 private class ResynchronizeAction {
133 final String connectionPoint;
134 final ModuleCallback callback;
136 public ResynchronizeAction(String connectionPoint,
137 ModuleCallback callback) {
138 this.connectionPoint = connectionPoint;
139 this.callback = callback;
143 public void resynchronize(String connectionPoint, ModuleCallback callback) {
144 if(resynchronizeActions == null) {
145 resynchronizeActions = new ArrayList<ResynchronizeAction>();
146 handler.addPostSynchronizationAction(new Runnable() {
149 ArrayList<ResynchronizeAction> resynchronizeActions = ModuleUpdateContext.this.resynchronizeActions;
150 ModuleUpdateContext.this.resynchronizeActions = null;
152 command = updater.createUpdateCommandBuilder(getModuleName());
153 for(ResynchronizeAction action : resynchronizeActions)
154 resolveReference(action.connectionPoint, action.callback);
156 command.apply(getSolver());
157 } catch (Exception e) {
158 handler.reportProblem("Exception while issuing command.", e);
163 resynchronizeActions.add(new ResynchronizeAction(connectionPoint, callback));