Search in sources :

Example 11 with JBPopupAdapter

use of com.intellij.openapi.ui.popup.JBPopupAdapter in project intellij-community by JetBrains.

the class OccurrencesChooser method showChooser.

public <C extends BaseReplaceChoice> void showChooser(final Pass<C> callback, final Map<C, List<T>> occurrencesMap, String title) {
    if (occurrencesMap.size() == 1) {
        callback.pass(occurrencesMap.keySet().iterator().next());
        return;
    }
    final DefaultListModel<C> model = new DefaultListModel<>();
    for (C choice : occurrencesMap.keySet()) {
        model.addElement(choice);
    }
    final JList<C> list = new JBList<>(model);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
            final Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            @SuppressWarnings("unchecked") final C choices = (C) value;
            if (choices != null) {
                setText(choices.formatDescription(occurrencesMap.get(choices).size()));
            }
            return rendererComponent;
        }
    });
    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            final C value = list.getSelectedValue();
            if (value == null)
                return;
            dropHighlighters();
            final MarkupModel markupModel = myEditor.getMarkupModel();
            final List<T> occurrenceList = occurrencesMap.get(value);
            for (T occurrence : occurrenceList) {
                final TextRange textRange = getOccurrenceRange(occurrence);
                final RangeHighlighter rangeHighlighter = markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.SELECTION - 1, myAttributes, HighlighterTargetArea.EXACT_RANGE);
                myRangeHighlighters.add(rangeHighlighter);
            }
        }
    });
    JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(title).setMovable(true).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> callback.pass(list.getSelectedValue())).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters();
        }
    }).createPopup().showInBestPositionFor(myEditor);
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) TextRange(com.intellij.openapi.util.TextRange) ListSelectionListener(javax.swing.event.ListSelectionListener) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List)

Example 12 with JBPopupAdapter

use of com.intellij.openapi.ui.popup.JBPopupAdapter in project intellij-community by JetBrains.

the class LiveTemplateSettingsEditor method createShortContextPanel.

private JPanel createShortContextPanel(final boolean allowNoContexts) {
    JPanel panel = new JPanel(new BorderLayout());
    final JLabel ctxLabel = new JLabel();
    final JLabel change = new JLabel();
    change.setForeground(PlatformColors.BLUE);
    change.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    panel.add(ctxLabel, BorderLayout.CENTER);
    panel.add(change, BorderLayout.EAST);
    final Runnable updateLabel = () -> {
        myExpandByCombo.setEnabled(isExpandableFromEditor());
        updateHighlighter();
        StringBuilder sb = new StringBuilder();
        String oldPrefix = "";
        for (TemplateContextType type : getApplicableContexts()) {
            final TemplateContextType base = type.getBaseContextType();
            String ownName = UIUtil.removeMnemonic(type.getPresentableName());
            String prefix = "";
            if (base != null && !(base instanceof EverywhereContextType)) {
                prefix = UIUtil.removeMnemonic(base.getPresentableName()) + ": ";
                ownName = StringUtil.decapitalize(ownName);
            }
            if (type instanceof EverywhereContextType) {
                ownName = "Other";
            }
            if (sb.length() > 0) {
                sb.append(oldPrefix.equals(prefix) ? ", " : "; ");
            }
            if (!oldPrefix.equals(prefix)) {
                sb.append(prefix);
                oldPrefix = prefix;
            }
            sb.append(ownName);
        }
        String contexts = "Applicable in " + sb.toString();
        change.setText("Change");
        final boolean noContexts = sb.length() == 0;
        if (noContexts) {
            if (!allowNoContexts) {
                ctxLabel.setForeground(JBColor.RED);
            }
            contexts = "No applicable contexts" + (allowNoContexts ? "" : " yet");
            ctxLabel.setIcon(AllIcons.General.BalloonWarning);
            change.setText("Define");
        } else {
            ctxLabel.setForeground(UIUtil.getLabelForeground());
            ctxLabel.setIcon(null);
        }
        ctxLabel.setText(StringUtil.first(contexts + ". ", 100, true));
        myTemplateOptionsPanel.removeAll();
        myTemplateOptionsPanel.add(createTemplateOptionsPanel());
    };
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (disposeContextPopup())
                return false;
            final JPanel content = createPopupContextPanel(updateLabel, myContext);
            Dimension prefSize = content.getPreferredSize();
            if (myLastSize != null && (myLastSize.width > prefSize.width || myLastSize.height > prefSize.height)) {
                content.setPreferredSize(new Dimension(Math.max(prefSize.width, myLastSize.width), Math.max(prefSize.height, myLastSize.height)));
            }
            myContextPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(content, null).setResizable(true).createPopup();
            myContextPopup.show(new RelativePoint(change, new Point(change.getWidth(), -content.getPreferredSize().height - 10)));
            myContextPopup.addListener(new JBPopupAdapter() {

                @Override
                public void onClosed(LightweightWindowEvent event) {
                    myLastSize = content.getSize();
                }
            });
            return true;
        }
    }.installOn(change);
    updateLabel.run();
    return panel;
}
Also used : EverywhereContextType(com.intellij.codeInsight.template.EverywhereContextType) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType)

Aggregations

JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)12 LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)12 JBList (com.intellij.ui.components.JBList)5 ActionEvent (java.awt.event.ActionEvent)4 ListSelectionEvent (javax.swing.event.ListSelectionEvent)4 ListSelectionListener (javax.swing.event.ListSelectionListener)4 Pair (com.intellij.openapi.util.Pair)3 TextRange (com.intellij.openapi.util.TextRange)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 ActionListener (java.awt.event.ActionListener)3 Balloon (com.intellij.openapi.ui.popup.Balloon)2 JBPopup (com.intellij.openapi.ui.popup.JBPopup)2 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EverywhereContextType (com.intellij.codeInsight.template.EverywhereContextType)1 TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)1 ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1 Notification (com.intellij.notification.Notification)1 NotificationListener (com.intellij.notification.NotificationListener)1