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