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.
18 * Created by IntelliJ IDEA.
23 package com.intellij.openapi.progress.impl;
25 import com.intellij.openapi.Disposable;
26 import com.intellij.openapi.progress.PerformInBackgroundOption;
27 import com.intellij.openapi.progress.Task;
28 import com.intellij.openapi.progress.TaskInfo;
29 import com.intellij.openapi.progress.util.ProgressWindow;
30 import com.intellij.openapi.project.*;
31 import com.intellij.openapi.util.Disposer;
32 import com.intellij.openapi.wm.IdeFrame;
33 import com.intellij.openapi.wm.WindowManager;
34 import com.intellij.openapi.wm.ex.StatusBarEx;
35 import com.intellij.openapi.wm.ex.WindowManagerEx;
36 import org.jetbrains.annotations.Nls;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
40 public class BackgroundableProcessIndicator extends ProgressWindow {
41 protected StatusBarEx myStatusBar;
43 @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"})
45 private PerformInBackgroundOption myOption;
46 private TaskInfo myInfo;
48 private boolean myDisposed;
49 private DumbModeAction myDumbModeAction = DumbModeAction.NOTHING;
51 public BackgroundableProcessIndicator(Task.Backgroundable task) {
52 this(task.getProject(), task, task);
54 myDumbModeAction = task.getDumbModeAction();
55 if (myDumbModeAction == DumbModeAction.CANCEL) {
56 task.getProject().getMessageBus().connect(this).subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
58 public void enteredDumbMode() {
62 public void exitDumbMode() {
68 public BackgroundableProcessIndicator(@Nullable final Project project, TaskInfo info, @NotNull PerformInBackgroundOption option) {
69 super(info.isCancellable(), true, project, info.getCancelText());
70 if (project != null) {
71 final ProjectManagerAdapter myListener = new ProjectManagerAdapter() {
72 public void projectClosing(Project closingProject) {
78 ProjectManager.getInstance().addProjectManagerListener(project, myListener);
79 Disposer.register(this, new Disposable() {
81 public void dispose() {
82 ProjectManager.getInstance().removeProjectManagerListener(project, myListener);
87 setProcessId(info.getProcessId());
90 setTitle(info.getTitle());
91 final Project nonDefaultProject = project == null || project.isDisposed() ? null : project.isDefault() ? null : project;
92 final IdeFrame frame = ((WindowManagerEx)WindowManager.getInstance()).findFrameFor(nonDefaultProject);
93 myStatusBar = (StatusBarEx)frame.getStatusBar();
94 if (option.shouldStartInBackground()) {
99 public BackgroundableProcessIndicator(Project project,
100 @Nls final String progressTitle,
101 @NotNull PerformInBackgroundOption option,
102 @Nls final String cancelButtonText,
103 @Nls final String backgroundStopTooltip, final boolean cancellable) {
104 this(project, new TaskInfo() {
105 public String getProcessId() {
109 public String getTitle() {
110 return progressTitle;
113 public String getCancelText() {
114 return cancelButtonText;
117 public String getCancelTooltipText() {
118 return backgroundStopTooltip;
121 public boolean isCancellable() {
127 public DumbModeAction getDumbModeAction() {
128 return myDumbModeAction;
131 protected void showDialog() {
132 if (myDisposed) return;
134 if (myOption.shouldStartInBackground()) {
141 public void background() {
142 if (myDisposed) return;
144 myOption.processSentToBackground();
149 private void doBackground() {
150 myStatusBar.addProgress(this, myInfo);
153 public void dispose() {