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.find.actions.ShowUsagesAction.java

License:Apache License

private void hint(@Nullable final Editor editor, @NotNull final String hint,
        @NotNull final FindUsagesHandler handler, @NotNull final RelativePoint popupPosition,
        final int maxUsages, @NotNull final FindUsagesOptions options, final boolean isWarning) {
    final Project project = handler.getProject();
    //opening editor is performing in invokeLater
    IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
        @Override//from ww w. j  av a2  s  .com
        public void run() {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    // after new editor created, some editor resizing events are still bubbling. To prevent hiding hint, invokeLater this
                    IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
                        @Override
                        public void run() {
                            showHint(editor, hint, handler, popupPosition, maxUsages, options, isWarning);
                        }
                    });
                }
            };
            if (editor == null)
                runnable.run();
            else
                editor.getScrollingModel().runActionOnScrollingFinished(runnable);
        }
    });
}

From source file:com.intellij.find.EditorSearchComponent.java

License:Apache License

private void requestFocus(Component c) {
    IdeFocusManager.getInstance(myProject).requestFocus(c, true);
}

From source file:com.intellij.find.EditorSearchComponent.java

License:Apache License

public void close() {
    IdeFocusManager.getInstance(myProject).requestFocus(myEditor.getContentComponent(), false);

    myLivePreviewController.dispose();
    myEditor.setHeaderComponent(null);
}

From source file:com.intellij.ide.actions.GotoActionBase.java

License:Apache License

protected static Pair<String, Integer> getInitialText(boolean useEditorSelection, AnActionEvent e) {
    final String predefined = e.getData(PlatformDataKeys.PREDEFINED_TEXT);
    if (!StringUtil.isEmpty(predefined)) {
        return Pair.create(predefined, 0);
    }//  ww w.  j  av a2  s  .co  m
    if (useEditorSelection) {
        final Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor != null) {
            final String selectedText = editor.getSelectionModel().getSelectedText();
            if (selectedText != null && !selectedText.contains("\n")) {
                return Pair.create(selectedText, 0);
            }
        }
    }

    final String query = e.getData(SpeedSearchSupply.SPEED_SEARCH_CURRENT_QUERY);
    if (!StringUtil.isEmpty(query)) {
        return Pair.create(query, 0);
    }

    final Component focusOwner = IdeFocusManager.getInstance(getEventProject(e)).getFocusOwner();
    if (focusOwner instanceof JComponent) {
        final SpeedSearchSupply supply = SpeedSearchSupply.getSupply((JComponent) focusOwner);
        if (supply != null) {
            return Pair.create(supply.getEnteredPrefix(), 0);
        }
    }

    if (myInAction != null) {
        final Pair<String, Integer> lastString = ourLastStrings.get(myInAction);
        if (lastString != null) {
            return lastString;
        }
    }

    return Pair.create("", 0);
}

From source file:com.intellij.ide.actions.SearchEverywhereAction.java

License:Apache License

