source: josm/trunk/src/org/openstreetmap/josm/gui/progress/CLIProgressMonitor.java@ 18365

Last change on this file since 18365 was 18365, checked in by Don-vip, 2 years ago

see #15182 - make JOSM callable as standalone validator (patch by taylor.smock)

File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.progress;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.util.Optional;
8import java.util.concurrent.TimeUnit;
9
10import org.openstreetmap.josm.tools.Logging;
11import org.openstreetmap.josm.tools.Stopwatch;
12import org.openstreetmap.josm.tools.Utils;
13
14/**
15 * CLI implementation of a {@link ProgressMonitor}
16 * @author Taylor Smock
17 * @since 18365
18 */
19public class CLIProgressMonitor extends AbstractProgressMonitor {
20 /** The current task id */
21 private ProgressTaskId taskId;
22 /** The current task title */
23 private String title = "";
24 /** The custom text (prepended with '/') */
25 private String customText = "";
26 /** The last time we updated the progress information */
27 private Stopwatch lastUpdateTime;
28 /** The start time of the monitor */
29 private Stopwatch startTime;
30
31 /**
32 * Create a new {@link CLIProgressMonitor}
33 */
34 public CLIProgressMonitor() {
35 super(new CancelHandler());
36 }
37
38 @Override
39 protected void doBeginTask() {
40 if (!Utils.isBlank(this.title)) {
41 Logging.info(tr("Beginning task{2}: {0}{1}", this.title, this.customText,
42 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse("")));
43 }
44 this.startTime = Stopwatch.createStarted();
45 this.lastUpdateTime = this.startTime;
46 }
47
48 @Override
49 protected void doFinishTask() {
50 Logging.info(tr("Finishing task{2}: {0}{1} ({3})", this.title, this.customText,
51 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), this.startTime));
52 this.lastUpdateTime = null;
53 }
54
55 @Override
56 protected void doSetIntermediate(boolean value) {
57 // Do nothing for now
58 }
59
60 @Override
61 protected void doSetTitle(String title) {
62 this.title = Optional.ofNullable(title).orElse("");
63 }
64
65 @Override
66 protected void doSetCustomText(String customText) {
67 this.customText = Optional.ofNullable(customText).map(str -> '/' + str).orElse("");
68 }
69
70 @Override
71 protected void updateProgress(double value) {
72 if (this.lastUpdateTime == null || this.lastUpdateTime.elapsed() > TimeUnit.SECONDS.toMillis(10)) {
73 this.lastUpdateTime = Stopwatch.createStarted();
74 Logging.info(tr("Progress of task{2}: {0}{1} is {3}% ({4})", this.title, this.customText,
75 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), value * 100, this.startTime));
76 }
77 }
78
79 @Override
80 public void setProgressTaskId(ProgressTaskId taskId) {
81 this.taskId = taskId;
82 }
83
84 @Override
85 public ProgressTaskId getProgressTaskId() {
86 return this.taskId;
87 }
88
89 @Override
90 public Component getWindowParent() {
91 return null;
92 }
93}
Note: See TracBrowser for help on using the repository browser.