1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * This is an abstract task for uploading or saving a data layer.
|
---|
6 | *
|
---|
7 | */
|
---|
8 | public abstract class AbstractIOTask implements Runnable {
|
---|
9 |
|
---|
10 | /** indicates whether the task has been canceled */
|
---|
11 | private boolean canceled;
|
---|
12 | /** indicates whether the task has been failed */
|
---|
13 | private boolean failed;
|
---|
14 | /** the last exception caught */
|
---|
15 | private Exception lastException;
|
---|
16 |
|
---|
17 | public AbstractIOTask() {
|
---|
18 | canceled = false;
|
---|
19 | failed = false;
|
---|
20 | lastException = null;
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Replies true if the task has been canceled
|
---|
25 | *
|
---|
26 | * @return true if the task has been canceled
|
---|
27 | */
|
---|
28 | public boolean isCanceled() {
|
---|
29 | return canceled;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Set whether this task has been canceled
|
---|
34 | *
|
---|
35 | * @param canceled true, if the task has been canceled; false otherwise
|
---|
36 | */
|
---|
37 | protected void setCanceled(boolean canceled) {
|
---|
38 | this.canceled = canceled;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Replies true if the task has been failed
|
---|
43 | *
|
---|
44 | * @return true if the task has been failed
|
---|
45 | */
|
---|
46 | public boolean isFailed() {
|
---|
47 | return failed || lastException != null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Sets whether the task has been failed
|
---|
52 | *
|
---|
53 | * @param failed whether the task has been failed
|
---|
54 | */
|
---|
55 | protected void setFailed(boolean failed) {
|
---|
56 | this.failed = failed;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Replies the last exception caught
|
---|
61 | *
|
---|
62 | * @return the last exception caught; null, if no exception was caught
|
---|
63 | */
|
---|
64 | public Exception getLastException() {
|
---|
65 | return lastException;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Sets the last exception caught
|
---|
70 | *
|
---|
71 | * @param lastException the last exception
|
---|
72 | */
|
---|
73 | protected void setLastException(Exception lastException) {
|
---|
74 | this.lastException = lastException;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Replies true if this task was successful, i.e. if it wasn't
|
---|
79 | * canceled and didn't fail
|
---|
80 | *
|
---|
81 | * @return true if this task was successful
|
---|
82 | */
|
---|
83 | public boolean isSuccessful() {
|
---|
84 | return !isCanceled() && !isFailed();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Runs the task
|
---|
89 | */
|
---|
90 | public abstract void run();
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Cancel the task
|
---|
94 | */
|
---|
95 | public abstract void cancel();
|
---|
96 | }
|
---|