Proper handling of progress monitor in TeaVMBuilder

This commit is contained in:
konsoletyper 2014-09-16 14:01:45 +04:00
parent b7c07886a6
commit ac2ffa42ff
2 changed files with 68 additions and 22 deletions

View File

@ -41,13 +41,17 @@ public class TeaVMBuilder extends IncrementalProjectBuilder {
tool.setRuntime(RuntimeCopyOperation.SEPARATE); tool.setRuntime(RuntimeCopyOperation.SEPARATE);
tool.setMinifying(false); tool.setMinifying(false);
tool.setMainClass(projectSettings.getMainClass()); tool.setMainClass(projectSettings.getMainClass());
tool.setProgressListener(new TeaVMEclipseProgressListener(monitor)); tool.setProgressListener(new TeaVMEclipseProgressListener(this, monitor, 10000));
try { try {
monitor.beginTask("Running TeaVM", 10000);
tool.generate(); tool.generate();
removeMarkers(); removeMarkers();
if (tool.getDependencyViolations().hasMissingItems()) { if (tool.getDependencyViolations().hasMissingItems()) {
putMarkers(tool.getDependencyViolations()); putMarkers(tool.getDependencyViolations());
} }
if (!monitor.isCanceled()) {
monitor.done();
}
} catch (TeaVMToolException e) { } catch (TeaVMToolException e) {
throw new CoreException(TeaVMEclipsePlugin.makeError(e)); throw new CoreException(TeaVMEclipsePlugin.makeError(e));
} }

View File

@ -9,15 +9,24 @@ import org.teavm.vm.TeaVMProgressListener;
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class TeaVMEclipseProgressListener implements TeaVMProgressListener { class TeaVMEclipseProgressListener implements TeaVMProgressListener {
private TeaVMBuilder builder;
private IProgressMonitor progressMonitor; 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.progressMonitor = progressMonitor;
this.total = total;
} }
@Override @Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) { public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) {
if (phase != currentPhase) {
String taskName = "Building"; String taskName = "Building";
switch (phase) { switch (phase) {
case DECOMPILATION: case DECOMPILATION:
@ -36,13 +45,46 @@ public class TeaVMEclipseProgressListener implements TeaVMProgressListener {
taskName = "Rendering"; taskName = "Rendering";
break; break;
} }
progressMonitor.beginTask(taskName, count); progressMonitor.subTask(taskName);
}
currentPhase = phase;
currentProgress = 0;
currentPhaseTotal = count;
if (builder.isInterrupted()) {
progressMonitor.setCanceled(true);
}
update();
return progressMonitor.isCanceled() ? TeaVMProgressFeedback.CANCEL : TeaVMProgressFeedback.CONTINUE; return progressMonitor.isCanceled() ? TeaVMProgressFeedback.CANCEL : TeaVMProgressFeedback.CONTINUE;
} }
@Override @Override
public TeaVMProgressFeedback progressReached(int progress) { public TeaVMProgressFeedback progressReached(int progress) {
progressMonitor.worked(progress); currentProgress = progress;
update();
if (builder.isInterrupted()) {
progressMonitor.setCanceled(true);
}
return progressMonitor.isCanceled() ? TeaVMProgressFeedback.CANCEL : TeaVMProgressFeedback.CONTINUE; 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;
}
}
} }