forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgressHelper.java
93 lines (80 loc) · 2.24 KB
/
ProgressHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2016 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.utils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class ProgressHelper {
final Project myProject;
final List<String> myTasks = new ArrayList<>();
private Task.Backgroundable myTask;
public ProgressHelper(@NotNull Project project) {
this.myProject = project;
}
/**
* Start a progress task.
*
* @param log the title of the progress task
*/
public void start(String log) {
synchronized (myTasks) {
myTasks.add(log);
myTasks.notifyAll();
if (myTask == null) {
myTask = new Task.Backgroundable(myProject, log, false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setText(log);
synchronized (myTasks) {
while (!myTasks.isEmpty()) {
indicator.setText(myTasks.get(myTasks.size() - 1));
try {
myTasks.wait();
}
catch (InterruptedException e) {
// ignore
}
myTask = null;
}
}
}
};
ApplicationManager.getApplication().invokeLater(() -> {
synchronized (myTasks) {
if (myTask != null && !myProject.isDisposed()) {
ProgressManager.getInstance().run(myTask);
}
}
});
}
}
}
/**
* Notify that a progress task has finished.
*/
public void done() {
synchronized (myTasks) {
if (!myTasks.isEmpty()) {
myTasks.remove(myTasks.size() - 1);
myTasks.notifyAll();
}
}
}
/**
* Finish any outstanding progress tasks.
*/
public void cancel() {
synchronized (myTasks) {
myTasks.clear();
myTasks.notifyAll();
}
}
}