Search in sources :

Example 6 with LightweightWindowEvent

use of com.intellij.openapi.ui.popup.LightweightWindowEvent 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)

Example 7 with LightweightWindowEvent

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

the class IntroduceParameterHandler method chooseMethodToIntroduceParameter.

private void chooseMethodToIntroduceParameter(final Editor editor, final List<PsiMethod> validEnclosingMethods, final PairConsumer<PsiMethod, PsiMethod> consumer) {
    final boolean unitTestMode = ApplicationManager.getApplication().isUnitTestMode();
    if (validEnclosingMethods.size() == 1 || unitTestMode) {
        final PsiMethod methodToIntroduceParameterTo = validEnclosingMethods.get(0);
        if (methodToIntroduceParameterTo.findDeepestSuperMethod() == null || unitTestMode) {
            consumer.consume(methodToIntroduceParameterTo, methodToIntroduceParameterTo);
            return;
        }
    }
    final JPanel panel = new JPanel(new BorderLayout());
    final JCheckBox superMethod = new JCheckBox("Refactor super method", true);
    superMethod.setMnemonic('U');
    panel.add(superMethod, BorderLayout.SOUTH);
    final JBList list = new JBList(validEnclosingMethods.toArray());
    list.setVisibleRowCount(5);
    list.setCellRenderer(new MethodCellRenderer());
    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() {

        public void valueChanged(final ListSelectionEvent e) {
            final PsiMethod selectedMethod = (PsiMethod) list.getSelectedValue();
            if (selectedMethod == null)
                return;
            dropHighlighters(highlighters);
            updateView(selectedMethod, editor, attributes, highlighters, superMethod);
        }
    });
    updateView(validEnclosingMethods.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 PsiMethod methodToSearchIn = (PsiMethod) list.getSelectedValue();
            if (myEnclosingMethodsPopup != null && myEnclosingMethodsPopup.isVisible()) {
                myEnclosingMethodsPopup.cancel();
            }
            final PsiMethod methodToSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? methodToSearchIn.findDeepestSuperMethod() : methodToSearchIn;
            consumer.consume(methodToSearchIn, methodToSearchFor);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)));
    myEnclosingMethodsPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, list).setTitle("Introduce parameter to method").setMovable(false).setResizable(false).setRequestFocus(true).setKeyboardActions(keyboardActions).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters(highlighters);
        }
    }).createPopup();
    myEnclosingMethodsPopup.showInBestPositionFor(editor);
}
Also used : ActionEvent(java.awt.event.ActionEvent) TIntArrayList(gnu.trove.TIntArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) MethodCellRenderer(com.intellij.refactoring.ui.MethodCellRenderer) ListSelectionListener(javax.swing.event.ListSelectionListener) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) ActionListener(java.awt.event.ActionListener) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBList(com.intellij.ui.components.JBList) Pair(com.intellij.openapi.util.Pair)

Example 8 with LightweightWindowEvent

use of com.intellij.openapi.ui.popup.LightweightWindowEvent 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)

Example 9 with LightweightWindowEvent

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

the class DebuggerTreeWithHistoryPopup method updateContainer.

@Override
protected void updateContainer(final Tree tree, String title) {
    if (myPopup != null) {
        myPopup.cancel();
    }
    tree.getModel().addTreeModelListener(createTreeListener(tree));
    myPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(createMainPanel(tree), tree).setRequestFocus(true).setTitle(title).setResizable(true).setMovable(true).setDimensionServiceKey(myProject, DIMENSION_SERVICE_KEY, false).setMayBeParent(true).setKeyEventHandler(event -> {
        if (AbstractPopup.isCloseRequest(event)) {
            SpeedSearchSupply supply = SpeedSearchSupply.getSupply(tree);
            return supply != null && StringUtil.isEmpty(supply.getEnteredPrefix());
        }
        return false;
    }).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            if (myHideRunnable != null) {
                myHideRunnable.run();
            }
        }
    }).setCancelCallback(() -> {
        Window parent = SwingUtilities.getWindowAncestor(tree);
        if (parent != null) {
            for (Window child : parent.getOwnedWindows()) {
                if (child.isShowing()) {
                    return false;
                }
            }
        }
        return true;
    }).createPopup();
    registerTreeDisposable(myPopup, tree);
    //Editor may be disposed before later invokator process this action
    if (myEditor.getComponent().getRootPane() == null) {
        myPopup.cancel();
        return;
    }
    myPopup.show(new RelativePoint(myEditor.getContentComponent(), myPoint));
    updateInitialBounds(tree);
}
Also used : SpeedSearchSupply(com.intellij.ui.speedSearch.SpeedSearchSupply) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) RelativePoint(com.intellij.ui.awt.RelativePoint) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent)

Example 10 with LightweightWindowEvent

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

the class JBComboBoxTableCellEditorComponent method initAndShowPopup.

private void initAndShowPopup() {
    myList.removeAll();
    myList.setModel(JBList.createDefaultListModel(myOptions));
    if (myRenderer != null) {
        myList.setCellRenderer(myRenderer);
    }
    final Rectangle rect = myTable.getCellRect(myRow, myColumn, true);
    Point point = new Point(rect.x, rect.y);
    final boolean surrendersFocusOnKeystrokeOldValue = myTable instanceof JBTable ? ((JBTable) myTable).surrendersFocusOnKeyStroke() : myTable.getSurrendersFocusOnKeystroke();
    final JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(myList).setItemChoosenCallback(() -> {
        myValue = myList.getSelectedValue();
        final ActionEvent event = new ActionEvent(myList, ActionEvent.ACTION_PERFORMED, "elementChosen");
        for (ActionListener listener : myListeners) {
            listener.actionPerformed(event);
        }
        TableUtil.stopEditing(myTable);
        // on Mac getCellEditorValue() called before myValue is set.
        myTable.setValueAt(myValue, myRow, myColumn);
        // force repaint
        myTable.tableChanged(new TableModelEvent(myTable.getModel(), myRow));
    }).setCancelCallback(() -> {
        TableUtil.stopEditing(myTable);
        return true;
    }).addListener(new JBPopupAdapter() {

        @Override
        public void beforeShown(LightweightWindowEvent event) {
            super.beforeShown(event);
            myTable.setSurrendersFocusOnKeystroke(false);
        }

        @Override
        public void onClosed(LightweightWindowEvent event) {
            myTable.setSurrendersFocusOnKeystroke(surrendersFocusOnKeystrokeOldValue);
            super.onClosed(event);
        }
    }).setMinSize(myWide ? new Dimension(((int) rect.getSize().getWidth()), -1) : null).createPopup();
    popup.show(new RelativePoint(myTable, point));
}
Also used : ActionEvent(java.awt.event.ActionEvent) TableModelEvent(javax.swing.event.TableModelEvent) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) JBTable(com.intellij.ui.table.JBTable) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) ActionListener(java.awt.event.ActionListener) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Aggregations

LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)14 JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)12 JBList (com.intellij.ui.components.JBList)5 RelativePoint (com.intellij.ui.awt.RelativePoint)4 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 ActionListener (java.awt.event.ActionListener)3 Balloon (com.intellij.openapi.ui.popup.Balloon)2 JBPopup (com.intellij.openapi.ui.popup.JBPopup)2 JBPopupListener (com.intellij.openapi.ui.popup.JBPopupListener)2 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2 EverywhereContextType (com.intellij.codeInsight.template.EverywhereContextType)1 TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)1 ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1