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:ru.list.search.AssistantSearchEverywhereAction.java

License:Apache License

private void doNavigate(final int index, final int windowIndex) {
    final DataManager dataManager = DataManager.getInstance();
    if (dataManager == null)
        return;/* w  w w .  j av  a  2 s  .com*/
    final Project project = CommonDataKeys.PROJECT
            .getData(dataManager.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;
        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() {
                public void run() {
                    PsiElement psiElement = (PsiElement) value;
                    FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
                    AssistantUtils.openPsiElement(windowIndex, psiElement, fileEditorManager, myEditorWindow);
                }
            };
            return;
        } else if (isVirtualFile(value)) {
            onDone = new Runnable() {
                public void run() {
                    VirtualFile virtualFile = (VirtualFile) value;
                    FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
                    AssistantUtils.openFileInEditorGroup(windowIndex, virtualFile, fileEditorManager,
                            myEditorWindow);
                }
            };
            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.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() {
                    Navigatable navigatable = (Navigatable) value;
                    FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
                    AssistantUtils.openNavigatable(windowIndex, navigatable, fileEditorManager, myEditorWindow);
                }
            };
            return;
        }
    } finally {
        token.finish();
        final ActionCallback callback = onFocusLost();
        if (onDone != null) {
            callback.doWhenDone(onDone);
        }
    }
    focusManager.requestDefaultFocus(true);
}

From source file:ru.list.search.AssistantSearchEverywhereAction.java

License:Apache License

public void actionPerformed(AnActionEvent e, MouseEvent me) {
    if (myBalloon != null && myBalloon.isVisible()) {
        showAll.set(!showAll.get());//from  w ww.  j a v a 2 s . com
        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);
        myEditorWindow = e.getData(EditorWindow.DATA_KEY);
    }
    if (e == null && myFocusOwner != null) {
        e = AnActionEvent.createFromAnAction(this, me, ActionPlaces.UNKNOWN,
                DataManager.getInstance().getDataContext(myFocusOwner));
    }
    if (e == null)
        return;
    final Project project = e.getProject();
    if (project == null)
        return;

    //noinspection SSBasedInspection
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            LookupManager.getInstance(project).hideActiveLookup();
        }
    });

    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(" Assistant 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() + ")  ");
    if (!NonProjectScopeDisablerEP.isSearchInNonProjectDisabled()) {
        controls.add(myNonProjectCheckBox, BorderLayout.WEST);
    }
    topPanel.add(controls, BorderLayout.EAST);
    panel.add(myPopupField, BorderLayout.CENTER);
    panel.add(topPanel, BorderLayout.NORTH);
    panel.add(new JLabel("Open in main group - (Enter);\t Open in second group - (Alt Enter)"),
            BorderLayout.SOUTH);
    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(JBUI.Borders.empty());
    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() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            rebuildList(myPopupField.getText());
                        }
                    });
                }
            });

    Component parent = UIUtil.findUltimateParent(window);
    registerDataProvider(panel, project);
    final RelativePoint showPoint;
    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);
}