001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.progress; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.util.Optional; 008import java.util.concurrent.TimeUnit; 009 010import org.openstreetmap.josm.tools.Logging; 011import org.openstreetmap.josm.tools.Stopwatch; 012import org.openstreetmap.josm.tools.Utils; 013 014/** 015 * CLI implementation of a {@link ProgressMonitor} 016 * @author Taylor Smock 017 * @since 18365 018 */ 019public class CLIProgressMonitor extends AbstractProgressMonitor { 020 /** The current task id */ 021 private ProgressTaskId taskId; 022 /** The current task title */ 023 private String title = ""; 024 /** The custom text (prepended with '/') */ 025 private String customText = ""; 026 /** The last time we updated the progress information */ 027 private Stopwatch lastUpdateTime; 028 /** The start time of the monitor */ 029 private Stopwatch startTime; 030 031 /** 032 * Create a new {@link CLIProgressMonitor} 033 */ 034 public CLIProgressMonitor() { 035 super(new CancelHandler()); 036 } 037 038 @Override 039 protected void doBeginTask() { 040 if (!Utils.isStripEmpty(this.title)) { 041 Logging.info(tr("Beginning task {2}: {0}{1}", this.title, this.customText, 042 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""))); 043 } 044 this.startTime = Stopwatch.createStarted(); 045 this.lastUpdateTime = this.startTime; 046 } 047 048 @Override 049 protected void doFinishTask() { 050 Logging.info(tr("Finishing task {2}: {0}{1} ({3})", this.title, this.customText, 051 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), this.startTime)); 052 this.lastUpdateTime = null; 053 } 054 055 @Override 056 protected void doSetIntermediate(boolean value) { 057 // Do nothing for now 058 } 059 060 @Override 061 protected void doSetTitle(String title) { 062 this.title = Optional.ofNullable(title).orElse(""); 063 } 064 065 @Override 066 protected void doSetCustomText(String customText) { 067 this.customText = Optional.ofNullable(customText).map(str -> '/' + str).orElse(""); 068 } 069 070 @Override 071 protected void updateProgress(double value) { 072 if (this.lastUpdateTime == null || this.lastUpdateTime.elapsed() > TimeUnit.SECONDS.toMillis(10)) { 073 this.lastUpdateTime = Stopwatch.createStarted(); 074 Logging.info(tr("Progress of task {2}: {0}{1} is {3}% ({4})", this.title, this.customText, 075 Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), value * 100, this.startTime)); 076 } 077 } 078 079 @Override 080 public void setProgressTaskId(ProgressTaskId taskId) { 081 this.taskId = taskId; 082 } 083 084 @Override 085 public ProgressTaskId getProgressTaskId() { 086 return this.taskId; 087 } 088 089 @Override 090 public Component getWindowParent() { 091 return null; 092 } 093}