private void doNavigate(final int index) {
    final Project project = CommonDataKeys.PROJECT
            .getData(DataManager.getInstance().getDataContext(getField().getTextEditor()));
    final Executor executor = ourShiftIsPressed.get() ? DefaultRunExecutor.getRunExecutorInstance()
            : ExecutorRegistry.getInstance().getExecutorById(ToolWindowId.DEBUG);
    assert project != null;
    final SearchListModel model = getModel();
    if (isMoreItem(index)) {
        final String pattern = myPopupField.getText();
        WidgetID wid = null;//from   w  ww. ja v a2 s  .  co  m
        if (index == model.moreIndex.classes)
            wid = WidgetID.CLASSES;
        else if (index == model.moreIndex.files)
            wid = WidgetID.FILES;
        else if (index == model.moreIndex.settings)
            wid = WidgetID.SETTINGS;
        else if (index == model.moreIndex.actions)
            wid = WidgetID.ACTIONS;
        else if (index == model.moreIndex.symbols)
            wid = WidgetID.SYMBOLS;
        else if (index == model.moreIndex.runConfigurations)
            wid = WidgetID.RUN_CONFIGURATIONS;
        if (wid != null) {
            final WidgetID widgetID = wid;
            myCurrentWorker.doWhenProcessed(new Runnable() {
                @Override
                public void run() {
                    myCalcThread = new CalcThread(project, pattern, true);
                    myPopupActualWidth = 0;
                    myCurrentWorker = myCalcThread.insert(index, widgetID);
                }
            });

            return;
        }
    }
    final String pattern = getField().getText();
    final Object value = myList.getSelectedValue();
    saveHistory(project, pattern, value);
    IdeFocusManager focusManager = IdeFocusManager.findInstanceByComponent(getField().getTextEditor());
    if (myPopup != null && myPopup.isVisible()) {
        myPopup.cancel();
    }

    if (value instanceof BooleanOptionDescription) {
        final BooleanOptionDescription option = (BooleanOptionDescription) value;
        option.setOptionState(!option.isOptionEnabled());
        myList.revalidate();
        myList.repaint();
        getField().requestFocus();
        return;
    }

    if (value instanceof OptionsTopHitProvider) {
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                getField().setText("#" + ((OptionsTopHitProvider) value).getId() + " ");
            }
        });
        return;
    }
    Runnable onDone = null;

    AccessToken token = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        if (value instanceof PsiElement) {
            onDone = new Runnable() {
                @Override
                public void run() {
                    NavigationUtil.activateFileWithPsiElement((PsiElement) value, true);
                }
            };
            return;
        } else if (isVirtualFile(value)) {
            onDone = new Runnable() {
                @Override
                public void run() {
                    OpenSourceUtil.navigate(true, new OpenFileDescriptor(project, (VirtualFile) value));
                }
            };
            return;
        } else if (isActionValue(value) || isSetting(value) || isRunConfiguration(value)) {
            focusManager.requestDefaultFocus(true);
            final Component comp = myContextComponent;
            final AnActionEvent event = myActionEvent;
            IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
                @Override
                public void run() {
                    Component c = comp;
                    if (c == null) {
                        c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                    }

                    if (isRunConfiguration(value)) {
                        ((ChooseRunConfigurationPopup.ItemWrapper) value).perform(project, executor,
                                DataManager.getInstance().getDataContext(c));
                    } else {
                        GotoActionAction.openOptionOrPerformAction(value, pattern, project, c, event);
                        if (isToolWindowAction(value))
                            return;
                    }
                }
            });
            return;
        } else if (value instanceof Navigatable) {
            onDone = new Runnable() {
                @Override
                public void run() {
                    OpenSourceUtil.navigate(true, (Navigatable) value);
                }
            };
            return;
        }
    } finally {
        token.finish();
        final ActionCallback callback = onFocusLost();
        if (onDone != null) {
            callback.doWhenDone(onDone);
        }
    }
    focusManager.requestDefaultFocus(true);
}

From source file:com.intellij.ide.actions.SearchEverywhereAction.java

License:Apache License

