]> 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 r33345.
[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 percentage;\r
24         private int worked;\r
25 \r
26         public Updater(TGStatusMonitor monitor, int total) {\r
27             this.monitor = monitor;\r
28             this.total = total;\r
29         }\r
30 \r
31         public int worked(int work) {\r
32             worked += work;\r
33             return percentage = updatePercentage(monitor, percentage, worked, total);\r
34         }\r
35     }\r
36 \r
37     public static int updatePercentage(TGStatusMonitor monitor, int oldPercentage, int done, int total) {\r
38         int current = (100 * done) / total;\r
39         if (current > oldPercentage) {\r
40             oldPercentage = current;\r
41             if (monitor != null) \r
42                 monitor.status(oldPercentage);\r
43         }\r
44         return oldPercentage;\r
45     }\r
46 \r
47 }