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 com
.intellij
.ide
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.openapi
.ui
.popup
.IdePopupEventDispatcher
;
20 import com
.intellij
.util
.containers
.ContainerUtil
;
24 import java
.awt
.event
.KeyEvent
;
25 import java
.awt
.event
.MouseEvent
;
26 import java
.awt
.event
.WindowEvent
;
27 import java
.util
.concurrent
.CopyOnWriteArrayList
;
29 public final class IdePopupManager
implements IdeEventQueue
.EventDispatcher
{
30 private static final Logger LOG
= Logger
.getInstance("com.intellij.ide.IdePopupManager");
32 private final CopyOnWriteArrayList
<IdePopupEventDispatcher
> myDispatchStack
= ContainerUtil
.createEmptyCOWList();
34 boolean isPopupActive() {
35 for (IdePopupEventDispatcher each
: myDispatchStack
) {
36 if (each
.getComponent() == null || !each
.getComponent().isShowing()) {
37 myDispatchStack
.remove(each
);
41 return myDispatchStack
.size() > 0;
44 public boolean dispatch(final AWTEvent e
) {
45 LOG
.assertTrue(isPopupActive());
47 if (e
.getID() == WindowEvent
.WINDOW_LOST_FOCUS
) {
48 SwingUtilities
.invokeLater(new Runnable() {
50 if (!isPopupActive()) return;
52 Window focused
= ((WindowEvent
)e
).getOppositeWindow();
53 if (focused
== null) {
54 focused
= KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusedWindow();
57 if (focused
== null) {
64 if (e
instanceof KeyEvent
|| e
instanceof MouseEvent
) {
65 for (int i
= myDispatchStack
.size() - 1; (i
>=0 && i
< myDispatchStack
.size()); i
--) {
66 final boolean dispatched
= myDispatchStack
.get(i
).dispatch(e
);
67 if (dispatched
) return true;
74 public void push(IdePopupEventDispatcher dispatcher
) {
75 if (!myDispatchStack
.contains(dispatcher
)) {
76 myDispatchStack
.add(dispatcher
);
80 public void remove(IdePopupEventDispatcher dispatcher
) {
81 myDispatchStack
.remove(dispatcher
);
84 public boolean closeAllPopups(){
85 if (myDispatchStack
.size() == 0) return false;
87 boolean closed
= true;
88 for (IdePopupEventDispatcher each
: myDispatchStack
) {
89 closed
&= each
.close();