ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / utils / MavenMergingUpdateQueue.java
blob5bef4d9c4bed1e3a1f7f8473f5f8005ef97c408b
1 /*
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.
16 package org.jetbrains.idea.maven.utils;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.application.*;
20 import com.intellij.openapi.application.impl.LaterInvocator;
21 import com.intellij.openapi.command.CommandProcessor;
22 import com.intellij.openapi.editor.EditorFactory;
23 import com.intellij.openapi.editor.event.*;
24 import com.intellij.openapi.project.DumbService;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.roots.ModuleRootEvent;
27 import com.intellij.openapi.roots.ModuleRootListener;
28 import com.intellij.openapi.roots.ProjectRootManager;
29 import com.intellij.openapi.util.Disposer;
30 import com.intellij.util.messages.MessageBusConnection;
31 import com.intellij.util.ui.update.MergingUpdateQueue;
32 import com.intellij.util.ui.update.Update;
34 import javax.swing.*;
35 import java.util.concurrent.atomic.AtomicInteger;
37 public class MavenMergingUpdateQueue extends MergingUpdateQueue {
38 private final AtomicInteger mySuspendCounter = new AtomicInteger(0);
40 public MavenMergingUpdateQueue(String name,
41 int mergingTimeSpan,
42 boolean isActive,
43 Disposable parent) {
44 this(name, mergingTimeSpan, isActive, ANY_COMPONENT, parent);
47 public MavenMergingUpdateQueue(String name,
48 int mergingTimeSpan,
49 boolean isActive,
50 JComponent modalityStateComponent,
51 Disposable parent) {
52 super(name, mergingTimeSpan, isActive, modalityStateComponent, parent, null, false);
55 @Override
56 public void queue(Update update) {
57 boolean passThrough = false;
58 if (ApplicationManager.getApplication().isUnitTestMode()) {
59 passThrough = isPassThrough();
61 else if (MavenUtil.isNoBackgroundMode()) {
62 passThrough = true;
65 if (passThrough) {
66 update.run();
67 return;
69 super.queue(update);
72 public void makeUserAware(final Project project) {
73 new ReadAction() {
74 protected void run(Result result) throws Throwable {
75 EditorEventMulticaster multicaster = EditorFactory.getInstance().getEventMulticaster();
77 multicaster.addCaretListener(new CaretListener() {
78 public void caretPositionChanged(CaretEvent e) {
79 MavenMergingUpdateQueue.this.restartTimer();
81 }, MavenMergingUpdateQueue.this);
83 multicaster.addDocumentListener(new DocumentAdapter() {
84 public void documentChanged(DocumentEvent event) {
85 MavenMergingUpdateQueue.this.restartTimer();
87 }, MavenMergingUpdateQueue.this);
89 if (CommandProcessor.getInstance().getCurrentCommand() != null) {
90 suspend();
93 ProjectRootManager.getInstance(project).addModuleRootListener(new ModuleRootListener() {
94 public void beforeRootsChange(ModuleRootEvent event) {
95 suspend();
98 public void rootsChanged(ModuleRootEvent event) {
99 resume();
100 MavenMergingUpdateQueue.this.restartTimer();
102 }, MavenMergingUpdateQueue.this);
104 }.execute();
107 public void makeDumbAware(final Project project) {
108 new ReadAction() {
109 protected void run(Result result) throws Throwable {
110 MessageBusConnection connection = project.getMessageBus().connect(MavenMergingUpdateQueue.this);
111 connection.subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
113 public void enteredDumbMode() {
114 suspend();
117 public void exitDumbMode() {
118 resume();
121 if (DumbService.getInstance(project).isDumb()) {
122 suspend();
125 }.execute();
128 public void makeModalAware(Project project) {
129 MavenUtil.invokeAndWait(project, new Runnable() {
130 public void run() {
131 final ModalityStateListener listener = new ModalityStateListener() {
132 public void beforeModalityStateChanged(boolean entering) {
133 if (entering) {
134 suspend();
136 else {
137 resume();
141 LaterInvocator.addModalityStateListener(listener);
142 if (MavenUtil.isInModalContext()) {
143 suspend();
145 Disposer.register(MavenMergingUpdateQueue.this, new Disposable() {
146 public void dispose() {
147 LaterInvocator.removeModalityStateListener(listener);
154 @Override
155 public void suspend() {
156 assert mySuspendCounter.get() >= 0;
157 if (mySuspendCounter.incrementAndGet() == 1) {
158 super.suspend();
162 @Override
163 public void resume() {
164 assert mySuspendCounter.get() > 0;
165 if (mySuspendCounter.decrementAndGet() == 0) {
166 super.resume();