public void actionPerformed(AnActionEvent e, MouseEvent me) {
    if (myBalloon != null && myBalloon.isVisible()) {
        showAll.set(!showAll.get());/* w w w  .  j  av  a  2  s. c om*/
        myNonProjectCheckBox.setSelected(showAll.get());
        //      myPopupField.getTextEditor().setBackground(showAll.get() ? new JBColor(new Color(0xffffe4), new Color(0x494539)) : UIUtil.getTextFieldBackground());
        rebuildList(myPopupField.getText());
        return;
    }
    myCurrentWorker = ActionCallback.DONE;
    if (e != null) {
        myEditor = e.getData(CommonDataKeys.EDITOR);
        myFileEditor = e.getData(PlatformDataKeys.FILE_EDITOR);
        myFile = e.getData(CommonDataKeys.PSI_FILE);
    }
    if (e == null && myFocusOwner != null) {
        e = new AnActionEvent(me, DataManager.getInstance().getDataContext(myFocusOwner), ActionPlaces.UNKNOWN,
                getTemplatePresentation(), ActionManager.getInstance(), 0);
    }
    if (e == null)
        return;
    final Project project = e.getProject();
    if (project == null)
        return;

    updateComponents();
    myContextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(e.getDataContext());
    Window wnd = myContextComponent != null ? SwingUtilities.windowForComponent(myContextComponent)
            : KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    if (wnd == null && myContextComponent instanceof Window) {
        wnd = (Window) myContextComponent;
    }
    if (wnd == null || wnd.getParent() != null)
        return;
    myActionEvent = e;
    if (myPopupField != null) {
        Disposer.dispose(myPopupField);
    }
    myPopupField = new MySearchTextField();
    myPopupField.getTextEditor().addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            myHistoryIndex = 0;
            myHistoryItem = null;
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                myList.repaint();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                myList.repaint();
            }
        }
    });
    initSearchField(myPopupField);
    myPopupField.setOpaque(false);
    final JTextField editor = myPopupField.getTextEditor();
    editor.setColumns(SEARCH_FIELD_COLUMNS);
    final JPanel panel = new JPanel(new BorderLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            final Gradient gradient = getGradientColors();
            ((Graphics2D) g).setPaint(
                    new GradientPaint(0, 0, gradient.getStartColor(), 0, getHeight(), gradient.getEndColor()));
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    };
    final JLabel title = new JLabel(" Search Everywhere:       ");
    final JPanel topPanel = new NonOpaquePanel(new BorderLayout());
    title.setForeground(new JBColor(Gray._240, Gray._200));
    if (SystemInfo.isMac) {
        title.setFont(title.getFont().deriveFont(Font.BOLD, title.getFont().getSize() - 1f));
    } else {
        title.setFont(title.getFont().deriveFont(Font.BOLD));
    }
    topPanel.add(title, BorderLayout.WEST);
    final JPanel controls = new JPanel(new BorderLayout());
    controls.setOpaque(false);
    final JLabel settings = new JLabel(AllIcons.General.SearchEverywhereGear);
    new ClickListener() {
        @Override
        public boolean onClick(@NotNull MouseEvent event, int clickCount) {
            showSettings();
            return true;
        }
    }.installOn(settings);
    controls.add(settings, BorderLayout.EAST);
    myNonProjectCheckBox.setForeground(new JBColor(Gray._240, Gray._200));
    myNonProjectCheckBox.setText("Include non-project items (" + getShortcut() + ")  ");
    controls.add(myNonProjectCheckBox, BorderLayout.WEST);
    topPanel.add(controls, BorderLayout.EAST);
    panel.add(myPopupField, BorderLayout.CENTER);
    panel.add(topPanel, BorderLayout.NORTH);
    panel.setBorder(IdeBorderFactory.createEmptyBorder(3, 5, 4, 5));
    DataManager.registerDataProvider(panel, this);
    final ComponentPopupBuilder builder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel,
            editor);
    myBalloon = builder.setCancelOnClickOutside(true).setModalContext(false).setRequestFocus(true)
            .setCancelCallback(new Computable<Boolean>() {
                @Override
                public Boolean compute() {
                    return !mySkipFocusGain;
                }
            }).createPopup();
    myBalloon.getContent().setBorder(new EmptyBorder(0, 0, 0, 0));
    final Window window = WindowManager.getInstance().suggestParentWindow(project);

    project.getMessageBus().connect(myBalloon).subscribe(DumbService.DUMB_MODE,
            new DumbService.DumbModeListener() {
                @Override
                public void enteredDumbMode() {
                }

                @Override
                public void exitDumbMode() {
                    rebuildList(myPopupField.getText());
                }
            });

    Component parent = UIUtil.findUltimateParent(window);
    registerDataProvider(panel, project);
    final RelativePoint showPoint;
    if (me != null) {
        final Component label = me.getComponent();
        final Component button = label.getParent();
        assert button != null;
        showPoint = new RelativePoint(button,
                new Point(button.getWidth() - panel.getPreferredSize().width, button.getHeight()));
    } else {
        if (parent != null) {
            int height = UISettings.getInstance().SHOW_MAIN_TOOLBAR ? 135 : 115;
            if (parent instanceof IdeFrameImpl && ((IdeFrameImpl) parent).isInFullScreen()) {
                height -= 20;
            }
            showPoint = new RelativePoint(parent,
                    new Point((parent.getSize().width - panel.getPreferredSize().width) / 2, height));
        } else {
            showPoint = JBPopupFactory.getInstance().guessBestPopupLocation(e.getDataContext());
        }
    }
    myList.setFont(UIUtil.getListFont());
    myBalloon.show(showPoint);
    initSearchActions(myBalloon, myPopupField);
    IdeFocusManager focusManager = IdeFocusManager.getInstance(project);
    focusManager.requestFocus(editor, true);
    FeatureUsageTracker.getInstance().triggerFeatureUsed(IdeActions.ACTION_SEARCH_EVERYWHERE);
}

