Example usage for com.intellij.openapi.ui.popup PopupChooserBuilder addListener

List of usage examples for com.intellij.openapi.ui.popup PopupChooserBuilder addListener

Introduction

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

Prototype

@Override
    public PopupChooserBuilder<T> addListener(final JBPopupListener listener) 

Source Link

Usage

From source file:com.intellij.tasks.actions.SearchSupport.java

License:Apache License

private void showPopup(boolean explicit) {

    myCancelled = false;/* w  w w .jav a 2 s  .  c  o m*/

    List<T> list = getItems(myTextField.getText());
    myListModel.clear();
    myListModel.addAll(list);

    if (list.isEmpty()) {
        if (explicit) {
            showNoSuggestions();
        } else {
            hideCurrentPopup();
        }
        return;
    }

    ensureSelectionExists();

    myList.setPrototypeCellValue(null);
    if (isPopupShowing()) {
        adjustPopupSize();
        return;
    }

    hideCurrentPopup();

    final PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(myList);
    builder.addListener(new JBPopupListener() {
        public void beforeShown(LightweightWindowEvent event) {
            myTextField.registerKeyboardAction(myCancelAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                    JComponent.WHEN_IN_FOCUSED_WINDOW);
        }

        public void onClosed(LightweightWindowEvent event) {
            myTextField.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
        }
    });
    myCurrentPopup = builder.setRequestFocus(false).setAutoSelectIfEmpty(false).setResizable(false)
            .setCancelCallback(new Computable<Boolean>() {
                public Boolean compute() {
                    final int caret = myTextField.getCaretModel().getOffset();
                    getEditor().getSelectionModel().setSelection(caret, caret);
                    myTextField.setFocusTraversalKeysEnabled(true);
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        public void run() {
                            myTextField.requestFocus();
                        }
                    });
                    return Boolean.TRUE;
                }
            }).setItemChoosenCallback(new Runnable() {
                public void run() {
                    processChosenFromCompletion();
                }
            }).setCancelKeyEnabled(false).setAlpha(0.1f).setFocusOwners(new Component[] { myTextField })
            .createPopup();

    adjustPopupSize();
    showPopup();
}

From source file:org.jboss.forge.plugin.idea.actions.ShowForgeMenuAction.java

License:Open Source License

@Override
public void actionPerformed(final AnActionEvent e) {
    if (active)/*from   ww w  .j  a  v  a  2s.c om*/
        return;
    active = true;
    final VirtualFile[] selectedFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);

    final JBList list = new JBList();
    DefaultListModel model = new DefaultListModel();

    final Project project = e.getData(DataKeys.PROJECT);

    final List<UICommand> allCandidates = getAllCandidates();
    model.setSize(allCandidates.size());

    list.setCellRenderer(new ListCellRendererWrapper<UICommand>() {
        @Override
        public void customize(JList list, UICommand data, int index, boolean selected, boolean hasFocus) {
            if (data != null) {
                setIcon(AllIcons.Nodes.Plugin);
                setText(data.getMetadata().getName());

                // if (hasFocus)
                // {
                // HintManager.getInstance().showInformationHint(editor,
                // data.getMetadata().getDescription());
                // }
            }
        }
    });

    for (int i = 0; i < allCandidates.size(); i++) {
        model.set(i, allCandidates.get(i));
    }

    list.setModel(model);

    final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
    listPopupBuilder.setTitle("Run a Forge command");
    listPopupBuilder.addListener(new JBPopupAdapter() {
        @Override
        public void onClosed(LightweightWindowEvent event) {
            ShowForgeMenuAction.this.active = false;
        }
    });
    listPopupBuilder.setItemChoosenCallback(new Runnable() {
        @Override
        public void run() {
            int selectedIndex = list.getSelectedIndex();
            UICommand selectedCommand = allCandidates.get(selectedIndex);
            openWizard(selectedCommand, selectedFiles);
        }
    }).createPopup().showCenteredInCurrentWindow(project);
}

From source file:org.jboss.forge.plugin.idea.ui.CommandListPopupBuilder.java

License:Open Source License

private JBPopup buildPopup(final JBList list, final Map<Object, String> filterIndex) {
    final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
    listPopupBuilder.setTitle("Run a Forge command");
    listPopupBuilder.setResizable(true);
    listPopupBuilder.addListener(new JBPopupAdapter() {
        @Override//from w  ww. j  ava2  s  . co  m
        public void onClosed(LightweightWindowEvent event) {
            CommandListPopupBuilder.this.active = false;
        }
    });
    listPopupBuilder.setItemChoosenCallback(new Runnable() {
        @Override
        public void run() {
            Object selectedObject = list.getSelectedValue();
            if (selectedObject instanceof UICommand) {
                UICommand selectedCommand = (UICommand) selectedObject;

                // Make sure that this cached command is still enabled
                if (selectedCommand.isEnabled(uiContext)) {
                    openWizard(selectedCommand);
                }
            }
        }
    });
    listPopupBuilder.setFilteringEnabled(new Function<Object, String>() {
        @Override
        public String fun(Object object) {
            return filterIndex.get(object);
        }
    });

    return listPopupBuilder.createPopup();
}