Example usage for com.intellij.openapi.wm IdeFocusManager getInstance

List of usage examples for com.intellij.openapi.wm IdeFocusManager getInstance

Introduction

In this page you can find the example usage for com.intellij.openapi.wm IdeFocusManager getInstance.

Prototype

public static IdeFocusManager getInstance(@Nullable Project project) 

Source Link

Usage

From source file:com.intellij.refactoring.wrapreturnvalue.WrapReturnValueDialog.java

License:Apache License

private void toggleRadioEnablement() {
    UIUtil.setEnabled(myExistingClassPanel, useExistingClassButton.isSelected(), true);
    UIUtil.setEnabled(myNewClassPanel, createNewClassButton.isSelected(), true);
    UIUtil.setEnabled(myCreateInnerPanel, myCreateInnerClassButton.isSelected(), true);
    final IdeFocusManager focusManager = IdeFocusManager.getInstance(myProject);
    if (useExistingClassButton.isSelected()) {
        focusManager.requestFocus(existingClassField, true);
    } else if (myCreateInnerClassButton.isSelected()) {
        focusManager.requestFocus(myInnerClassNameTextField, true);
    } else {//  ww w . jav a  2s.  c o  m
        focusManager.requestFocus(classNameField, true);
    }
    validateButtons();
}

From source file:com.intellij.translation.TranslationManager.java

License:Apache License

private void closeTranslationHint() {
    JBPopup hint = getTranslationHint();
    if (hint == null) {
        return;//w  ww  .ja v  a 2s.c o  m
    }
    myCloseOnSneeze = false;
    hint.cancel();
    Component toFocus = myPreviouslyFocused;
    hint.cancel();
    if (toFocus != null) {
        IdeFocusManager.getInstance(myProject).requestFocus(toFocus, true);
    }
}

From source file:com.intellij.translation.TranslationManager.java

License:Apache License

private void doShowTranslation(@NotNull String queryText, boolean requestFocus,
        @Nullable final Runnable closeCallback) {
    final Project project = myProject;
    if (!project.isOpen())
        return;//ww w  .ja  va 2s.  c o  m

    myPreviouslyFocused = WindowManagerEx.getInstanceEx().getFocusedComponent(project);

    JBPopup _oldHint = getTranslationHint();

    if (myToolWindow == null
            && PropertiesComponent.getInstance().isTrueValue(SHOW_TRANSLATION_IN_TOOL_WINDOW)) {
        createToolWindow(null, null);
    } else if (myToolWindow != null) {
        Content content = myToolWindow.getContentManager().getSelectedContent();
        if (content != null) {
            TranslationComponent component = (TranslationComponent) content.getComponent();
            boolean samQuery = queryText.equals(component.getQuery());
            if (samQuery) {
                JComponent preferredFocusableComponent = content.getPreferredFocusableComponent();
                // focus toolwindow on the second actionPerformed
                boolean focus = requestFocus || CommandProcessor.getInstance().getCurrentCommand() != null;
                if (preferredFocusableComponent != null && focus) {
                    IdeFocusManager.getInstance(myProject).requestFocus(preferredFocusableComponent, true);
                }
            } else {
                content.setDisplayName(getTitle(null, true));
                fetchTranslation(getDefaultCollector(queryText), component, true);
            }
        }
        if (!myToolWindow.isVisible()) {

            myToolWindow.show(null);
        }
    } else if (_oldHint != null && _oldHint.isVisible() && _oldHint instanceof AbstractPopup) {
        TranslationComponent oldComponent = (TranslationComponent) ((AbstractPopup) _oldHint).getComponent();
        fetchTranslation(getDefaultCollector(queryText), oldComponent);
    } else {
        showInPopup(queryText, requestFocus, closeCallback);
    }
}

From source file:com.intellij.ui.ComboboxEditorTextField.java

License:Apache License