From source file:com.intellij.ide.bookmarks.actions.BookmarksAction.java

License:Apache License

@Override
public void handleMnemonic(KeyEvent e, Project project, JBPopup popup) {
    char mnemonic = e.getKeyChar();
    final Bookmark bookmark = BookmarkManager.getInstance(project).findBookmarkForMnemonic(mnemonic);
    if (bookmark != null) {
        popup.cancel();/*from  w  w w . j  a v  a2 s. c o  m*/
        IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
            @Override
            public void run() {
                bookmark.navigate(true);
            }
        });
    }
}

From source file:com.intellij.ide.impl.ProjectUtil.java

License:Apache License

public static void focusProjectWindow(final Project p, boolean executeIfAppInactive) {
    FocusCommand cmd = new FocusCommand() {
        @NotNull//from   ww  w. ja v  a 2  s .  c om
        @Override
        public ActionCallback run() {
            JFrame f = WindowManager.getInstance().getFrame(p);
            if (f != null) {
                f.toFront();
                //f.requestFocus();
            }
            return new ActionCallback.Done();
        }
    };

    if (executeIfAppInactive) {
        AppIcon.getInstance().requestFocus((IdeFrame) WindowManager.getInstance().getFrame(p));
        cmd.run();
    } else {
        IdeFocusManager.getInstance(p).requestFocus(cmd, false);
    }
}

From source file:com.intellij.ide.impl.ProjectViewSelectInGroupTarget.java

License:Apache License

@Override
public void selectIn(final SelectInContext context, final boolean requestFocus) {
    ProjectView projectView = ProjectView.getInstance(context.getProject());
    Collection<SelectInTarget> targets = projectView.getSelectInTargets();
    Collection<SelectInTarget> targetsToCheck = new LinkedHashSet<SelectInTarget>();
    String currentId = projectView.getCurrentViewId();
    for (SelectInTarget projectViewTarget : targets) {
        if (Comparing.equal(currentId, projectViewTarget.getMinorViewId())) {
            targetsToCheck.add(projectViewTarget);
            break;
        }//from  w w w.  j av a  2 s .  c om
    }
    targetsToCheck.addAll(targets);
    for (final SelectInTarget target : targetsToCheck) {
        if (target.canSelect(context)) {
            if (requestFocus) {
                IdeFocusManager.getInstance(context.getProject()).requestFocus(new FocusCommand() {
                    @NotNull
                    @Override
                    public ActionCallback run() {
                        target.selectIn(context, requestFocus);
                        return new ActionCallback.Done();
                    }
                }, true);
            } else {
                target.selectIn(context, requestFocus);
            }
            break;
        }
    }
}

From source file:com.intellij.ide.navigationToolbar.NavBarListener.java

License:Apache License

@Override
public void keyPressed(final KeyEvent e) {
    if (!(e.isAltDown() || e.isMetaDown() || e.isControlDown() || myPanel.isNodePopupActive())) {
        if (!Character.isLetter(e.getKeyChar())) {
            return;
        }/*from  w w  w.  ja va  2 s. com*/

        final IdeFocusManager focusManager = IdeFocusManager.getInstance(myPanel.getProject());
        final ActionCallback firstCharTyped = new ActionCallback();
        focusManager.typeAheadUntil(firstCharTyped);
        myPanel.moveDown();
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final Robot robot = new Robot();
                    final boolean shiftOn = e.isShiftDown();
                    final int code = e.getKeyCode();
                    if (shiftOn) {
                        robot.keyPress(KeyEvent.VK_SHIFT);
                    }
                    robot.keyPress(code);
                    robot.keyRelease(code);

                    //don't release Shift
                    firstCharTyped.setDone();
                } catch (AWTException ignored) {
                }
            }
        });
    }
}