1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.progress;
|
---|
3 |
|
---|
4 | import javax.swing.SwingUtilities;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * SwingRenderingProgressMonitor is progress monitor which delegates the rendering
|
---|
10 | * of progress information to a {@see ProgressRenderer}.
|
---|
11 | * Methods of the progress renderer are always called on the Swing EDT.
|
---|
12 | *
|
---|
13 | */
|
---|
14 | public class SwingRenderingProgressMonitor extends AbstractProgressMonitor {
|
---|
15 | private static final int PROGRESS_BAR_MAX = 100;
|
---|
16 | private int currentProgressValue = 0;
|
---|
17 | private ProgressRenderer delegate;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | *
|
---|
21 | * @param delegate the delegate which renders the progress information. Must not be null.
|
---|
22 | * @throws IllegalArgumentException thrown if delegate is null
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public SwingRenderingProgressMonitor(ProgressRenderer delegate) {
|
---|
26 | super(new CancelHandler());
|
---|
27 | CheckParameterUtil.ensureParameterNotNull(delegate, "delegate");
|
---|
28 | this.delegate = delegate;
|
---|
29 | }
|
---|
30 |
|
---|
31 | private void doInEDT(Runnable runnable) {
|
---|
32 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
33 | runnable.run();
|
---|
34 | } else {
|
---|
35 | SwingUtilities.invokeLater(runnable);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public void doBeginTask() {
|
---|
41 | doInEDT(new Runnable() {
|
---|
42 | public void run() {
|
---|
43 | delegate.setCustomText("");
|
---|
44 | delegate.setMaximum(PROGRESS_BAR_MAX);
|
---|
45 | }
|
---|
46 | });
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void doFinishTask() {
|
---|
51 | // do nothing
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected void updateProgress(double progressValue) {
|
---|
56 | final int newValue = (int)(progressValue * PROGRESS_BAR_MAX);
|
---|
57 | if (newValue != currentProgressValue) {
|
---|
58 | currentProgressValue = newValue;
|
---|
59 | doInEDT(new Runnable() {
|
---|
60 | public void run() {
|
---|
61 | delegate.setValue(currentProgressValue);
|
---|
62 | }
|
---|
63 | });
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | protected void doSetCustomText(final String title) {
|
---|
69 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
70 | doInEDT(new Runnable() {
|
---|
71 | public void run() {
|
---|
72 | delegate.setCustomText(title);
|
---|
73 | }
|
---|
74 | });
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | protected void doSetTitle(final String title) {
|
---|
79 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
80 | doInEDT(new Runnable() {
|
---|
81 | public void run() {
|
---|
82 | delegate.setTaskTitle(title);
|
---|
83 | }
|
---|
84 | });
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected void doSetIntermediate(final boolean value) {
|
---|
89 | doInEDT(new Runnable() {
|
---|
90 | public void run() {
|
---|
91 | delegate.setIndeterminate(value);
|
---|
92 | }
|
---|
93 | });
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public ProgressTaskId getProgressTaskId() {
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 | }
|
---|