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
.util
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.application
.ModalityState
;
20 import com
.intellij
.openapi
.wm
.ex
.WindowManagerEx
;
21 import com
.intellij
.util
.containers
.ContainerUtil
;
22 import com
.intellij
.util
.containers
.FilteringIterator
;
23 import gnu
.trove
.TIntStack
;
27 import java
.util
.Iterator
;
29 public class IJSwingUtilities
{
30 public static void invoke(Runnable runnable
) {
31 if (ApplicationManager
.getApplication().isDispatchThread()) {
34 ApplicationManager
.getApplication().invokeLater(runnable
, ModalityState
.NON_MODAL
);
39 * @return true if javax.swing.SwingUtilities.findFocusOwner(component) != null
41 public static boolean hasFocus(Component component
) {
42 Component focusOwner
= findFocusOwner(component
);
43 return focusOwner
!= null;
46 private static Component
findFocusOwner(Component c
) {
47 Component focusOwner
= KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
49 // verify focusOwner is a descendant of c
50 for (Component temp
= focusOwner
; temp
!= null; temp
= (temp
instanceof Window
) ?
null : temp
.getParent())
61 * @return true if window ancestor of component was most recent focused window and most recent focused component
62 * in that window was descended from component
64 public static boolean hasFocus2(Component component
) {
65 WindowManagerEx windowManager
= WindowManagerEx
.getInstanceEx();
66 Window activeWindow
=null;
67 if (windowManager
!= null) {
68 activeWindow
= windowManager
.getMostRecentFocusedWindow();
70 if(activeWindow
==null){
73 Component focusedComponent
= windowManager
.getFocusedComponent(activeWindow
);
74 if (focusedComponent
== null) {
78 return SwingUtilities
.isDescendingFrom(focusedComponent
, component
);
82 * This method is copied from <code>SwingUtilities</code>.
83 * Returns index of the first occurrence of <code>mnemonic</code>
84 * within string <code>text</code>. Matching algorithm is not
87 * @param text The text to search through, may be null
88 * @param mnemonic The mnemonic to find the character for.
89 * @return index into the string if exists, otherwise -1
91 public static int findDisplayedMnemonicIndex(String text
, int mnemonic
) {
92 if (text
== null || mnemonic
== '\0') {
96 char uc
= Character
.toUpperCase((char)mnemonic
);
97 char lc
= Character
.toLowerCase((char)mnemonic
);
99 int uci
= text
.indexOf(uc
);
100 int lci
= text
.indexOf(lc
);
104 } else if(lci
== -1) {
107 return (lci
< uci
) ? lci
: uci
;
111 public static Iterator
<Component
> getParents(final Component component
) {
112 return new Iterator
<Component
>() {
113 private Component myCurrent
= component
;
114 public boolean hasNext() {
115 return myCurrent
!= null && myCurrent
.getParent() != null;
118 public Component
next() {
119 myCurrent
= myCurrent
.getParent();
123 public void remove() {
124 throw new UnsupportedOperationException();
130 * @param component - parent component, won't be reached by iterator.
131 * @return Component tree traverse {@link Iterator}.
133 public static Iterator
<Component
> getChildren(final Container component
) {
134 return new Iterator
<Component
>() {
135 private Container myCurrentParent
= component
;
136 private final TIntStack myState
= new TIntStack();
137 private int myCurrentIndex
= 0;
139 public boolean hasNext() {
140 return hasNextChild();
143 public Component
next() {
144 Component next
= myCurrentParent
.getComponent(myCurrentIndex
);
146 if (next
instanceof Container
) {
147 Container container
= ((Container
)next
);
148 if (container
.getComponentCount() > 0) {
149 myState
.push(myCurrentIndex
);
151 myCurrentParent
= container
;
154 while (!hasNextChild()) {
155 if (myState
.size() == 0) break;
156 myCurrentIndex
= myState
.pop();
157 myCurrentParent
= myCurrentParent
.getParent();
162 public void remove() {
163 throw new UnsupportedOperationException();
166 private boolean hasNextChild() {
167 return myCurrentParent
.getComponentCount() > myCurrentIndex
;
172 public static <T
extends Component
> T
findParentOfType(Component focusOwner
, Class
<T
> aClass
) {
173 return (T
)ContainerUtil
.find(getParents(focusOwner
), (FilteringIterator
.InstanceOf
<T
>)FilteringIterator
.instanceOf(aClass
));