Search in sources :

Example 1 with JBPopupAdapter

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

the class PyStudyShowTutorial method projectOpened.

@Override
public void projectOpened() {
    ApplicationManager.getApplication().invokeLater(new DumbAwareRunnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new DumbAwareRunnable() {

                @Override
                public void run() {
                    if (PropertiesComponent.getInstance().getBoolean(ourShowPopup, true)) {
                        final String content = "<html>If you'd like to learn more about PyCharm Edu, " + "click <a href=\"https://www.jetbrains.com/pycharm-edu/quickstart/\">here</a> to watch a tutorial</html>";
                        final Notification notification = new Notification("Watch Tutorials!", "", content, NotificationType.INFORMATION, new NotificationListener.UrlOpeningListener(true));
                        Notifications.Bus.notify(notification);
                        Balloon balloon = notification.getBalloon();
                        if (balloon != null) {
                            balloon.addListener(new JBPopupAdapter() {

                                @Override
                                public void onClosed(LightweightWindowEvent event) {
                                    notification.expire();
                                }
                            });
                        }
                        notification.whenExpired(() -> PropertiesComponent.getInstance().setValue(ourShowPopup, false, true));
                    }
                }
            });
        }
    });
}
Also used : Balloon(com.intellij.openapi.ui.popup.Balloon) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable) Notification(com.intellij.notification.Notification) NotificationListener(com.intellij.notification.NotificationListener) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent)

Example 2 with JBPopupAdapter

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

the class MethodOrClosureScopeChooser method create.

/**
   * @param callback is invoked if any scope was chosen. The first arg is this scope and the second arg is a psielement to search for (super method of chosen method or
   *                 variable if the scope is a closure)
   */
public static JBPopup create(List<? extends GrParametersOwner> scopes, final Editor editor, final JBPopupOwner popupRef, final PairFunction<GrParametersOwner, PsiElement, Object> callback) {
    final JPanel panel = new JPanel(new BorderLayout());
    final JCheckBox superMethod = new JCheckBox(USE_SUPER_METHOD_OF, true);
    superMethod.setMnemonic('U');
    panel.add(superMethod, BorderLayout.SOUTH);
    final JBList list = new JBList(scopes.toArray());
    list.setVisibleRowCount(5);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final String text;
            if (value instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) value;
                text = PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
                final int flags = Iconable.ICON_FLAG_VISIBILITY;
                final Icon icon = method.getIcon(flags);
                if (icon != null)
                    setIcon(icon);
            } else {
                LOG.assertTrue(value instanceof GrClosableBlock);
                setIcon(JetgroovyIcons.Groovy.Groovy_16x16);
                text = "{...}";
            }
            setText(text);
            return this;
        }
    });
    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    final TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            final GrParametersOwner selectedMethod = (GrParametersOwner) list.getSelectedValue();
            if (selectedMethod == null)
                return;
            dropHighlighters(highlighters);
            updateView(selectedMethod, editor, attributes, highlighters, superMethod);
        }
    });
    updateView(scopes.get(0), editor, attributes, highlighters, superMethod);
    final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(list);
    scrollPane.setBorder(null);
    panel.add(scrollPane, BorderLayout.CENTER);
    final List<Pair<ActionListener, KeyStroke>> keyboardActions = Collections.singletonList(Pair.<ActionListener, KeyStroke>create(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final GrParametersOwner ToSearchIn = (GrParametersOwner) list.getSelectedValue();
            final JBPopup popup = popupRef.get();
            if (popup != null && popup.isVisible()) {
                popup.cancel();
            }
            final PsiElement toSearchFor;
            if (ToSearchIn instanceof GrMethod) {
                final GrMethod method = (GrMethod) ToSearchIn;
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? method.findDeepestSuperMethod() : method;
            } else {
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? ToSearchIn.getParent() : null;
            }
            IdeFocusManager.findInstance().doWhenFocusSettlesDown(() -> callback.fun(ToSearchIn, toSearchFor), ModalityState.current());
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)));
    return JBPopupFactory.getInstance().createComponentPopupBuilder(panel, list).setTitle("Introduce parameter to").setMovable(false).setResizable(false).setRequestFocus(true).setKeyboardActions(keyboardActions).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters(highlighters);
        }
    }).createPopup();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ListSelectionListener(javax.swing.event.ListSelectionListener) ActionListener(java.awt.event.ActionListener) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBList(com.intellij.ui.components.JBList)

