X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.structural.synchronization.client%2Fsrc%2Forg%2Fsimantics%2Fstructural%2Fsynchronization%2Fbase%2FUpdateComponentUids.java;h=5ace8a2c345374035754e3ca8fa90de8d24a51ef;hp=840d9ec4c7dc5bd688f382939d04734e8c1fa998;hb=a2a42428426818a7498d5a1705603fb8d3a8a95a;hpb=53059ca1a958697cc6235d27628614fbaa944d59;ds=sidebyside diff --git a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java index 840d9ec4c..5ace8a2c3 100644 --- a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java +++ b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java @@ -1,6 +1,8 @@ package org.simantics.structural.synchronization.base; +import org.eclipse.core.runtime.IProgressMonitor; import org.simantics.db.ReadGraph; +import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; @@ -11,17 +13,67 @@ import org.simantics.db.layer0.variable.Variable; * and resource ids have been changed. * * @author Hannu Niemistö + * @author Tuukka Lehtonen */ -public class UpdateComponentUids { - - public static > void update(ReadGraph graph, T component, Variable variable) throws DatabaseException { +public class UpdateComponentUids> { + + public static interface ComponentUpdateProgressMonitor { + void componentsDone(int components, int componentCount); + } + + private IProgressMonitor monitor; + private ComponentUpdateProgressMonitor componentMonitor; + private ReadGraph graph; + private int componentCount; + private int counter; + + private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) { + this.monitor = monitor; + this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null; + this.graph = graph; + this.componentCount = componentCount; + } + + private void update(T component, Variable variable) throws DatabaseException { component.uid = variable.getRVI(graph).toString(); + + // Handle progress monitoring and cancellation + counter++; + if (monitor != null) { + if (componentMonitor != null) + componentMonitor.componentsDone(counter, componentCount); + if (monitor.isCanceled()) + throw new CancelTransactionException(); + } + for(Variable childVariable : variable.getChildren(graph)) { String childName = childVariable.getName(graph); T childComponent = component.getChild(childName); if(childComponent != null) - update(graph, childComponent, childVariable); + update(childComponent, childVariable); } } - + + public static > void update(ReadGraph graph, T component, Variable variable) throws DatabaseException { + update(null, graph, component, variable); + } + + public static > void update(IProgressMonitor monitor, ReadGraph graph, T component, Variable variable) throws DatabaseException { + new UpdateComponentUids(monitor, graph, countComponents(component)).update(component, variable); + } + + public static > int countComponents(T component) { + return new Counter().count(component); + } + + private static class Counter> { + int counter; + int count(T component) { + ++counter; + for (T child : component.getChildren()) + count(child); + return counter; + } + } + }