Search in sources :

Example 1 with IntroduceTarget

use of com.intellij.refactoring.introduce.IntroduceTarget in project intellij-community by JetBrains.

the class IntroduceTargetChooser method showIntroduceTargetChooser.

public static <T extends IntroduceTarget> void showIntroduceTargetChooser(@NotNull Editor editor, @NotNull List<T> expressions, @NotNull Pass<T> callback, @NotNull @Nls String title, int selection) {
    AtomicReference<ScopeHighlighter> highlighter = new AtomicReference<>(new ScopeHighlighter(editor));
    CollectionListModel<T> model = new CollectionListModel<>(expressions);
    JBList<T> list = new JBList<>(model);
    // Set the accessible name so that screen readers announce the list tile (e.g. "Expression Types list").
    AccessibleContextUtil.setName(list, title);
    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (selection > -1)
        list.setSelectedIndex(selection);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            IntroduceTarget expr = (T) value;
            if (expr.isValid()) {
                String text = expr.render();
                int firstNewLinePos = text.indexOf('\n');
                String trimmedText = text.substring(0, firstNewLinePos != -1 ? firstNewLinePos : Math.min(100, text.length()));
                if (trimmedText.length() != text.length())
                    trimmedText += " ...";
                setText(trimmedText);
            } else {
                setForeground(JBColor.RED);
                setText("Invalid");
            }
            return rendererComponent;
        }
    });
    list.addListSelectionListener(e -> {
        ScopeHighlighter h = highlighter.get();
        if (h == null)
            return;
        h.dropHighlight();
        T expr = list.getSelectedValue();
        if (expr != null && expr.isValid()) {
            TextRange range = expr.getTextRange();
            h.highlight(Pair.create(range, Collections.singletonList(range)));
        }
    });
    JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(title).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
        T expr = list.getSelectedValue();
        if (expr != null && expr.isValid()) {
            callback.pass(expr);
        }
    }).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            highlighter.getAndSet(null).dropHighlight();
        }
    }).createPopup().showInBestPositionFor(editor);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TextRange(com.intellij.openapi.util.TextRange) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) PsiIntroduceTarget(com.intellij.refactoring.introduce.PsiIntroduceTarget) IntroduceTarget(com.intellij.refactoring.introduce.IntroduceTarget) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) ScopeHighlighter(com.intellij.codeInsight.unwrap.ScopeHighlighter) JBList(com.intellij.ui.components.JBList) CollectionListModel(com.intellij.ui.CollectionListModel)

Aggregations

ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1 JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)1 LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)1 TextRange (com.intellij.openapi.util.TextRange)1 IntroduceTarget (com.intellij.refactoring.introduce.IntroduceTarget)1 PsiIntroduceTarget (com.intellij.refactoring.introduce.PsiIntroduceTarget)1 CollectionListModel (com.intellij.ui.CollectionListModel)1 JBList (com.intellij.ui.components.JBList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1