Example usage for com.intellij.openapi.ui.popup JBPopupListener.Adapter JBPopupListener.Adapter

List of usage examples for com.intellij.openapi.ui.popup JBPopupListener.Adapter JBPopupListener.Adapter

Introduction

In this page you can find the example usage for com.intellij.openapi.ui.popup JBPopupListener.Adapter JBPopupListener.Adapter.

Prototype

JBPopupListener.Adapter

Source Link

Usage

From source file:com.intellij.ide.util.gotoByName.ChooseByNameBase.java

License:Apache License

/**
 * @param modalityState          - if not null rebuilds list in given {@link ModalityState}
 *//*from   w  w w  .  jav a2  s  .  c o  m*/
protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState,
        final boolean allowMultipleSelection) {
    myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject);

    myActionListener = callback;
    myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS));

    final JPanel hBox = new JPanel();
    hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS));

    JPanel caption2Tools = new JPanel(new BorderLayout());

    if (myModel.getPromptText() != null) {
        JLabel label = new JLabel(myModel.getPromptText());
        if (UIUtil.isUnderAquaLookAndFeel()) {
            label.setBorder(new CompoundBorder(new EmptyBorder(0, 9, 0, 0), label.getBorder()));
        }
        label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));
        caption2Tools.add(label, BorderLayout.WEST);
    }

    caption2Tools.add(hBox, BorderLayout.EAST);

    myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4)); // space between checkbox and filter/show all in view buttons

    final String checkBoxName = myModel.getCheckBoxName();
    myCheckBox = new JCheckBox(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null
            ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")"
            : "") : "");
    myCheckBox.setAlignmentX(SwingConstants.RIGHT);

    if (!SystemInfo.isMac) {
        myCheckBox.setBorder(null);
    }

    myCheckBox.setSelected(myModel.loadInitialCheckBoxState());

    if (checkBoxName == null) {
        myCheckBox.setVisible(false);
    }

    addCard(myCheckBox, CHECK_BOX_CARD);

    addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD);
    addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD);
    JPanel searching = new JPanel(new BorderLayout(5, 0));
    searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST);
    searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER);
    addCard(searching, SEARCHING_CARD);
    myCard.show(myCardContainer, CHECK_BOX_CARD);

    if (isCheckboxVisible()) {
        hBox.add(myCardContainer);
    }

    final DefaultActionGroup group = new DefaultActionGroup();
    group.add(new ShowFindUsagesAction() {
        @Override
        public PsiElement[][] getElements() {
            final Object[] objects = myListModel.toArray();
            final List<PsiElement> prefixMatchElements = new ArrayList<PsiElement>(objects.length);
            final List<PsiElement> nonPrefixMatchElements = new ArrayList<PsiElement>(objects.length);
            List<PsiElement> curElements = prefixMatchElements;
            for (Object object : objects) {
                if (object instanceof PsiElement) {
                    curElements.add((PsiElement) object);
                } else if (object instanceof DataProvider) {
                    final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object);
                    if (psi != null) {
                        curElements.add(psi);
                    }
                } else if (object == NON_PREFIX_SEPARATOR) {
                    curElements = nonPrefixMatchElements;
                }
            }
            return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements),
                    PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) };
        }
    });
    final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN,
            group, true);
    actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
    final JComponent toolbarComponent = actionToolbar.getComponent();
    toolbarComponent.setBorder(null);

    if (myToolArea == null) {
        myToolArea = new JLabel(EmptyIcon.create(1, 24));
    }
    hBox.add(myToolArea);
    hBox.add(toolbarComponent);

    myTextFieldPanel.add(caption2Tools);

    final ActionMap actionMap = new ActionMap();
    actionMap.setParent(myTextField.getActionMap());
    actionMap.put(DefaultEditorKit.copyAction, new AbstractAction() {
        @Override
        public void actionPerformed(@NotNull ActionEvent e) {
            if (myTextField.getSelectedText() != null) {
                actionMap.getParent().get(DefaultEditorKit.copyAction).actionPerformed(e);
                return;
            }
            final Object chosenElement = getChosenElement();
            if (chosenElement instanceof PsiElement) {
                CopyReferenceAction.doCopy((PsiElement) chosenElement, myProject);
            }
        }
    });
    myTextField.setActionMap(actionMap);

    myTextFieldPanel.add(myTextField);
    Font editorFont = getEditorFont();
    myTextField.setFont(editorFont);

    if (checkBoxName != null) {
        if (myCheckBox != null && myCheckBoxShortcut != null) {
            new AnAction("change goto check box", null, null) {
                @RequiredDispatchThread
                @Override
                public void actionPerformed(@NotNull AnActionEvent e) {
                    myCheckBox.setSelected(!myCheckBox.isSelected());
                }
            }.registerCustomShortcutSet(myCheckBoxShortcut, myTextField);
        }
    }

    if (isCloseByFocusLost()) {
        myTextField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(@NotNull final FocusEvent e) {
                cancelListUpdater(); // cancel thread as early as possible
                myHideAlarm.addRequest(new Runnable() {
                    @Override
                    public void run() {
                        JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent());
                        if (popup != null) {
                            popup.addListener(new JBPopupListener.Adapter() {
                                @Override
                                public void onClosed(@NotNull LightweightWindowEvent event) {
                                    if (event.isOk()) {
                                        hideHint();
                                    }
                                }
                            });
                        } else {
                            Component oppositeComponent = e.getOppositeComponent();
                            if (oppositeComponent == myCheckBox) {
                                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                                return;
                            }
                            if (oppositeComponent != null && !(oppositeComponent instanceof JFrame)
                                    && myList.isShowing() && (oppositeComponent == myList
                                            || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) {
                                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);// Otherwise me may skip some KeyEvents
                                return;
                            }

                            if (oppositeComponent != null) {
                                ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
                                ToolWindow toolWindow = toolWindowManager
                                        .getToolWindow(toolWindowManager.getActiveToolWindowId());
                                if (toolWindow != null) {
                                    JComponent toolWindowComponent = toolWindow.getComponent();
                                    if (SwingUtilities.isDescendingFrom(oppositeComponent,
                                            toolWindowComponent)) {
                                        return; // Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow)
                                    }
                                }
                            }

                            EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
                            if (queue instanceof IdeEventQueue) {
                                if (!((IdeEventQueue) queue).wasRootRecentlyClicked(oppositeComponent)) {
                                    Component root = SwingUtilities.getRoot(myTextField);
                                    if (root != null && root.isShowing()) {
                                        IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                                        return;
                                    }
                                }
                            }

                            hideHint();
                        }
                    }
                }, 5);
            }
        });
    }

    if (myCheckBox != null) {
        myCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(@NotNull ItemEvent e) {
                rebuildList(false);
            }
        });
        myCheckBox.setFocusable(false);
    }

    myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
        @Override
        protected void textChanged(DocumentEvent e) {
            clearPostponedOkAction(false);
            rebuildList(false);
        }
    });

    final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
    final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN);
    myTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(@NotNull KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
                myClosedByShiftEnter = true;
                close(true);
            }
            if (!myListScrollPane.isVisible()) {
                return;
            }
            final int keyCode;

            // Add support for user-defined 'caret up/down' shortcuts.
            KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
            if (upShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_UP;
            } else if (downShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_DOWN;
            } else {
                keyCode = e.getKeyCode();
            }
            switch (keyCode) {
            case KeyEvent.VK_DOWN:
                ListScrollingUtil.moveDown(myList, e.getModifiersEx());
                break;
            case KeyEvent.VK_UP:
                ListScrollingUtil.moveUp(myList, e.getModifiersEx());
                break;
            case KeyEvent.VK_PAGE_UP:
                ListScrollingUtil.movePageUp(myList);
                break;
            case KeyEvent.VK_PAGE_DOWN:
                ListScrollingUtil.movePageDown(myList);
                break;
            case KeyEvent.VK_TAB:
                close(true);
                break;
            case KeyEvent.VK_ENTER:
                if (myList.getSelectedValue() == EXTRA_ELEM) {
                    myMaximumListSizeLimit += myListSizeIncreasing;
                    rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null);
                    e.consume();
                }
                break;
            }

            if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) {
                if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) {
                    ListScrollingUtil.moveUp(myList, e.getModifiersEx());
                } else {
                    ListScrollingUtil.moveDown(myList, e.getModifiersEx());
                }
            }
        }
    });

    myTextField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(@NotNull ActionEvent actionEvent) {
            doClose(true);
        }
    });

    myList.setFocusable(false);
    myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
            : ListSelectionModel.SINGLE_SELECTION);
    new ClickListener() {
        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (!myTextField.hasFocus()) {
                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
            }

            if (clickCount == 2) {
                int selectedIndex = myList.getSelectedIndex();
                Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex);

                if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) { // Otherwise it was reselected in the selection listener
                    if (myList.getSelectedValue() == EXTRA_ELEM) {
                        myMaximumListSizeLimit += myListSizeIncreasing;
                        rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null);
                    } else {
                        doClose(true);
                    }
                }
                return true;
            }

            return false;
        }
    }.installOn(myList);

    myList.setCellRenderer(myModel.getListCellRenderer());
    myList.setFont(editorFont);

    myList.addListSelectionListener(new ListSelectionListener() {
        private int myPreviousSelectionIndex = 0;

        @Override
        public void valueChanged(@NotNull ListSelectionEvent e) {
            if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) {
                myPreviousSelectionIndex = myList.getSelectedIndex();
                chosenElementMightChange();
                updateDocumentation();
            } else if (allowMultipleSelection) {
                myList.setSelectedIndex(myPreviousSelectionIndex);
            }
        }
    });

    myListScrollPane = ScrollPaneFactory.createScrollPane(myList);
    myListScrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));

    myTextFieldPanel.setBorder(new EmptyBorder(2, 2, 2, 2));

    showTextFieldPanel();

    myInitialized = true;

    if (modalityState != null) {
        rebuildList(myInitialIndex, 0, modalityState, null);
    }
}

