X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.structural.synchronization%2Fsrc%2Forg%2Fsimantics%2Fstructural%2Fsynchronization%2Fbase%2FSynchronizationEventHandlerBase.java;h=5f8334398fe9ba99d896b9798ef195f20c5c059a;hb=HEAD;hp=a6efeefb2534bc402c25cfc671705ccbb6ad0cbb;hpb=31a36bcdcd31bedc2e5106dba68ae057ba9c1875;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/base/SynchronizationEventHandlerBase.java b/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/base/SynchronizationEventHandlerBase.java index a6efeefb2..5f8334398 100644 --- a/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/base/SynchronizationEventHandlerBase.java +++ b/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/base/SynchronizationEventHandlerBase.java @@ -56,6 +56,13 @@ public abstract class SynchronizationEventHandlerBase */ THashSet potentiallyUpdatedComponents = new THashSet<>(); + /** + * Is this potentially an undo/redo-related synchronization? + * + * Default is true for backwards compatibility. + */ + protected boolean isUndo = true; + public SynchronizationEventHandlerBase(Solver solver, ReferenceResolverBase resolver, SolverNameUtil nameUtil, ComponentFactory componentFactory, ModuleUpdaterFactoryBase moduleUpdaterFactory, MappingBase mapping) { this.solver = solver; @@ -66,6 +73,19 @@ public abstract class SynchronizationEventHandlerBase this.resolver = resolver; } + /** + * Mark the undo/redo status of this handler. + * + * Set 'isUndo' to false when processing a normal synchronization and true when + * processing an undo/redo. + * + * When 'isUndo' is false, loading of component solver state from the state undo context + * is skipped for added components. + */ + public void setAsUndo(boolean isUndo) { + this.isUndo = isUndo; + } + @Override public void beginSynchronization() { if(TRACE_EVENTS) { @@ -201,10 +221,14 @@ public abstract class SynchronizationEventHandlerBase // TODO these two lines can be removed when IncludedInSimulation -property is given to all composites if(component.getParent() != null) { - String parentName = solver.getName(component.getParent().getModuleId()); - solver.includeSubprocess(parentName, subprocessName); - component.solverComponentName = subprocessName; - solverComponentNameToComponent.put(subprocessName, component); + int nearestParentId = getNearestParentComponentId(component); + if (nearestParentId <= 0) { + throw new SynchronizationException("Could not find parent with non-zero component id from Component("+name+", " + (component != null ? component.uid : "null") + "," + typeId + ")"); + } + String parentName = solver.getName(nearestParentId); + solver.includeSubprocess(parentName, subprocessName); + component.solverComponentName = subprocessName; + solverComponentNameToComponent.put(subprocessName, component); } if(updater.isComposite) { @@ -248,7 +272,7 @@ public abstract class SynchronizationEventHandlerBase if(oldChildMap != null) for(T component : oldChildMap.values()) { component.clearParent(); - mapping.addPendingRemoval(component); + addPendingRemoval(component); } } // Alternative implementation when uids are not available. @@ -275,7 +299,7 @@ public abstract class SynchronizationEventHandlerBase if(oldChildMap != null) for(T component : oldChildMap.values()) { component.clearParent(); - mapping.addPendingRemoval(component); + addPendingRemoval(component); } } @@ -307,14 +331,16 @@ public abstract class SynchronizationEventHandlerBase else { component.attached = false; context.setModuleName(nameUtil.getFreshName(parentSolverComponentName, name)); - context.addPostUpdateAction(new Runnable() { - @Override - public void run() { - context.stateLoadedFromUndo = mapping.undoContext.loadState(solver, - context.component.componentId, - context.component.uid); - } - }); + if (isUndo) { + context.addPostUpdateAction(new Runnable() { + @Override + public void run() { + context.stateLoadedFromUndo = mapping.undoContext.loadState(solver, + context.component.componentId, + context.component.uid); + } + }); + } updater.create(context, properties, connections); solverComponentNameToComponent.put(context.getModuleName(), component); } @@ -334,6 +360,23 @@ public abstract class SynchronizationEventHandlerBase } } + private static int getNearestParentComponentId(ComponentBase component) { + ComponentBase parent = component.getParent(); + while (parent != null) { + int pid = parent.getModuleId(); + if (pid > 0) + return pid; + parent = parent.getParent(); + } + return -1; + } + + protected void addPendingRemoval(T component) { + if (TRACE_EVENTS) + System.out.println("addPendingRemoval(" + component.componentId + " : " + component.solverComponentName + ")"); + mapping.addPendingRemoval(component); + } + private String getSubprocessName(String name, Collection properties) { for(SerializedVariable property : properties)