workaround for sticky focused components and windows
[fedora-idea.git] / platform-api / src / com / intellij / openapi / wm / IdeFocusManager.java
blobbbdfb3e8beaa45b1b1952b6ff2e2db6b8bcf7159
1 /*
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;
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.event.KeyEvent;
30 public abstract class IdeFocusManager {
32 /**
33 * Requests focus on a component
34 * @param c
35 * @param forced
36 * @return action callback that either notifies when the focus was obtained or focus request was droppped
38 @NotNull
39 public abstract ActionCallback requestFocus(@NotNull Component c, boolean forced);
41 /**
42 * Runs a request focus command, actual focus request is defined by the user in the command itself
43 * @param command
44 * @param forced
45 * @return action callback that either notifies when the focus was obtained or focus request was droppped
47 @NotNull
48 public abstract ActionCallback requestFocus(@NotNull FocusCommand command, boolean forced);
50 /**
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
53 * @param comp
54 * @return suitable component to focus
56 @Nullable
57 public abstract JComponent getFocusTargetFor(@NotNull final JComponent comp);
60 public abstract void doWhenFocusSettlesDown(@NotNull Runnable runnable);
62 @Nullable
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);
78 @NotNull
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();
93 return instance;
96 @NotNull
97 public static IdeFocusManager findInstanceByComponent(@NotNull Component c) {
98 final IdeFocusManager instance = findByComponent(c);
99 return instance != null ? instance : findInstanceByContext(null);
103 @Nullable
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());
109 return null;
113 @Nullable
114 private static IdeFocusManager getInstanceSafe(@Nullable Project project) {
115 if (project != null && !project.isDisposed() && project.isInitialized()) {
116 return IdeFocusManager.getInstance(project);
117 } else {
118 return null;
122 @NotNull
123 public static IdeFocusManager findInstance() {
124 final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
125 return owner != null ? findInstanceByComponent(owner) : findInstanceByContext(null);