summaryrefslogtreecommitdiff
path: root/platform/platform-api/src/com/intellij/openapi/wm/FocusCommand.java
blob: 6584184a1bb2272461780b6a341d54aee3c2a599 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright 2000-2014 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.
 */
package com.intellij.openapi.wm;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.popup.util.PopupUtil;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.ActiveRunnable;
import com.intellij.openapi.util.Expirable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * The container class for focus requests for <code>IdeFocusManager</code>
 * @see IdeFocusManager
 */
public abstract class FocusCommand extends ActiveRunnable implements Expirable {
  protected Component myDominationComponent;
  private Throwable myAllocation;
  private ActionCallback myCallback;
  private boolean myInvalidatesPendingFurtherRequestors = true;
  private Expirable myExpirable;

  public boolean isForced() {
    return myForced;
  }

  public void setForced(boolean forced) {
    myForced = forced;
  }

  private boolean myForced;

  protected FocusCommand() {
    saveAllocation();
  }

  protected FocusCommand(Component dominationComp) {
    myDominationComponent = dominationComp;
    saveAllocation();
  }

  protected FocusCommand(final Object object) {
    super(object);
    saveAllocation();
  }

  protected FocusCommand(final Object object, Component dominationComp) {
    super(object);
    myDominationComponent = dominationComp;
    saveAllocation();
  }

  protected FocusCommand(final Object[] objects) {
    super(objects);
    saveAllocation();
  }

  protected FocusCommand(final Object[] objects, Component dominationComp) {
    super(objects);
    myDominationComponent = dominationComp;
    saveAllocation();
  }

  public final ActionCallback getCallback() {
    return myCallback;
  }

  public final void setCallback(ActionCallback callback) {
    myCallback = callback;
  }

  public boolean isExpired() {
    return myExpirable != null && myExpirable.isExpired();
  }

  public boolean canExecuteOnInactiveApp() {
    return false;
  }

  @Nullable
  public KeyEventProcessor getProcessor() {
    return null;
  }

  public boolean invalidatesRequestors() {
    return myInvalidatesPendingFurtherRequestors;
  }

  public FocusCommand setExpirable(Expirable expirable) {
    myExpirable = expirable;
    return this;
  }

  public FocusCommand setToInvalidateRequestors(boolean invalidatesPendingFurtherRequestors) {
    myInvalidatesPendingFurtherRequestors = invalidatesPendingFurtherRequestors;
    return this;
  }

  @Nullable
  public final Component getDominationComponent() {
    return myDominationComponent;
  }

  public boolean dominatesOver(FocusCommand cmd) {
    final Component thisComponent = PopupUtil.getOwner(getDominationComponent());
    final Component thatComponent = PopupUtil.getOwner(cmd.getDominationComponent());

    if (thisComponent != null && thatComponent != null) {
      return thisComponent != thatComponent && SwingUtilities.isDescendingFrom(thisComponent, thatComponent);
    }

    return false;
  }

  public final FocusCommand saveAllocation() {
    myAllocation = new Exception();
    return this;
  }

  public Throwable getAllocation() {
    return myAllocation;
  }

  public boolean canFocusChangeFrom(@Nullable Component component) {
    return true;
  }

  @Override
  public String toString() {
    final Object[] objects = getEqualityObjects();
    return "FocusCommand objectCount=" + objects.length + " objects=" + Arrays.asList(objects);
  }

  public static class ByComponent extends FocusCommand {
    private Component myToFocus;

    public ByComponent(@Nullable Component toFocus) {
      this(toFocus, toFocus);
    }

    public ByComponent(@Nullable Component toFocus, @Nullable Component dominationComponent) {
      super(toFocus, dominationComponent);
      myToFocus = toFocus;
    }

    @NotNull
    public final ActionCallback run() {
      if (myToFocus != null) {
        if (Registry.is("actionSystem.doNotStealFocus")) {
          Window topWindow = SwingUtilities.windowForComponent(myToFocus);
          UIUtil.setAutoRequestFocus(topWindow, topWindow.isActive());
          while (topWindow.getOwner() != null) {
            topWindow = SwingUtilities.windowForComponent(topWindow);
            UIUtil.setAutoRequestFocus(topWindow, topWindow.isActive());
          }

          if (topWindow.isActive()) {
            if (!myToFocus.requestFocusInWindow()) {
              myToFocus.requestFocus();
            }
          } else {
            myToFocus.requestFocusInWindow();
          }

        } else {
          // This change seems reasonable to me. But as far as some implementations
          // can ignore the "forced" parameter we can get bad focus behaviour.
          // So let's start from mac.
          if (!(myToFocus.requestFocusInWindow())) {
            if (!SystemInfo.isMac || isForced() ) {
              myToFocus.requestFocus();
            }
          }
        }
      }
      clear();
      return new ActionCallback.Done();
    }

    private void clear() {
      myToFocus = null;
      myDominationComponent = null;
    }

    @Override
    public boolean isExpired() {
      if (myToFocus == null) {
        return true;
      }
      if (SwingUtilities.getWindowAncestor(myToFocus) == null) {
        clear();
        return true;
      }
      return false;
    }

    public Component getComponent() {
      return myToFocus;
    }

    @Override
    public boolean canFocusChangeFrom(@Nullable Component component) {
      DialogWrapper dialog = DialogWrapper.findInstance(component);
      return (dialog == null) || (dialog == DialogWrapper.findInstance(myToFocus));
    }
  }
}