]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGStatusMonitor.java
Sync git svn branch with SVN repository r33349.
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / TGStatusMonitor.java
1 package org.simantics.graph.db;\r
2 \r
3 public interface TGStatusMonitor {\r
4 \r
5     void status(int percentage);\r
6     boolean isCanceled();\r
7 \r
8     public static class NullMonitor implements TGStatusMonitor {\r
9         @Override\r
10         public void status(int percentage) {\r
11         }\r
12         @Override\r
13         public boolean isCanceled() {\r
14             return false;\r
15         }\r
16     }\r
17 \r
18     public static final TGStatusMonitor NULL_MONITOR = new NullMonitor();\r
19 \r
20     public static class Updater {\r
21         private final TGStatusMonitor monitor;\r
22         private final int total;\r
23         private int low;\r
24         private int high;\r
25         private int current;\r
26         private int worked;\r
27 \r
28         public Updater(TGStatusMonitor monitor, int lowPercentage, int highPercentage, int totalWork) {\r
29             this.monitor = monitor;\r
30             this.low = lowPercentage;\r
31             this.high = highPercentage;\r
32             this.total = totalWork;\r
33         }\r
34 \r
35         public int worked(int work) {\r
36             worked += work;\r
37             return current = updatePercentage(monitor, low, high, current, worked, total);\r
38         }\r
39     }\r
40 \r
41     public static int updatePercentage(TGStatusMonitor monitor, int low, int high, int oldPercentage, int done, int total) {\r
42         int range = high - low;\r
43         int current = low + (range * done) / total;\r
44         if (current != oldPercentage) {\r
45             oldPercentage = current;\r
46             if (monitor != null) \r
47                 monitor.status(oldPercentage);\r
48         }\r
49         return oldPercentage;\r
50     }\r
51 \r
52 }