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
;
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
,
44 this(name
, mergingTimeSpan
, isActive
, ANY_COMPONENT
, parent
);
47 public MavenMergingUpdateQueue(String name
,
50 JComponent modalityStateComponent
,
52 super(name
, mergingTimeSpan
, isActive
, modalityStateComponent
, parent
, null, false);
56 public void queue(Update update
) {
57 boolean passThrough
= false;
58 if (ApplicationManager
.getApplication().isUnitTestMode()) {
59 passThrough
= isPassThrough();
61 else if (MavenUtil
.isNoBackgroundMode()) {
72 public void makeUserAware(final Project project
) {
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) {
93 ProjectRootManager
.getInstance(project
).addModuleRootListener(new ModuleRootListener() {
94 public void beforeRootsChange(ModuleRootEvent event
) {
98 public void rootsChanged(ModuleRootEvent event
) {
100 MavenMergingUpdateQueue
.this.restartTimer();
102 }, MavenMergingUpdateQueue
.this);
107 public void makeDumbAware(final Project project
) {
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() {
117 public void exitDumbMode() {
121 if (DumbService
.getInstance(project
).isDumb()) {
128 public void makeModalAware(Project project
) {
129 MavenUtil
.invokeAndWait(project
, new Runnable() {
131 final ModalityStateListener listener
= new ModalityStateListener() {
132 public void beforeModalityStateChanged(boolean entering
) {
141 LaterInvocator
.addModalityStateListener(listener
);
142 if (MavenUtil
.isInModalContext()) {
145 Disposer
.register(MavenMergingUpdateQueue
.this, new Disposable() {
146 public void dispose() {
147 LaterInvocator
.removeModalityStateListener(listener
);
155 public void suspend() {
156 assert mySuspendCounter
.get() >= 0;
157 if (mySuspendCounter
.incrementAndGet() == 1) {
163 public void resume() {
164 assert mySuspendCounter
.get() > 0;
165 if (mySuspendCounter
.decrementAndGet() == 0) {