private void repaintComboBox() {
    // TODO://from w  ww. j  a v a2s .  c o  m
    if (UIUtil.isUnderBuildInLaF() || (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel())) {
        IdeFocusManager.getInstance(getProject()).doWhenFocusSettlesDown(new Runnable() {
            @Override
            public void run() {
                final Container parent = getParent();
                if (parent != null)
                    parent.repaint();
            }
        });
    }
}

From source file:com.intellij.ui.content.impl.ContentManagerImpl.java

License:Apache License

private IdeFocusManager getFocusManager() {
    return IdeFocusManager.getInstance(myProject);
}

From source file:com.intellij.ui.docking.impl.DockManagerImpl.java

License:Apache License

@Override
public String getDimensionKeyForFocus(@NotNull String key) {
    Component owner = IdeFocusManager.getInstance(myProject).getFocusOwner();
    if (owner == null)
        return key;

    DockWindow wnd = myWindows.getValue(getContainerFor(owner));

    return wnd != null ? key + "#" + wnd.myId : key;
}

From source file:com.intellij.ui.FocusTrackback.java

License:Apache License

public void restoreFocus() {
    final Application app = ApplicationManager.getApplication();
    if (app == null || wrongOS() || myConsumed || isSheduledForRestore())
        return;//from w  w  w .ja  v a2s.c  om

    Project project = null;
    DataContext context = myParentWindow == null ? DataManager.getInstance().getDataContext()
            : DataManager.getInstance().getDataContext(myParentWindow);
    if (context != null) {
        project = CommonDataKeys.PROJECT.getData(context);
    }

    mySheduledForRestore = true;
    final List<FocusTrackback> stack = getCleanStackForRoot();
    final int index = stack.indexOf(this);
    for (int i = index - 1; i >= 0; i--) {
        if (stack.get(i).isSheduledForRestore()) {
            dispose();
            return;
        }
    }

    if (project != null && !project.isDisposed()) {
        final IdeFocusManager focusManager = IdeFocusManager.getInstance(project);
        cleanParentWindow();
        final Project finalProject = project;
        focusManager.requestFocus(new MyFocusCommand(), myForcedRestore).doWhenProcessed(new Runnable() {
            public void run() {
                dispose();
            }
        }).doWhenRejected(new Runnable() {
            @Override
            public void run() {
                focusManager.revalidateFocus(new ExpirableRunnable.ForProject(finalProject) {
                    @Override
                    public void run() {
                        if (UIUtil.isMeaninglessFocusOwner(focusManager.getFocusOwner())) {
                            focusManager.requestDefaultFocus(false);
                        }
                    }
                });
            }
        });
    } else {
        // no ide focus manager, so no way -- do just later
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                _restoreFocus();
                dispose();
            }
        });
    }
}

From source file:com.intellij.ui.JBTabsPaneImpl.java

License:Apache License

