5919 |
24 Apr 20 |
nicklas |
1 |
package net.sf.basedb.reggie; |
5919 |
24 Apr 20 |
nicklas |
2 |
|
5919 |
24 Apr 20 |
nicklas |
3 |
import net.sf.basedb.core.ProgressReporter; |
5919 |
24 Apr 20 |
nicklas |
4 |
import net.sf.basedb.core.SimpleProgressReporter; |
5919 |
24 Apr 20 |
nicklas |
5 |
import net.sf.basedb.core.signal.SignalException; |
5919 |
24 Apr 20 |
nicklas |
6 |
|
5919 |
24 Apr 20 |
nicklas |
7 |
/** |
5919 |
24 Apr 20 |
nicklas |
Progress reporter implementation that add support for aborting |
5919 |
24 Apr 20 |
nicklas |
a process. The implementation requires a parent progress |
5919 |
24 Apr 20 |
nicklas |
reporter that is responsible for reporting the progress. |
5919 |
24 Apr 20 |
nicklas |
11 |
|
5919 |
24 Apr 20 |
nicklas |
By calling {@link #setAbort()} a flag is set on this implementation |
5919 |
24 Apr 20 |
nicklas |
that causes the next call to {@link #display(int, String)} or |
5919 |
24 Apr 20 |
nicklas |
{@link #append(String)} to result in an exception. |
5919 |
24 Apr 20 |
nicklas |
15 |
|
5919 |
24 Apr 20 |
nicklas |
@author nicklas |
5919 |
24 Apr 20 |
nicklas |
@since 4.27 |
5919 |
24 Apr 20 |
nicklas |
18 |
*/ |
5919 |
24 Apr 20 |
nicklas |
19 |
public class AbortableProgressReporter |
5919 |
24 Apr 20 |
nicklas |
20 |
extends SimpleProgressReporter |
5919 |
24 Apr 20 |
nicklas |
21 |
{ |
5919 |
24 Apr 20 |
nicklas |
22 |
|
5919 |
24 Apr 20 |
nicklas |
23 |
private transient boolean shouldAbort; |
5919 |
24 Apr 20 |
nicklas |
24 |
|
5919 |
24 Apr 20 |
nicklas |
25 |
public AbortableProgressReporter(ProgressReporter parent) |
5919 |
24 Apr 20 |
nicklas |
26 |
{ |
5919 |
24 Apr 20 |
nicklas |
27 |
super(parent); |
5919 |
24 Apr 20 |
nicklas |
28 |
} |
5919 |
24 Apr 20 |
nicklas |
29 |
|
5919 |
24 Apr 20 |
nicklas |
30 |
@Override |
5919 |
24 Apr 20 |
nicklas |
31 |
public void display(int percent, String message) |
5919 |
24 Apr 20 |
nicklas |
32 |
{ |
5919 |
24 Apr 20 |
nicklas |
33 |
checkAbort(); |
5919 |
24 Apr 20 |
nicklas |
34 |
super.display(percent, message); |
5919 |
24 Apr 20 |
nicklas |
35 |
} |
5919 |
24 Apr 20 |
nicklas |
36 |
|
5919 |
24 Apr 20 |
nicklas |
37 |
@Override |
5919 |
24 Apr 20 |
nicklas |
38 |
public void append(String message) |
5919 |
24 Apr 20 |
nicklas |
39 |
{ |
5919 |
24 Apr 20 |
nicklas |
40 |
checkAbort(); |
5919 |
24 Apr 20 |
nicklas |
41 |
super.append(message); |
5919 |
24 Apr 20 |
nicklas |
42 |
} |
5919 |
24 Apr 20 |
nicklas |
43 |
|
5919 |
24 Apr 20 |
nicklas |
44 |
/** |
5919 |
24 Apr 20 |
nicklas |
Set a flag indicating that the process should be aborted. |
5919 |
24 Apr 20 |
nicklas |
46 |
*/ |
5919 |
24 Apr 20 |
nicklas |
47 |
public void setAbort() |
5919 |
24 Apr 20 |
nicklas |
48 |
{ |
5919 |
24 Apr 20 |
nicklas |
49 |
shouldAbort = true; |
5919 |
24 Apr 20 |
nicklas |
50 |
} |
5919 |
24 Apr 20 |
nicklas |
51 |
|
5919 |
24 Apr 20 |
nicklas |
52 |
/** |
5919 |
24 Apr 20 |
nicklas |
Check the flag indicating if the process should be aborted. |
5919 |
24 Apr 20 |
nicklas |
54 |
*/ |
5919 |
24 Apr 20 |
nicklas |
55 |
public boolean shouldAbort() |
5919 |
24 Apr 20 |
nicklas |
56 |
{ |
5919 |
24 Apr 20 |
nicklas |
57 |
return shouldAbort; |
5919 |
24 Apr 20 |
nicklas |
58 |
} |
5919 |
24 Apr 20 |
nicklas |
59 |
|
5919 |
24 Apr 20 |
nicklas |
60 |
/** |
5919 |
24 Apr 20 |
nicklas |
Check the flag indicating if the process should be aborted |
5919 |
24 Apr 20 |
nicklas |
and throw an expection if the flag has been set. |
5919 |
24 Apr 20 |
nicklas |
63 |
*/ |
5919 |
24 Apr 20 |
nicklas |
64 |
public void checkAbort() |
5919 |
24 Apr 20 |
nicklas |
65 |
{ |
5919 |
24 Apr 20 |
nicklas |
66 |
if (shouldAbort()) throw new SignalException("Aborted by user"); |
5919 |
24 Apr 20 |
nicklas |
67 |
} |
5919 |
24 Apr 20 |
nicklas |
68 |
|
5919 |
24 Apr 20 |
nicklas |
69 |
} |