2 * Copyright 2000-2007 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
.openapi
.wm
;
18 import com
.intellij
.openapi
.actionSystem
.DataContext
;
19 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.util
.ActionCallback
;
22 import com
.intellij
.util
.ui
.UIUtil
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
28 import java
.awt
.event
.KeyEvent
;
30 public abstract class IdeFocusManager
{
33 * Requests focus on a component
36 * @return action callback that either notifies when the focus was obtained or focus request was droppped
39 public abstract ActionCallback
requestFocus(@NotNull Component c
, boolean forced
);
42 * Runs a request focus command, actual focus request is defined by the user in the command itself
45 * @return action callback that either notifies when the focus was obtained or focus request was droppped
48 public abstract ActionCallback
requestFocus(@NotNull FocusCommand command
, boolean forced
);
51 * Finds most suitable component to request focus to. For instance you may pass a JPanel instance,
52 * this method will traverse into it's children to find focusable component
54 * @return suitable component to focus
57 public abstract JComponent
getFocusTargetFor(@NotNull final JComponent comp
);
60 public abstract void doWhenFocusSettlesDown(@NotNull Runnable runnable
);
63 public abstract Component
getFocusedDescendantFor(final Component comp
);
65 public abstract boolean dispatch(KeyEvent e
);
67 public abstract void suspendKeyProcessingUntil(@NotNull ActionCallback done
);
69 public abstract boolean isFocusBeingTransferred();
71 public abstract ActionCallback
requestDefaultFocus(boolean forced
);
73 public static IdeFocusManager
getInstance(@NotNull Project project
) {
74 if (project
.isDisposed() || !project
.isInitialized()) return PassThroughtIdeFocusManager
.getInstance();
75 return project
.getComponent(IdeFocusManager
.class);
79 public static IdeFocusManager
findInstanceByContext(@Nullable DataContext context
) {
80 IdeFocusManager instance
= null;
81 if (context
!= null) {
82 instance
= getInstanceSafe(PlatformDataKeys
.PROJECT
.getData(context
));
85 if (instance
== null) {
86 instance
= findByComponent(KeyboardFocusManager
.getCurrentKeyboardFocusManager().getActiveWindow());
89 if (instance
== null) {
90 instance
= PassThroughtIdeFocusManager
.getInstance();
97 public static IdeFocusManager
findInstanceByComponent(@NotNull Component c
) {
98 final IdeFocusManager instance
= findByComponent(c
);
99 return instance
!= null ? instance
: findInstanceByContext(null);
104 private static IdeFocusManager
findByComponent(Component c
) {
105 final Component parent
= UIUtil
.findUltimateParent(c
);
106 if (parent
instanceof IdeFrame
) {
107 return getInstanceSafe(((IdeFrame
)parent
).getProject());
114 private static IdeFocusManager
getInstanceSafe(@Nullable Project project
) {
115 if (project
!= null && !project
.isDisposed() && project
.isInitialized()) {
116 return IdeFocusManager
.getInstance(project
);
123 public static IdeFocusManager
findInstance() {
124 final Component owner
= KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
125 return owner
!= null ?
findInstanceByComponent(owner
) : findInstanceByContext(null);