From 81be032ebc26262ae227353e9fbf10d19867c4f2 Mon Sep 17 00:00:00 2001 From: Alexey Pegov Date: Tue, 10 Aug 2010 20:31:16 +0400 Subject: [PATCH] fix popups: heavy/medium: restore state of factory after popup creation + delegate setPopupType to the original factory --- .../openapi/ui/popup/util/PopupUtil.java | 29 +++++++++++++++++++ .../com/intellij/ide/ui/LafManagerImpl.java | 14 ++++----- .../com/intellij/ui/popup/AbstractPopup.java | 5 +++- .../com/intellij/ui/popup/PopupComponent.java | 17 ++++------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/ui/popup/util/PopupUtil.java b/platform/platform-api/src/com/intellij/openapi/ui/popup/util/PopupUtil.java index b8d9f8f87e14..02a92ed721f0 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/popup/util/PopupUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/popup/util/PopupUtil.java @@ -15,13 +15,17 @@ */ package com.intellij.openapi.ui.popup.util; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.ui.popup.JBPopup; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; +import java.lang.reflect.Method; public class PopupUtil { + private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.ui.popup.util.PopupUtil"); private PopupUtil() { } @@ -46,4 +50,29 @@ public class PopupUtil { } } + public static void setPopupType(@NotNull final PopupFactory factory, final int type) { + try { + final Method method = PopupFactory.class.getDeclaredMethod("setPopupType", int.class); + method.setAccessible(true); + method.invoke(factory, type); + } + catch (Throwable e) { + LOG.error(e); + } + } + + public static int getPopupType(@NotNull final PopupFactory factory) { + try { + final Method method = PopupFactory.class.getDeclaredMethod("getPopupType"); + method.setAccessible(true); + final Object result = method.invoke(factory); + return result instanceof Integer ? (Integer) result : -1; + } + catch (Throwable e) { + LOG.error(e); + } + + return -1; + } + } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/ide/ui/LafManagerImpl.java b/platform/platform-impl/src/com/intellij/ide/ui/LafManagerImpl.java index c3c279f8631b..8c491af6dda8 100644 --- a/platform/platform-impl/src/com/intellij/ide/ui/LafManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/ide/ui/LafManagerImpl.java @@ -21,6 +21,7 @@ import com.intellij.idea.StartupUtil; import com.intellij.openapi.components.*; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.ui.popup.util.PopupUtil; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.IconLoader; import com.intellij.openapi.util.SystemInfo; @@ -308,10 +309,8 @@ public final class LafManagerImpl extends LafManager implements ApplicationCompo } popupWeight = popupWeight.trim(); - final boolean heavyWeighPopup = HEAVY_WEIGHT_POPUP.equals(popupWeight); PopupFactory popupFactory; - final PopupFactory oldFactory = PopupFactory.getSharedInstance(); if (!(oldFactory instanceof OurPopupFactory)) { popupFactory = new OurPopupFactory() { @@ -322,20 +321,17 @@ public final class LafManagerImpl extends LafManager implements ApplicationCompo int y ) throws IllegalArgumentException { final Point point = fixPopupLocation(contents, x, y); - try { - final Method method = PopupFactory.class.getDeclaredMethod("setPopupType", int.class); - method.setAccessible(true); - method.invoke(oldFactory, heavyWeighPopup ? 2 : 1); - } - catch (Throwable e) { - LOG.error(e); + final int popupType = PopupUtil.getPopupType(this); + if (popupType >= 0) { + PopupUtil.setPopupType(oldFactory, popupType); } return oldFactory.getPopup(owner, contents, point.x, point.y); } }; + PopupUtil.setPopupType(popupFactory, HEAVY_WEIGHT_POPUP.equals(popupWeight) ? 2 : 1); PopupFactory.setSharedInstance(popupFactory); } 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 6f95abdfc7d0..db179130e98b 100644 --- a/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java +++ b/platform/platform-impl/src/com/intellij/ui/popup/AbstractPopup.java @@ -710,7 +710,10 @@ public class AbstractPopup implements JBPopup { } if (myWindow != null) { - if (!myMayBeParent) { + // dialogwrapper-based popups do this internally through peer, + // for other popups like jdialog-based we should exclude them manually, but + // we still have to be able to use IdeFrame as parent + if (!myMayBeParent && !(myWindow instanceof Frame)) { WindowManager.getInstance().doNotSuggestAsParent(myWindow); } } 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 c62669b4b13b..445c642a0e0a 100644 --- a/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java +++ b/platform/platform-impl/src/com/intellij/ui/popup/PopupComponent.java @@ -16,11 +16,11 @@ package com.intellij.ui.popup; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.ui.popup.util.PopupUtil; import com.intellij.util.ReflectionUtil; import javax.swing.*; import java.awt.*; -import java.lang.reflect.Method; public interface PopupComponent { @@ -53,17 +53,12 @@ public interface PopupComponent { public PopupComponent getPopup(Component owner, Component content, int x, int y) { final PopupFactory factory = PopupFactory.getSharedInstance(); - try { - final Method method = PopupFactory.class.getDeclaredMethod("setPopupType", int.class); - method.setAccessible(true); - method.invoke(factory, 2); + final int oldType = PopupUtil.getPopupType(factory); + PopupUtil.setPopupType(factory, 2); + final Popup popup = factory.getPopup(owner, content, x, y); + if (oldType >= 0) PopupUtil.setPopupType(factory, oldType); - } - catch (Throwable e) { - LOG.error(e); - } - - return new AwtPopupWrapper(factory.getPopup(owner, content, x, y)); + return new AwtPopupWrapper(popup); } public boolean isNativePopup() {