From 241fd0595972f1a1536dff3ad033bbfc72ab6d86 Mon Sep 17 00:00:00 2001 From: myk Date: Wed, 13 Mar 2013 18:00:32 +0000 Subject: [PATCH] minimize UI update messages during import phase --- .../java/forge/gui/DialogMigrateProfile.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/forge/gui/DialogMigrateProfile.java b/src/main/java/forge/gui/DialogMigrateProfile.java index 0172d2677a7..c951f1ad623 100644 --- a/src/main/java/forge/gui/DialogMigrateProfile.java +++ b/src/main/java/forge/gui/DialogMigrateProfile.java @@ -380,6 +380,7 @@ public class DialogMigrateProfile { _operationLog.setLineWrap(true); _operationLog.setEditable(false); // autoscroll to bottom when we append text + // it would be nice if we only autoscrolled when the caret is at the bottom, though DefaultCaret caret = (DefaultCaret)_operationLog.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); JScrollPane scroller = new JScrollPane(_operationLog); @@ -461,6 +462,7 @@ public class DialogMigrateProfile { final MigrationSourceAnalyzer msa = new MigrationSourceAnalyzer(_srcDir, cb); final int numFilesToAnalyze = msa.getNumFilesToAnalyze(); + // update only once every half-second so we're not flooding the UI with updates timer = new Timer(500, null); final Timer finalTimer = timer; timer.addActionListener(new ActionListener() { @@ -721,6 +723,14 @@ public class DialogMigrateProfile { // working with textbox text is thread safe _operationLog.setText(""); + // only update the text box once very half second + final long updateIntervalMs = 500; + long lastUpdateTimestampMs = System.currentTimeMillis(); + StringBuffer opLogBuf = new StringBuffer(); + + // only update the progress bar when we expect the visual value to change + final long progressInterval = Math.max(1, _operations.size() / _progressBar.getWidth()); + int numOps = 0; int numSucceeded = 0; int numFailed = 0; @@ -728,12 +738,23 @@ public class DialogMigrateProfile { if (_cancel) { break; } final int curOpNum = ++numOps; - SwingUtilities.invokeLater(new Runnable() { - @Override public void run() { - if (_cancel) { return; } - _progressBar.setValue(curOpNum); - } - }); + if (0 == curOpNum % progressInterval) { + SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { + if (_cancel) { return; } + _progressBar.setValue(curOpNum); + } + }); + } + + long curTimeMs = System.currentTimeMillis(); + if (updateIntervalMs <= curTimeMs - lastUpdateTimestampMs) { + lastUpdateTimestampMs = curTimeMs; + + // working with textbox text is thread safe + _operationLog.append(opLogBuf.toString()); + opLogBuf.setLength(0); + } File srcFile = op.getKey(); File destFile = op.getValue(); @@ -747,13 +768,12 @@ public class DialogMigrateProfile { srcFile.delete(); } - // working with textbox text is thread safe - _operationLog.append(String.format("%s %s -> %s\n", + opLogBuf.append(String.format("%s %s -> %s\n", _move ? "Moved" : "Copied", srcFile.getAbsolutePath(), destFile.getAbsolutePath())); ++numSucceeded; } catch (IOException e) { - _operationLog.append(String.format("Failed to %s %s -> %s (%s)\n", + opLogBuf.append(String.format("Failed to %s %s -> %s (%s)\n", _move ? "move" : "copy", srcFile.getAbsolutePath(), destFile.getAbsolutePath(), e.getMessage())); @@ -762,6 +782,7 @@ public class DialogMigrateProfile { } // append summary footer + _operationLog.append(opLogBuf.toString()); _operationLog.append(String.format("\nImport complete. %d files %s, %d errors", numSucceeded, _move ? "moved" : "copied", numFailed)); } catch (final Exception e) {