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.
18 * @author Vladimir Kondratyev
20 package com
.intellij
.openapi
.wm
.impl
.commands
;
22 import com
.intellij
.openapi
.fileEditor
.ex
.FileEditorManagerEx
;
23 import com
.intellij
.openapi
.util
.ActionCallback
;
24 import com
.intellij
.openapi
.util
.Expirable
;
25 import com
.intellij
.openapi
.wm
.IdeFocusManager
;
26 import com
.intellij
.openapi
.wm
.impl
.FloatingDecorator
;
27 import org
.jetbrains
.annotations
.NotNull
;
33 * Requests focus for the editor component.
35 public final class RequestFocusInEditorComponentCmd
extends FinalizableCommand
{
36 private final FileEditorManagerEx myEditorManager
;
37 private final JComponent myComponent
;
38 private final boolean myForced
;
39 private final ActionCallback myDoneCallback
;
41 private IdeFocusManager myFocusManager
;
42 private Expirable myTimestamp
;
44 public RequestFocusInEditorComponentCmd(@NotNull final FileEditorManagerEx editorManager
, IdeFocusManager
45 focusManager
, final Runnable finishCallBack
, boolean forced
){
46 super(finishCallBack
);
47 myEditorManager
= editorManager
;
48 myComponent
= myEditorManager
.getPreferredFocusedComponent();
50 myFocusManager
= focusManager
;
52 myDoneCallback
= new ActionCallback();
54 myTimestamp
= myFocusManager
.getTimestamp(true);
57 public ActionCallback
getDoneCallback() {
58 return myDoneCallback
;
61 public final void run(){
63 if (myTimestamp
.isExpired()) {
64 final Component owner
= KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
65 if (owner
!= null && owner
== myComponent
) {
66 myDoneCallback
.setDone();
68 myDoneCallback
.setRejected();
73 final Window owner
=SwingUtilities
.getWindowAncestor(myEditorManager
.getComponent());
78 if(myComponent
!= null){
79 myFocusManager
.requestFocus(myComponent
, myForced
).notifyWhenDone(myDoneCallback
).doWhenDone(new Runnable() {
81 // if owner is active window or it has active child window which isn't floating decorator then
82 // don't bring owner window to font. If we will make toFront every time then it's possible
83 // the following situation:
84 // 1. user prform refactoring
85 // 2. "Do not show preview" dialog is popping up.
86 // 3. At that time "preview" tool window is being activated and modal "don't show..." dialog
88 if(!owner
.isActive()){
89 final Window activeWindow
=getActiveWindow(owner
.getOwnedWindows());
90 if(activeWindow
== null || (activeWindow
instanceof FloatingDecorator
)){
92 //System.out.println("------------------------------------------------------");
99 myDoneCallback
.setRejected();
108 * @return first active window from hierarchy with specified roots. Returns <code>null</code>
109 * if there is no active window in the hierarchy.
111 private Window
getActiveWindow(final Window
[] windows
){
112 for(int i
=0;i
<windows
.length
;i
++){
113 Window window
=windows
[i
];
114 if(window
.isShowing()&&window
.isActive()){
117 window
=getActiveWindow(window
.getOwnedWindows());