Example 3 with JBPopupAdapter

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

the class RenameChooser method showChooser.

public void showChooser(final Collection<PsiReference> refs, final Collection<Pair<PsiElement, TextRange>> stringUsages) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        runRenameTemplate(RefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS_FOR_FILE ? stringUsages : new ArrayList<>());
        return;
    }
    final DefaultListModel model = new DefaultListModel();
    model.addElement(CODE_OCCURRENCES);
    model.addElement(ALL_OCCURRENCES);
    final JList list = new JBList(model);
    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            final String selectedValue = (String) list.getSelectedValue();
            if (selectedValue == null)
                return;
            dropHighlighters();
            final MarkupModel markupModel = myEditor.getMarkupModel();
            if (selectedValue.equals(ALL_OCCURRENCES)) {
                for (Pair<PsiElement, TextRange> pair : stringUsages) {
                    final TextRange textRange = pair.second.shiftRight(pair.first.getTextOffset());
                    final RangeHighlighter rangeHighlighter = markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.SELECTION - 1, myAttributes, HighlighterTargetArea.EXACT_RANGE);
                    myRangeHighlighters.add(rangeHighlighter);
                }
            }
            for (PsiReference reference : refs) {
                final PsiElement element = reference.getElement();
                if (element == null)
                    continue;
                final TextRange textRange = element.getTextRange();
                final RangeHighlighter rangeHighlighter = markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.SELECTION - 1, myAttributes, HighlighterTargetArea.EXACT_RANGE);
                myRangeHighlighters.add(rangeHighlighter);
            }
        }
    });
    JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("String occurrences found").setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> runRenameTemplate(ALL_OCCURRENCES.equals(list.getSelectedValue()) ? stringUsages : new ArrayList<>())).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters();
        }
    }).createPopup().showInBestPositionFor(myEditor);
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) PsiReference(com.intellij.psi.PsiReference) 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) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 4 with JBPopupAdapter

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

the class Notification method setBalloon.

public void setBalloon(@NotNull final Balloon balloon) {
    hideBalloon();
    myBalloonRef = new WeakReference<>(balloon);
    balloon.addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            if (SoftReference.dereference(myBalloonRef) == balloon) {
                myBalloonRef = null;
            }
        }
    });
}
Also used : JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent)

Example 5 with JBPopupAdapter

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

the class BalloonPopupBuilderImpl method createBalloon.

@NotNull
@Override
public Balloon createBalloon() {
    final BalloonImpl result = new BalloonImpl(myContent, myBorder, myBorderInsets, myFill, myHideOnMouseOutside, myHideOnKeyOutside, myHideOnAction, myHideOnCloseClick, myShowCallout, myCloseButtonEnabled, myFadeoutTime, myHideOnFrameResize, myHideOnLinkClick, myClickHandler, myCloseOnClick, myAnimationCycle, myCalloutShift, myPositionChangeXShift, myPositionChangeYShift, myDialogMode, myTitle, myContentInsets, myShadow, mySmallVariant, myBlockClicks, myLayer, myRequestFocus, myPointerSize, myCornerToPointerDistance);
    if (myStorage != null && myAnchor != null) {
        List<Balloon> balloons = myStorage.get(myAnchor);
        if (balloons == null) {
            myStorage.put(myAnchor, balloons = new ArrayList<>());
            Disposer.register(myAnchor, new Disposable() {

                @Override
                public void dispose() {
                    List<Balloon> toDispose = myStorage.remove(myAnchor);
                    if (toDispose != null) {
                        for (Balloon balloon : toDispose) {
                            if (!balloon.isDisposed()) {
                                Disposer.dispose(balloon);
                            }
                        }
                    }
                }
            });
        }
        balloons.add(result);
        result.addListener(new JBPopupAdapter() {

            @Override
            public void onClosed(LightweightWindowEvent event) {
                if (!result.isDisposed()) {
                    Disposer.dispose(result);
                }
            }
        });
    }
    return result;
}
Also used : Disposable(com.intellij.openapi.Disposable) ArrayList(java.util.ArrayList) BalloonImpl(com.intellij.ui.BalloonImpl) Balloon(com.intellij.openapi.ui.popup.Balloon) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) ArrayList(java.util.ArrayList) List(java.util.List) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) NotNull(org.jetbrains.annotations.NotNull)

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