From ac2ffa42ff19dd3242073ab738a116a1b058ad1b Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 16 Sep 2014 14:01:45 +0400 Subject: [PATCH] Proper handling of progress monitor in TeaVMBuilder --- .../java/org/teavm/eclipse/TeaVMBuilder.java | 6 +- .../eclipse/TeaVMEclipseProgressListener.java | 84 ++++++++++++++----- 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMBuilder.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMBuilder.java index 72ec42276..93238a9e2 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMBuilder.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMBuilder.java @@ -41,13 +41,17 @@ public class TeaVMBuilder extends IncrementalProjectBuilder { tool.setRuntime(RuntimeCopyOperation.SEPARATE); tool.setMinifying(false); tool.setMainClass(projectSettings.getMainClass()); - tool.setProgressListener(new TeaVMEclipseProgressListener(monitor)); + tool.setProgressListener(new TeaVMEclipseProgressListener(this, monitor, 10000)); try { + monitor.beginTask("Running TeaVM", 10000); tool.generate(); removeMarkers(); if (tool.getDependencyViolations().hasMissingItems()) { putMarkers(tool.getDependencyViolations()); } + if (!monitor.isCanceled()) { + monitor.done(); + } } catch (TeaVMToolException e) { throw new CoreException(TeaVMEclipsePlugin.makeError(e)); } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMEclipseProgressListener.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMEclipseProgressListener.java index 57808acac..05ee3be50 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMEclipseProgressListener.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMEclipseProgressListener.java @@ -9,40 +9,82 @@ import org.teavm.vm.TeaVMProgressListener; * * @author Alexey Andreev */ -public class TeaVMEclipseProgressListener implements TeaVMProgressListener { +class TeaVMEclipseProgressListener implements TeaVMProgressListener { + private TeaVMBuilder builder; private IProgressMonitor progressMonitor; + private TeaVMPhase currentPhase; + private int currentProgress; + private int currentPhaseTotal; + private int total; + private int last; - public TeaVMEclipseProgressListener(IProgressMonitor progressMonitor) { + public TeaVMEclipseProgressListener(TeaVMBuilder builder, IProgressMonitor progressMonitor, int total) { + this.builder = builder; this.progressMonitor = progressMonitor; + this.total = total; } @Override public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) { - String taskName = "Building"; - switch (phase) { - case DECOMPILATION: - taskName = "Decompiling"; - break; - case DEPENDENCY_CHECKING: - taskName = "Dependency checking"; - break; - case DEVIRTUALIZATION: - taskName = "Applying devirtualization"; - break; - case LINKING: - taskName = "Linking"; - break; - case RENDERING: - taskName = "Rendering"; - break; + if (phase != currentPhase) { + String taskName = "Building"; + switch (phase) { + case DECOMPILATION: + taskName = "Decompiling"; + break; + case DEPENDENCY_CHECKING: + taskName = "Dependency checking"; + break; + case DEVIRTUALIZATION: + taskName = "Applying devirtualization"; + break; + case LINKING: + taskName = "Linking"; + break; + case RENDERING: + taskName = "Rendering"; + break; + } + progressMonitor.subTask(taskName); } - progressMonitor.beginTask(taskName, count); + currentPhase = phase; + currentProgress = 0; + currentPhaseTotal = count; + if (builder.isInterrupted()) { + progressMonitor.setCanceled(true); + } + update(); return progressMonitor.isCanceled() ? TeaVMProgressFeedback.CANCEL : TeaVMProgressFeedback.CONTINUE; } @Override public TeaVMProgressFeedback progressReached(int progress) { - progressMonitor.worked(progress); + currentProgress = progress; + update(); + if (builder.isInterrupted()) { + progressMonitor.setCanceled(true); + } return progressMonitor.isCanceled() ? TeaVMProgressFeedback.CANCEL : TeaVMProgressFeedback.CONTINUE; } + + private int getActual() { + if (currentPhase == null) { + return 0; + } + int totalPhases = TeaVMPhase.values().length; + int min = total * currentPhase.ordinal() / totalPhases; + if (currentPhaseTotal <= 0) { + return min; + } + int max = total * (currentPhase.ordinal() + 1) / totalPhases - 1; + return Math.min(max, min + (max - min) * currentProgress / currentPhaseTotal); + } + + private void update() { + int actual = getActual(); + if (actual > last) { + progressMonitor.worked(actual - last); + last = actual; + } + } }