From source file:com.intellij.xdebugger.impl.ui.DebuggerUIUtil.java

License:Apache License

public static void showXBreakpointEditorBalloon(final Project project, @Nullable final Point point,
        final JComponent component, final boolean showAllOptions, final XBreakpoint breakpoint) {
    final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
    final XLightBreakpointPropertiesPanel<XBreakpointBase<?, ?, ?>> propertiesPanel = new XLightBreakpointPropertiesPanel<XBreakpointBase<?, ?, ?>>(
            project, breakpointManager, (XBreakpointBase) breakpoint, showAllOptions);

    final Ref<Balloon> balloonRef = Ref.create(null);
    final Ref<Boolean> isLoading = Ref.create(Boolean.FALSE);
    final Ref<Boolean> moreOptionsRequested = Ref.create(Boolean.FALSE);

    propertiesPanel.setDelegate(new XLightBreakpointPropertiesPanel.Delegate() {
        @Override//from w w w  . j  av a  2  s  .c  o  m
        public void showMoreOptions() {
            if (!isLoading.get()) {
                propertiesPanel.saveProperties();
            }
            if (!balloonRef.isNull()) {
                balloonRef.get().hide();
            }
            showXBreakpointEditorBalloon(project, point, component, true, breakpoint);
            moreOptionsRequested.set(true);
        }
    });

    isLoading.set(Boolean.TRUE);
    propertiesPanel.loadProperties();
    isLoading.set(Boolean.FALSE);

    if (moreOptionsRequested.get()) {
        return;
    }

    Runnable showMoreOptions = new Runnable() {
        @Override
        public void run() {
            propertiesPanel.saveProperties();
            propertiesPanel.dispose();
            BreakpointsDialogFactory.getInstance(project).showDialog(breakpoint);
        }
    };

    final JComponent mainPanel = propertiesPanel.getMainPanel();
    final Balloon balloon = showBreakpointEditor(project, mainPanel, point, component, showMoreOptions,
            breakpoint);
    balloonRef.set(balloon);

    final XBreakpointListener<XBreakpoint<?>> breakpointListener = new XBreakpointAdapter<XBreakpoint<?>>() {
        @Override
        public void breakpointRemoved(@NotNull XBreakpoint<?> removedBreakpoint) {
            if (removedBreakpoint.equals(breakpoint)) {
                balloon.hide();
            }
        }
    };

    balloon.addListener(new JBPopupListener.Adapter() {
        @Override
        public void onClosed(LightweightWindowEvent event) {
            propertiesPanel.saveProperties();
            propertiesPanel.dispose();
            breakpointManager.removeBreakpointListener(breakpointListener);
        }
    });

    breakpointManager.addBreakpointListener(breakpointListener);
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
            IdeFocusManager.findInstance().requestFocus(mainPanel, true);
        }
    });
}