2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.ui.popup.util;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.ui.popup.JBPopup;
20 import org.jetbrains.annotations.NotNull;
21 import org.jetbrains.annotations.Nullable;
25 import java.lang.reflect.Method;
27 public class PopupUtil {
28 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.ui.popup.util.PopupUtil");
34 public static Component getOwner(@Nullable Component c) {
35 if (c == null) return null;
37 final Window wnd = SwingUtilities.getWindowAncestor(c);
38 if (wnd instanceof JWindow) {
39 final JRootPane root = ((JWindow)wnd).getRootPane();
40 final JBPopup popup = (JBPopup)root.getClientProperty(JBPopup.KEY);
41 if (popup == null) return c;
43 final Component owner = popup.getOwner();
44 if (owner == null) return c;
46 return getOwner(owner);
53 public static void setPopupType(@NotNull final PopupFactory factory, final int type) {
55 final Method method = PopupFactory.class.getDeclaredMethod("setPopupType", int.class);
56 method.setAccessible(true);
57 method.invoke(factory, type);
64 public static int getPopupType(@NotNull final PopupFactory factory) {
66 final Method method = PopupFactory.class.getDeclaredMethod("getPopupType");
67 method.setAccessible(true);
68 final Object result = method.invoke(factory);
69 return result instanceof Integer ? (Integer) result : -1;