Search in sources :

Example 6 with JBPopupAdapter

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

use of com.intellij.openapi.ui.popup.JBPopupAdapter 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 8 with JBPopupAdapter

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

Example 9 with JBPopupAdapter

use of com.intellij.openapi.ui.popup.JBPopupAdapter 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 10 with JBPopupAdapter

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

the class LoadContextAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = getProject(e);
    assert project != null;
    DefaultActionGroup group = new DefaultActionGroup();
    final WorkingContextManager manager = WorkingContextManager.getInstance(project);
    List<ContextInfo> history = manager.getContextHistory();
    List<ContextHolder> infos = new ArrayList<>(ContainerUtil.map2List(history, (Function<ContextInfo, ContextHolder>) info -> new ContextHolder() {

        @Override
        void load(final boolean clear) {
            LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, info.name);
            UndoableCommand.execute(project, undoableAction, "Load context " + info.comment, "Context");
        }

        @Override
        void remove() {
            manager.removeContext(info.name);
        }

        @Override
        Date getDate() {
            return new Date(info.date);
        }

        @Override
        String getComment() {
            return info.comment;
        }

        @Override
        Icon getIcon() {
            return TasksIcons.SavedContext;
        }
    }));
    final TaskManager taskManager = TaskManager.getManager(project);
    List<LocalTask> tasks = taskManager.getLocalTasks();
    infos.addAll(ContainerUtil.mapNotNull(tasks, (NullableFunction<LocalTask, ContextHolder>) task -> {
        if (task.isActive()) {
            return null;
        }
        return new ContextHolder() {

            @Override
            void load(boolean clear) {
                LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, task);
                UndoableCommand.execute(project, undoableAction, "Load context " + TaskUtil.getTrimmedSummary(task), "Context");
            }

            @Override
            void remove() {
                SwitchTaskAction.removeTask(project, task, taskManager);
            }

            @Override
            Date getDate() {
                return task.getUpdated();
            }

            @Override
            String getComment() {
                return TaskUtil.getTrimmedSummary(task);
            }

            @Override
            Icon getIcon() {
                return task.getIcon();
            }
        };
    }));
    Collections.sort(infos, (o1, o2) -> o2.getDate().compareTo(o1.getDate()));
    final Ref<Boolean> shiftPressed = Ref.create(false);
    boolean today = true;
    Calendar now = Calendar.getInstance();
    for (int i = 0, historySize = Math.min(MAX_ROW_COUNT, infos.size()); i < historySize; i++) {
        final ContextHolder info = infos.get(i);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(info.getDate());
        if (today && (calendar.get(Calendar.YEAR) != now.get(Calendar.YEAR) || calendar.get(Calendar.DAY_OF_YEAR) != now.get(Calendar.DAY_OF_YEAR))) {
            group.addSeparator();
            today = false;
        }
        group.add(createItem(info, shiftPressed));
    }
    final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createActionGroupPopup("Load Context", group, e.getDataContext(), false, null, MAX_ROW_COUNT);
    popup.setAdText("Press SHIFT to merge with current context");
    popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(true);
            popup.setCaption("Merge with Current Context");
        }
    });
    popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(false);
            popup.setCaption("Load Context");
        }
    });
    popup.registerAction("invoke", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            popup.handleSelect(true);
        }
    });
    popup.addPopupListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
        }
    });
    popup.showCenteredInCurrentWindow(project);
}
Also used : ActionEvent(java.awt.event.ActionEvent) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) NullableFunction(com.intellij.util.NullableFunction) Function(com.intellij.util.Function) ContextInfo(com.intellij.tasks.context.ContextInfo) WorkingContextManager(com.intellij.tasks.context.WorkingContextManager) LocalTask(com.intellij.tasks.LocalTask) NullableFunction(com.intellij.util.NullableFunction) Project(com.intellij.openapi.project.Project) LoadContextUndoableAction(com.intellij.tasks.context.LoadContextUndoableAction) TaskManager(com.intellij.tasks.TaskManager) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter)

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