diff --git a/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java b/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java
index 435e230b6a47..2bbca4d274fc 100644
--- a/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java
+++ b/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java
@@ -656,12 +656,7 @@ public class IdeEventQueue extends EventQueue {
try {
Object owner = getNativeFocusOwner.invoke(mgr);
if (owner instanceof Component) {
- Component nativeFocusOwner = (Component)owner;
- if (nativeFocusOwner instanceof Window) {
- showingWindow = (Window)nativeFocusOwner;
- } else {
- showingWindow = SwingUtilities.getWindowAncestor(nativeFocusOwner);
- }
+ showingWindow = UIUtil.getWindow((Component)owner);
}
}
catch (Exception e1) {
@@ -996,7 +991,7 @@ public class IdeEventQueue extends EventQueue {
@Override
public void run() {
try {
- final Window window = component instanceof Window ? (Window)component : SwingUtilities.windowForComponent(component);
+ final Window window = UIUtil.getWindow(component);
if (window == null || !window.isActive()) {
return;
}
diff --git a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaRootPaneUI.java b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaRootPaneUI.java
index 8b2be4cca78c..114422f579e8 100644
--- a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaRootPaneUI.java
+++ b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaRootPaneUI.java
@@ -15,6 +15,8 @@
*/
package com.intellij.ide.ui.laf.darcula.ui;
+import com.intellij.util.ui.UIUtil;
+
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
@@ -109,12 +111,7 @@ public class DarculaRootPaneUI extends BasicRootPaneUI {
private void installWindowListeners(JRootPane root, Component parent) {
- if (parent instanceof Window) {
- myWindow = (Window)parent;
- }
- else {
- myWindow = SwingUtilities.getWindowAncestor(parent);
- }
+ myWindow = UIUtil.getWindow(parent);
if (myWindow != null) {
if (myMouseInputListener == null) {
@@ -177,13 +174,7 @@ public class DarculaRootPaneUI extends BasicRootPaneUI {
});
}
- Window currWindow;
- if (parent instanceof Window) {
- currWindow = (Window)parent;
- }
- else {
- currWindow = SwingUtilities.getWindowAncestor(parent);
- }
+ Window currWindow = UIUtil.getWindow(parent);
if (myWindowListener != null) {
myCurrentWindow
.removeWindowListener(myWindowListener);
diff --git a/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java b/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java
index 512ee8729dca..90bd90d474d1 100644
--- a/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java
+++ b/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java
@@ -672,7 +672,7 @@ public class ApplicationImpl extends PlatformComponentManagerImpl implements App
@Override
@NotNull
public ModalityState getModalityStateForComponent(@NotNull Component c) {
- Window window = c instanceof Window ? (Window)c : SwingUtilities.windowForComponent(c);
+ Window window = UIUtil.getWindow(c);
if (window == null) return getNoneModalityState(); //?
return LaterInvocator.modalityStateForWindow(window);
}
diff --git a/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java b/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java
index 852cc7f2e5ff..ff2820a5cc90 100644
--- a/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java
+++ b/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java
@@ -249,12 +249,7 @@ public final class IdeKeyEventDispatcher implements Disposable {
* @throws IllegalArgumentException if component is null.
*/
public static boolean isModalContext(@NotNull Component component) {
- Window window;
- if (component instanceof Window) {
- window = (Window)component;
- } else {
- window = SwingUtilities.getWindowAncestor(component);
- }
+ Window window = UIUtil.getWindow(component);
if (window instanceof IdeFrameImpl) {
final Component pane = ((IdeFrameImpl) window).getGlassPane();
diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/ex/LayoutFocusTraversalPolicyExt.java b/platform/platform-impl/src/com/intellij/openapi/wm/ex/LayoutFocusTraversalPolicyExt.java
index 5a64a1cd8bd6..b427b94dc2bf 100644
--- a/platform/platform-impl/src/com/intellij/openapi/wm/ex/LayoutFocusTraversalPolicyExt.java
+++ b/platform/platform-impl/src/com/intellij/openapi/wm/ex/LayoutFocusTraversalPolicyExt.java
@@ -17,6 +17,7 @@ package com.intellij.openapi.wm.ex;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.registry.Registry;
+import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
@@ -56,12 +57,7 @@ public class LayoutFocusTraversalPolicyExt extends LayoutFocusTraversalPolicy{
@Nullable
public static LayoutFocusTraversalPolicyExt findWindowPolicy(Component c) {
- Window wnd;
- if (c instanceof Window) {
- wnd = (Window)c;
- } else {
- wnd = SwingUtilities.getWindowAncestor(c);
- }
+ Window wnd = UIUtil.getWindow(c);
final FocusTraversalPolicy policy = wnd.getFocusTraversalPolicy();
if (policy instanceof LayoutFocusTraversalPolicyExt) {
diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeGlassPaneImpl.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeGlassPaneImpl.java
index 1613953287b6..ecee36ecf18a 100644
--- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeGlassPaneImpl.java
+++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeGlassPaneImpl.java
@@ -109,8 +109,7 @@ public class IdeGlassPaneImpl extends JPanel implements IdeGlassPaneEx, IdeEvent
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent)e;
- Window eventWindow =
- me.getComponent() instanceof Window ? (Window)me.getComponent() : SwingUtilities.getWindowAncestor(me.getComponent());
+ Window eventWindow = UIUtil.getWindow(me.getComponent());
if (isContextMenu(eventWindow)) return false;
@@ -155,7 +154,7 @@ public class IdeGlassPaneImpl extends JPanel implements IdeGlassPaneEx, IdeEvent
MouseEvent me = (MouseEvent)e;
final Component meComponent = me.getComponent();
if (!dispatched && meComponent != null) {
- final Window eventWindow = meComponent instanceof Window ? (Window)meComponent : SwingUtilities.getWindowAncestor(meComponent);
+ final Window eventWindow = UIUtil.getWindow(meComponent);
if (eventWindow != SwingUtilities.getWindowAncestor(myRootPane)) {
return false;
}
diff --git a/platform/platform-impl/src/com/intellij/ui/FocusTrackback.java b/platform/platform-impl/src/com/intellij/ui/FocusTrackback.java
index 5c531a7b6ae8..fee094a77ba6 100644
--- a/platform/platform-impl/src/com/intellij/ui/FocusTrackback.java
+++ b/platform/platform-impl/src/com/intellij/ui/FocusTrackback.java
@@ -69,7 +69,7 @@ public class FocusTrackback {
private boolean myForcedRestore;
public FocusTrackback(@NotNull Object requestor, Component parent, boolean mustBeShown) {
- this(requestor, parent == null || parent instanceof Window ? (Window)parent : SwingUtilities.getWindowAncestor(parent), mustBeShown);
+ this(requestor, parent == null ? null : UIUtil.getWindow(parent), mustBeShown);
}
public FocusTrackback(@NotNull Object requestor, Window parent, boolean mustBeShown) {
@@ -263,7 +263,7 @@ public class FocusTrackback {
}
if (myParentWindow != null) {
- final Window to = toFocus instanceof Window ? (Window) toFocus : SwingUtilities.getWindowAncestor(toFocus);
+ final Window to = UIUtil.getWindow(toFocus);
if (to != null && UIUtil.findUltimateParent(to) == UIUtil.findUltimateParent(myParentWindow)) { // IDEADEV-34537
toFocus.requestFocus();
result.setDone();
@@ -490,7 +490,7 @@ public class FocusTrackback {
public static List getChildPopups(@NotNull final Component component) {
List result = new ArrayList();
- final Window window = component instanceof Window ? (Window)component: SwingUtilities.windowForComponent(component);
+ final Window window = UIUtil.getWindow(component);
if (window == null) return result;
final List stack = getCleanStackForRoot(findUtlimateParent(window));
diff --git a/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java b/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java
index 2f87b866e68a..02f0785971b7 100644
--- a/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java
+++ b/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java
@@ -1674,17 +1674,11 @@ public class AbstractPopup implements JBPopup {
if (owner == null) return false;
- Window wnd;
- if (owner instanceof Window) {
- wnd = (Window)owner;
- }
- else {
- wnd = SwingUtilities.getWindowAncestor(owner);
- }
+ Window wnd = UIUtil.getWindow(owner);
for (Component each : components) {
if (each != null && SwingUtilities.isDescendingFrom(owner, each)) {
- Window eachWindow = each instanceof Window ? (Window)each : SwingUtilities.getWindowAncestor(each);
+ Window eachWindow = UIUtil.getWindow(each);
if (eachWindow == wnd) {
return true;
}
diff --git a/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java b/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java
index 3b78e44b3f89..91aa6fc8d2b4 100644
--- a/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java
+++ b/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java
@@ -106,7 +106,7 @@ public interface PopupComponent {
throw new IllegalArgumentException("Popup owner must be showing");
}
- final Window wnd = owner instanceof Window ? (Window)owner: SwingUtilities.getWindowAncestor(owner);
+ final Window wnd = UIUtil.getWindow(owner);
if (wnd instanceof Frame) {
myDialog = new JDialog((Frame)wnd);
} else if (wnd instanceof Dialog) {
diff --git a/platform/util/src/com/intellij/util/ui/UIUtil.java b/platform/util/src/com/intellij/util/ui/UIUtil.java
index ad8d3eeb36c3..5e3402802df6 100644
--- a/platform/util/src/com/intellij/util/ui/UIUtil.java
+++ b/platform/util/src/com/intellij/util/ui/UIUtil.java
@@ -3217,4 +3217,16 @@ public class UIUtil {
textField.setColumns(4);
}
+
+ /**
+ * Returns the first window ancestor of the component.
+ * Note that this method returns the component itself if it is a window.
+ *
+ * @param component the component used to find corresponding window
+ * @return the first window ancestor of the component; or {@code null}
+ * if the component is not a window and is not contained inside a window
+ */
+ public static Window getWindow(Component component) {
+ return component instanceof Window ? (Window)component : SwingUtilities.getWindowAncestor(component);
+ }
}