2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.openapi.progress;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.application.ModalityState;
21 import com.intellij.openapi.project.Project;
22 import org.jetbrains.annotations.NotNull;
24 import java.util.LinkedList;
25 import java.util.Queue;
30 public class BackgroundTaskQueue {
31 private final Project myProject;
32 private final Queue<Task> myQueue = new LinkedList<Task>();
33 private boolean myHasActiveTask = false;
34 private Task.Backgroundable myRunnerTask;
36 public BackgroundTaskQueue(String title) {
40 public BackgroundTaskQueue(Project project, String title) {
42 myRunnerTask = new Task.Backgroundable(project, title) {
43 public void run(@NotNull final ProgressIndicator indicator) {
47 synchronized(myQueue) {
48 myHasActiveTask = true;
49 task = myQueue.poll();
51 myHasActiveTask = false;
56 indicator.setText(task.getTitle());
59 } catch (ProcessCanceledException e) {
63 ApplicationManager.getApplication().invokeLater(new Runnable() {
65 if (myProject == null || !myProject.isDisposed()) {
69 }, ModalityState.NON_MODAL);
75 public void run(Task.Backgroundable task) {
76 if (ApplicationManager.getApplication().isUnitTestMode()) {
77 task.run(new EmptyProgressIndicator());
80 else if (task.isConditionalModal() && !task.shouldStartInBackground()) {
81 ProgressManager.getInstance().run(task);
84 boolean hadActiveTask;
85 synchronized (myQueue) {
86 hadActiveTask = myHasActiveTask;
88 myHasActiveTask = true;
91 if (ApplicationManager.getApplication().isDispatchThread()) {
92 ProgressManager.getInstance().run(myRunnerTask);
95 ApplicationManager.getApplication().invokeLater(new Runnable() {
97 if (myProject == null || !myProject.isDisposed()) {
98 ProgressManager.getInstance().run(myRunnerTask);