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