Add utility method: com.intellij.util.ui.UIUtil.getWindow

This commit is contained in:
Sergey Malenkov
2014-11-27 15:08:10 +03:00
parent 1dcf60a518
commit cbb26891ca
10 changed files with 30 additions and 48 deletions
@@ -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;
}
@@ -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);
@@ -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);
}
@@ -249,12 +249,7 @@ public final class IdeKeyEventDispatcher implements Disposable {
* @throws IllegalArgumentException if <code>component</code> is <code>null</code>.
*/
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();
@@ -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) {
@@ -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;
}
@@ -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<JBPopup> getChildPopups(@NotNull final Component component) {
List<JBPopup> result = new ArrayList<JBPopup>();
final Window window = component instanceof Window ? (Window)component: SwingUtilities.windowForComponent(component);
final Window window = UIUtil.getWindow(component);
if (window == null) return result;
final List<FocusTrackback> stack = getCleanStackForRoot(findUtlimateParent(window));
@@ -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;
}
@@ -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) {
@@ -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);
}
}