summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/util/IJSwingUtilities.java
blob: 60c5f90c8b731aa4d2f5ca9e840c849a287c6239 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright 2000-2009 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.util;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.components.OrphanGuardian;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.FilteringIterator;
import com.intellij.util.ui.UIUtil;
import gnu.trove.TIntStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

public class IJSwingUtilities {
  public static void invoke(Runnable runnable) {
    if (ApplicationManager.getApplication().isDispatchThread()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().invokeLater(runnable, ModalityState.NON_MODAL);
    }
  }

  /**
   * @return true if javax.swing.SwingUtilities.findFocusOwner(component) != null
   */
  public static boolean hasFocus(Component component) {
    Component focusOwner = findFocusOwner(component);
    return focusOwner != null;
  }

  private static Component findFocusOwner(Component c) {
    Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

    // verify focusOwner is a descendant of c
    for (Component temp = focusOwner; temp != null; temp = (temp instanceof Window) ? null : temp.getParent())
    {
      if (temp == c) {
        return focusOwner;
      }
    }

    return null;
  }

  /**
   * @return true if window ancestor of component was most recent focused window and most recent focused component
   * in that window was descended from component
   */
  public static boolean hasFocus2(Component component) {
    WindowManagerEx windowManager = WindowManagerEx.getInstanceEx();
    Window activeWindow=null;
    if (windowManager != null) {
      activeWindow = windowManager.getMostRecentFocusedWindow();
    }
    if(activeWindow==null){
      return false;
    }
    Component focusedComponent = windowManager.getFocusedComponent(activeWindow);
    if (focusedComponent == null) {
      return false;
    }

    return SwingUtilities.isDescendingFrom(focusedComponent, component);
  }

  /**
   * This method is copied from <code>SwingUtilities</code>.
   * Returns index of the first occurrence of <code>mnemonic</code>
   * within string <code>text</code>. Matching algorithm is not
   * case-sensitive.
   *
   * @param text The text to search through, may be null
   * @param mnemonic The mnemonic to find the character for.
   * @return index into the string if exists, otherwise -1
   */
  public static int findDisplayedMnemonicIndex(String text, int mnemonic) {
    if (text == null || mnemonic == '\0') {
      return -1;
    }

    char uc = Character.toUpperCase((char)mnemonic);
    char lc = Character.toLowerCase((char)mnemonic);

    int uci = text.indexOf(uc);
    int lci = text.indexOf(lc);

    if (uci == -1) {
      return lci;
    } else if(lci == -1) {
      return uci;
    } else {
      return (lci < uci) ? lci : uci;
    }
  }

  public static Iterator<Component> getParents(final Component component) {
    return new Iterator<Component>() {
      private Component myCurrent = component;
      public boolean hasNext() {
        return myCurrent != null && myCurrent.getParent() != null;
      }

      public Component next() {
        myCurrent = myCurrent.getParent();
        return myCurrent;
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }

  /**
   * @param component - parent component, won't be reached by iterator.
   * @return Component tree traverse {@link Iterator}.
   */
  public static Iterator<Component> getChildren(final Container component) {
    return new Iterator<Component>() {
      private Container myCurrentParent = component;
      private final TIntStack myState = new TIntStack();
      private int myCurrentIndex = 0;

      public boolean hasNext() {
        return hasNextChild();
      }

      public Component next() {
        Component next = myCurrentParent.getComponent(myCurrentIndex);
        myCurrentIndex++;
        if (next instanceof Container) {
          Container container = ((Container)next);
          if (container.getComponentCount() > 0) {
            myState.push(myCurrentIndex);
            myCurrentIndex = 0;
            myCurrentParent = container;
          }
        }
        while (!hasNextChild()) {
          if (myState.size() == 0) break;
          myCurrentIndex = myState.pop();
          myCurrentParent = myCurrentParent.getParent();
        }
        return next;
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }

      private boolean hasNextChild() {
        return myCurrentParent.getComponentCount() > myCurrentIndex;
      }
    };
  }

  @Nullable
  public static <T extends Component> T findParentOfType(Component focusOwner, Class<T> aClass) {
    return (T)ContainerUtil.find(getParents(focusOwner), (FilteringIterator.InstanceOf<T>)FilteringIterator.instanceOf(aClass));

  }
  @Nullable
  public static Component findParentByInterface(Component focusOwner, Class aClass) {
    return ContainerUtil.find(getParents(focusOwner), FilteringIterator.instanceOf(aClass));
  }

  public static void adjustComponentsOnMac(@Nullable JComponent component) {
    adjustComponentsOnMac(null, component);
  }


  public static void adjustComponentsOnMac(@Nullable JLabel label, @Nullable JComponent component) {
    if (component == null) return;
    if (!UIUtil.isUnderAquaLookAndFeel()) return;

    if (component instanceof JComboBox) {
      UIUtil.addInsets(component, new Insets(0,-2,0,0));
      if (label != null) {
        UIUtil.addInsets(label, new Insets(0,2,0,0));
      }
    }
    if (component instanceof JCheckBox) {
      UIUtil.addInsets(component, new Insets(0,-5,0,0));
    }
    if (component instanceof JTextField || component instanceof EditorTextField) {
      if (label != null) {
        UIUtil.addInsets(label, new Insets(0,3,0,0));
      }
    }
  }

  public static HyperlinkEvent createHyperlinkEvent(@Nullable String href, @NotNull Object source) {
    URL url = null;
    try {
      url = new URL(href);
    }
    catch (MalformedURLException ignored) {
    }
    return new HyperlinkEvent(source, HyperlinkEvent.EventType.ACTIVATED, url, href);
  }

  /**
   * A copy of javax.swing.SwingUtilities#updateComponentTreeUI that invokes children updateUI() first

   * @param c component
   * @see javax.swing.SwingUtilities#updateComponentTreeUI
   */
  public static void updateComponentTreeUI(Component c) {
    updateComponentTreeUI0(c);
    c.invalidate();
    c.validate();
    c.repaint();
  }

  private static final Consumer<JComponent> UI_TREE_UPDATER = new Consumer<JComponent>() {
    @Override
    public void consume(JComponent component) {
      updateComponentTreeUI0(component);
    }
  };

  private static void updateComponentTreeUI0(Component c) {
    Component[] children = null;
    if (c instanceof JMenu) {
      children = ((JMenu)c).getMenuComponents();
    }
    else if (c instanceof Container) {
      children = ((Container)c).getComponents();
    }
    if (children != null) {
      for (Component aChildren : children) {
        updateComponentTreeUI0(aChildren);
      }
    }
    if (c instanceof JComponent) {
      JComponent jc = (JComponent)c;
      OrphanGuardian orphans = (OrphanGuardian)jc.getClientProperty(OrphanGuardian.CLIENT_PROPERTY_KEY);
      if (orphans != null) {
        orphans.iterateOrphans(UI_TREE_UPDATER);
      }
      jc.updateUI();
      JPopupMenu jpm = jc.getComponentPopupMenu();
      if (jpm != null && jpm.isVisible() && jpm.getInvoker() == jc) {
        updateComponentTreeUI(jpm);
      }
    }
  }
}