summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/wm/impl/commands/RequestFocusInEditorComponentCmd.java
blob: 7a772e7aed34861b3a08014f263e64ab23694e74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Vladimir Kondratyev
 */
package com.intellij.openapi.wm.impl.commands;

import com.intellij.openapi.fileEditor.impl.EditorWindow;
import com.intellij.openapi.fileEditor.impl.EditorWithProviderComposite;
import com.intellij.openapi.fileEditor.impl.EditorsSplitters;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Expirable;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.impl.FloatingDecorator;
import com.intellij.openapi.wm.impl.IdeFrameImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * Requests focus for the editor component.
 */
public final class RequestFocusInEditorComponentCmd extends FinalizableCommand{
  private JComponent myComponent;
  private final boolean myForced;
  private final ActionCallback myDoneCallback;

  private final IdeFocusManager myFocusManager;
  private final Expirable myTimestamp;

  public RequestFocusInEditorComponentCmd(@NotNull final EditorsSplitters splitters, IdeFocusManager
                                          focusManager, final Runnable finishCallBack, boolean forced){
    super(finishCallBack);

    myComponent = null;
    final EditorWindow window = splitters.getCurrentWindow();
    if (window != null) {
      final EditorWithProviderComposite editor = window.getSelectedEditor();
      if (editor != null) {
        myComponent = editor.getPreferredFocusedComponent();
      }
    }

    myForced = forced;
    myFocusManager = focusManager;

    myDoneCallback = new ActionCallback();

    myTimestamp = myFocusManager.getTimestamp(true);
  }

  public ActionCallback getDoneCallback() {
    return myDoneCallback;
  }

  public final void run(){
    try{
      if (myTimestamp.isExpired()) {
        final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        if (owner != null && owner == myComponent) {
          myDoneCallback.setDone();
        } else {
          myDoneCallback.setRejected();
        }
      }


      final Window owner = myComponent != null ? SwingUtilities.getWindowAncestor(myComponent) : null;
      if(owner==null){
        myDoneCallback.setRejected();
        return;
      }

      final Window activeFrame = IdeFrameImpl.getActiveFrame();
      if (activeFrame != null && owner instanceof IdeFrameImpl && activeFrame != owner) {
        myDoneCallback.setRejected();
        return;
      }

      if(myComponent != null){
        myFocusManager.requestFocus(myComponent, myForced).notifyWhenDone(myDoneCallback).doWhenDone(new Runnable() {
          public void run() {
            // if owner is active window or it has active child window which isn't floating decorator then
            // don't bring owner window to font. If we will make toFront every time then it's possible
            // the following situation:
            // 1. user perform refactoring
            // 2. "Do not show preview" dialog is popping up.
            // 3. At that time "preview" tool window is being activated and modal "don't show..." dialog
            // isn't active.
            if(!owner.isActive()){
              final Window activeWindow=getActiveWindow(owner.getOwnedWindows());
              if(activeWindow == null || (activeWindow instanceof FloatingDecorator)){
                //Thread.dumpStack();
                //System.out.println("------------------------------------------------------");
                owner.toFront();
              }
            }
          }
        });
      } else {
        myDoneCallback.setRejected();
      }

    }finally{
      finish();
    }
  }

  /**
   * @return first active window from hierarchy with specified roots. Returns <code>null</code>
   * if there is no active window in the hierarchy.
   */
  @Nullable
  private static Window getActiveWindow(final Window[] windows) {
    for (Window window1 : windows) {
      Window window = window1;
      if (window.isShowing() && window.isActive()) {
        return window;
      }
      window = getActiveWindow(window.getOwnedWindows());
      if (window != null) {
        return window;
      }
    }
    return null;
  }
}