public JBTabsPaneImpl(@Nullable Project project, int tabPlacement, @NotNull Disposable parent) {
    myTabs = new JBEditorTabs(project, ActionManager.getInstance(),
            project == null ? null : IdeFocusManager.getInstance(project), parent) {
        @Override/* ww  w.jav  a2  s. c om*/
        public boolean isAlphabeticalMode() {
            return false;
        }

        @Override
        public boolean supportsCompression() {
            return false;
        }

        @Override
        protected void doPaintBackground(Graphics2D g2d, Rectangle clip) {
            super.doPaintBackground(g2d, clip);
            if (getTabsPosition() == JBTabsPosition.top && isSingleRow()) {
                int maxOffset = 0;
                int maxLength = 0;

                for (int i = getVisibleInfos().size() - 1; i >= 0; i--) {
                    TabInfo visibleInfo = getVisibleInfos().get(i);
                    TabLabel tabLabel = myInfo2Label.get(visibleInfo);
                    Rectangle r = tabLabel.getBounds();
                    if (r.width == 0 || r.height == 0)
                        continue;
                    maxOffset = r.x + r.width;
                    maxLength = r.height;
                    break;
                }

                maxOffset++;
                g2d.setPaint(UIUtil.getPanelBackground());
                if (getFirstTabOffset() > 0) {
                    g2d.fillRect(clip.x, clip.y, clip.x + getFirstTabOffset() - 1,
                            clip.y + maxLength - myTabs.getActiveTabUnderlineHeight());
                }
                g2d.fillRect(clip.x + maxOffset, clip.y, clip.width - maxOffset,
                        clip.y + maxLength - myTabs.getActiveTabUnderlineHeight());
                g2d.setPaint(new JBColor(Gray._181, UIUtil.getPanelBackground()));
                g2d.drawLine(clip.x + maxOffset, clip.y + maxLength - myTabs.getActiveTabUnderlineHeight(),
                        clip.x + clip.width, clip.y + maxLength - myTabs.getActiveTabUnderlineHeight());
                g2d.setPaint(UIUtil.getPanelBackground());
                g2d.drawLine(clip.x, clip.y + maxLength, clip.width, clip.y + maxLength);
            }
        }

        @Override
        protected void paintSelectionAndBorder(Graphics2D g2d) {
            super.paintSelectionAndBorder(g2d);
        }
    };
    myTabs.setFirstTabOffset(10);

    myTabs.addListener(new TabsListener.Adapter() {
        @Override
        public void selectionChanged(TabInfo oldSelection, TabInfo newSelection) {
            fireChanged(new ChangeEvent(myTabs));
        }
    }).getPresentation().setPaintBorder(1, 1, 1, 1).setTabSidePaintBorder(2)
            .setPaintFocus(UIUtil.isUnderBuildInLaF()).setAlwaysPaintSelectedTab(UIUtil.isUnderBuildInLaF())
            .setGhostsAlwaysVisible(true);

    setTabPlacement(tabPlacement);
}

From source file:com.intellij.ui.popup.AbstractPopup.java

License:Apache License

