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;
31 public class BackgroundTaskQueue {
32 private final Project myProject;
33 private final Queue<Task> myQueue = new LinkedList<Task>();
34 private boolean myHasActiveTask = false;
35 private Task.Backgroundable myRunnerTask;
37 public BackgroundTaskQueue(String title) {
41 public BackgroundTaskQueue(Project project, String title) {
43 myRunnerTask = new Task.Backgroundable(project, title) {
44 public void run(@NotNull final ProgressIndicator indicator) {
48 synchronized(myQueue) {
49 myHasActiveTask = true;
50 task = myQueue.poll();
52 myHasActiveTask = false;
57 indicator.setText(task.getTitle());
60 } catch (ProcessCanceledException e) {
64 ApplicationManager.getApplication().invokeLater(new Runnable() {
66 if (myProject == null || !myProject.isDisposed()) {
70 }, ModalityState.NON_MODAL);
76 public void run(Task.Backgroundable task) {
77 if (ApplicationManager.getApplication().isUnitTestMode()) {
78 task.run(new EmptyProgressIndicator());
81 else if (task.isConditionalModal() && !task.shouldStartInBackground()) {
82 ProgressManager.getInstance().run(task);
85 boolean hadActiveTask;
86 synchronized (myQueue) {
87 hadActiveTask = myHasActiveTask;
89 myHasActiveTask = true;
92 if (ApplicationManager.getApplication().isDispatchThread()) {
93 ProgressManager.getInstance().run(myRunnerTask);
96 ApplicationManager.getApplication().invokeLater(new Runnable() {
98 if (myProject == null || !myProject.isDisposed()) {
99 ProgressManager.getInstance().run(myRunnerTask);