X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.db%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fdb%2FTGStatusMonitor.java;h=54d65ce98f307f2e6a4dd2a95f881837930295d5;hp=06e0adbedb659f31fe4e1092c28ef8d9643128b9;hb=32a17a670cf4f9d459917495be5f4a504afac205;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGStatusMonitor.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGStatusMonitor.java index 06e0adbed..54d65ce98 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGStatusMonitor.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGStatusMonitor.java @@ -1,47 +1,52 @@ -package org.simantics.graph.db; - -public interface TGStatusMonitor { - - void status(int percentage); - boolean isCanceled(); - - public static class NullMonitor implements TGStatusMonitor { - @Override - public void status(int percentage) { - } - @Override - public boolean isCanceled() { - return false; - } - } - - public static final TGStatusMonitor NULL_MONITOR = new NullMonitor(); - - public static class Updater { - private final TGStatusMonitor monitor; - private final int total; - private int percentage; - private int worked; - - public Updater(TGStatusMonitor monitor, int total) { - this.monitor = monitor; - this.total = total; - } - - public int worked(int work) { - worked += work; - return percentage = updatePercentage(monitor, percentage, worked, total); - } - } - - public static int updatePercentage(TGStatusMonitor monitor, int oldPercentage, int done, int total) { - int current = (100 * done) / total; - if (current > oldPercentage) { - oldPercentage = current; - if (monitor != null) - monitor.status(oldPercentage); - } - return oldPercentage; - } - +package org.simantics.graph.db; + +public interface TGStatusMonitor { + + void status(int percentage); + boolean isCanceled(); + + public static class NullMonitor implements TGStatusMonitor { + @Override + public void status(int percentage) { + } + @Override + public boolean isCanceled() { + return false; + } + } + + public static final TGStatusMonitor NULL_MONITOR = new NullMonitor(); + + public static class Updater { + private final TGStatusMonitor monitor; + private final int total; + private int low; + private int high; + private int current; + private int worked; + + public Updater(TGStatusMonitor monitor, int lowPercentage, int highPercentage, int totalWork) { + this.monitor = monitor; + this.low = lowPercentage; + this.high = highPercentage; + this.total = totalWork; + } + + public int worked(int work) { + worked += work; + return current = updatePercentage(monitor, low, high, current, worked, total); + } + } + + public static int updatePercentage(TGStatusMonitor monitor, int low, int high, int oldPercentage, int done, int total) { + int range = high - low; + int current = low + (range * done) / total; + if (current != oldPercentage) { + oldPercentage = current; + if (monitor != null) + monitor.status(oldPercentage); + } + return oldPercentage; + } + } \ No newline at end of file