public void show(Component owner, int aScreenX, int aScreenY, final boolean considerForcedXY) {
    if (ApplicationManagerEx.getApplicationEx() != null
            && ApplicationManager.getApplication().isHeadlessEnvironment())
        return;/*from ww w  .  j  a  v  a  2 s.  com*/
    if (isDisposed()) {
        throw new IllegalStateException("Popup was already disposed. Recreate a new instance to show again");
    }

    assert ApplicationManager.getApplication().isDispatchThread();
    assert myState == State.INIT : "Popup was already shown. Recreate a new instance to show again.";

    debugState("show popup", State.INIT);
    myState = State.SHOWING;

    installWindowHook(this);
    installProjectDisposer();
    addActivity();

    final Component prevOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

    final boolean shouldShow = beforeShow();
    if (!shouldShow) {
        removeActivity();
        debugState("rejected to show popup", State.SHOWING);
        myState = State.INIT;
        return;
    }

    prepareToShow();

    if (myInStack) {
        myFocusTrackback = new FocusTrackback(this, owner, true);
        myFocusTrackback.setMustBeShown(true);
    }

    Dimension sizeToSet = null;

    if (myDimensionServiceKey != null) {
        sizeToSet = DimensionService.getInstance().getSize(myDimensionServiceKey, myProject);
    }

    if (myForcedSize != null) {
        sizeToSet = myForcedSize;
    }

    if (sizeToSet != null) {
        sizeToSet.width = Math.max(sizeToSet.width, myContent.getMinimumSize().width);
        sizeToSet.height = Math.max(sizeToSet.height, myContent.getMinimumSize().height);

        myContent.setSize(sizeToSet);
        myContent.setPreferredSize(sizeToSet);
    }

    Point xy = new Point(aScreenX, aScreenY);
    boolean adjustXY = true;
    if (myUseDimServiceForXYLocation && myDimensionServiceKey != null) {
        final Point storedLocation = DimensionService.getInstance().getLocation(myDimensionServiceKey,
                myProject);
        if (storedLocation != null) {
            xy = storedLocation;
            adjustXY = false;
        }
    }

    if (adjustXY) {
        final Insets insets = myContent.getInsets();
        if (insets != null) {
            xy.x -= insets.left;
            xy.y -= insets.top;
        }
    }

    if (considerForcedXY && myForcedLocation != null) {
        xy = myForcedLocation;
    }

    if (myLocateByContent) {
        final Dimension captionSize = myHeaderPanel.getPreferredSize();
        xy.y -= captionSize.height;
    }

    Rectangle targetBounds = new Rectangle(xy, myContent.getPreferredSize());
    Rectangle original = new Rectangle(targetBounds);
    if (myLocateWithinScreen) {
        ScreenUtil.moveToFit(targetBounds, ScreenUtil.getScreenRectangle(aScreenX, aScreenY), null);
    }

    if (myMouseOutCanceller != null) {
        myMouseOutCanceller.myEverEntered = targetBounds.equals(original);
    }

    myOwner = getFrameOrDialog(owner); // use correct popup owner for non-modal dialogs too
    if (myOwner == null) {
        myOwner = owner;
    }

    myRequestorComponent = owner;

    boolean forcedDialog = myMayBeParent
            || SystemInfo.isMac && !(myOwner instanceof IdeFrame) && myOwner != null && myOwner.isShowing();

    PopupComponent.Factory factory = getFactory(myForcedHeavyweight || myResizable, forcedDialog);
    myNativePopup = factory.isNativePopup();
    Component popupOwner = myOwner;
    if (popupOwner instanceof RootPaneContainer
            && !(popupOwner instanceof IdeFrame && !Registry.is("popup.fix.ide.frame.owner"))) {
        // JDK uses cached heavyweight popup for a window ancestor
        RootPaneContainer root = (RootPaneContainer) popupOwner;
        popupOwner = root.getRootPane();
        LOG.debug("popup owner fixed for JDK cache");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("expected preferred size: " + myContent.getPreferredSize());
    }
    myPopup = factory.getPopup(popupOwner, myContent, targetBounds.x, targetBounds.y, this);
    if (LOG.isDebugEnabled()) {
        LOG.debug("  actual preferred size: " + myContent.getPreferredSize());
    }
    if ((targetBounds.width != myContent.getWidth()) || (targetBounds.height != myContent.getHeight())) {
        // JDK uses cached heavyweight popup that is not initialized properly
        LOG.debug("the expected size is not equal to the actual size");
        Window popup = myPopup.getWindow();
        if (popup != null) {
            popup.setSize(targetBounds.width, targetBounds.height);
            if (myContent.getParent().getComponentCount() != 1) {
                LOG.debug("unexpected count of components in heavy-weight popup");
            }
        } else {
            LOG.debug("cannot fix size for non-heavy-weight popup");
        }
    }

    if (myResizable) {
        final JRootPane root = myContent.getRootPane();
        final IdeGlassPaneImpl glass = new IdeGlassPaneImpl(root);
        root.setGlassPane(glass);

        int i = Registry.intValue("ide.popup.resizable.border.sensitivity", 4);
        WindowResizeListener resizeListener = new WindowResizeListener(myContent,
                myMovable ? new Insets(i, i, i, i) : new Insets(0, 0, i, i),
                isToDrawMacCorner() ? AllIcons.General.MacCorner : null) {
            private Cursor myCursor;

            @Override
            protected void setCursor(Component content, Cursor cursor) {
                if (myCursor != cursor || myCursor != Cursor.getDefaultCursor()) {
                    glass.setCursor(cursor, this);
                    myCursor = cursor;
                }
            }
        };
        glass.addMousePreprocessor(resizeListener, this);
        glass.addMouseMotionPreprocessor(resizeListener, this);
        myResizeListener = resizeListener;
    }

    if (myCaption != null && myMovable) {
        final WindowMoveListener moveListener = new WindowMoveListener(myCaption) {
            @Override
            public void mousePressed(final MouseEvent e) {
                if (e.isConsumed())
                    return;
                if (UIUtil.isCloseClick(e) && myCaption.isWithinPanel(e)) {
                    cancel();
                } else {
                    super.mousePressed(e);
                }
            }
        };
        myCaption.addMouseListener(moveListener);
        myCaption.addMouseMotionListener(moveListener);
        final MyContentPanel saved = myContent;
        Disposer.register(this, new Disposable() {
            @Override
            public void dispose() {
                ListenerUtil.removeMouseListener(saved, moveListener);
                ListenerUtil.removeMouseMotionListener(saved, moveListener);
            }
        });
        myMoveListener = moveListener;
    }

    for (JBPopupListener listener : myListeners) {
        listener.beforeShown(new LightweightWindowEvent(this));
    }

    myPopup.setRequestFocus(myRequestFocus);
    myPopup.show();

    final Window window = getContentWindow(myContent);

    myWindow = window;

    myWindowListener = new MyWindowListener();
    window.addWindowListener(myWindowListener);

    if (myFocusable) {
        window.setFocusableWindowState(true);
        window.setFocusable(true);
    }

    if (myWindow != null) {
        // 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);
        }
    }

    setMinimumSize(myMinSize);

    final Runnable afterShow = new Runnable() {
        @Override
        public void run() {
            if (myPreferredFocusedComponent != null && myInStack && myFocusable) {
                myFocusTrackback.registerFocusComponent(myPreferredFocusedComponent);
                if (myPreferredFocusedComponent instanceof JTextComponent) {
                    IJSwingUtilities.moveMousePointerOn(myPreferredFocusedComponent);
                }
            }

            removeActivity();

            afterShow();

        }
    };

    if (myRequestFocus) {
        getFocusManager().requestFocus(new FocusCommand() {
            @NotNull
            @Override
            public ActionCallback run() {
                if (isDisposed()) {
                    removeActivity();
                    return ActionCallback.DONE;
                }

                _requestFocus();

                final ActionCallback result = new ActionCallback();

                final Runnable afterShowRunnable = new Runnable() {
                    @Override
                    public void run() {
                        afterShow.run();
                        result.setDone();
                    }
                };
                if (myNativePopup) {
                    final FocusRequestor furtherRequestor = getFocusManager().getFurtherRequestor();
                    //noinspection SSBasedInspection
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            if (isDisposed()) {
                                result.setRejected();
                                return;
                            }

                            furtherRequestor.requestFocus(new FocusCommand() {
                                @NotNull
                                @Override
                                public ActionCallback run() {
                                    if (isDisposed()) {
                                        return ActionCallback.REJECTED;
                                    }

                                    _requestFocus();

                                    afterShowRunnable.run();

                                    return ActionCallback.DONE;
                                }
                            }, true).notify(result).doWhenProcessed(new Runnable() {
                                @Override
                                public void run() {
                                    removeActivity();
                                }
                            });
                        }
                    });
                } else {
                    afterShowRunnable.run();
                }

                return result;
            }
        }, true).doWhenRejected(new Runnable() {
            @Override
            public void run() {
                afterShow.run();
            }
        });
    } else {
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (isDisposed()) {
                    removeActivity();
                    return;
                }

                if (X_WINDOW_FOCUS_BUG && !myRequestFocus && prevOwner != null
                        && Registry.is("actionSystem.xWindow.remove.focus.from.nonFocusable.popups")) {
                    new Alarm().addRequest(new Runnable() {
                        @Override
                        public void run() {
                            if (isFocused()) {
                                IdeFocusManager.getInstance(myProject).requestFocus(prevOwner, false);
                            }
                        }
                    }, Registry.intValue("actionSystem.xWindow.remove.focus.from.nonFocusable.popups.delay"));
                }

                afterShow.run();
            }
        });
    }
    debugState("popup shown", State.SHOWING);
    myState = State.SHOWN;
}

From source file:com.intellij.ui.popup.AbstractPopup.java

License:Apache License

private IdeFocusManager getFocusManager() {
    if (myProject != null) {
        return IdeFocusManager.getInstance(myProject);
    }/*ww w.  ja v  a2  s  .c  o  m*/
    if (myOwner != null) {
        return IdeFocusManager.findInstanceByComponent(myOwner);
    }
    return IdeFocusManager.findInstance();
}