Search in sources :

Example 11 with DumbAwareAction

use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.

the class RunIdeConsoleAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    List<String> languages = IdeScriptEngineManager.getInstance().getLanguages();
    if (languages.size() == 1) {
        runConsole(e, languages.iterator().next());
        return;
    }
    DefaultActionGroup actions = new DefaultActionGroup(ContainerUtil.map(languages, (NotNullFunction<String, AnAction>) language -> new DumbAwareAction(language) {

        @Override
        public void actionPerformed(@NotNull AnActionEvent e1) {
            runConsole(e1, language);
        }
    }));
    JBPopupFactory.getInstance().createActionGroupPopup("Script Engine", actions, e.getDataContext(), JBPopupFactory.ActionSelectionAid.NUMBERING, false).showInBestPositionFor(e.getDataContext());
}
Also used : NotNullFunction(com.intellij.util.NotNullFunction) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction)

Example 12 with DumbAwareAction

use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.

the class ChooseByNameBase method initUI.

/**
   * @param modalityState          - if not null rebuilds list in given {@link ModalityState}
   */
protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState, final boolean allowMultipleSelection) {
    myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject);
    myActionListener = callback;
    myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS));
    final JPanel hBox = new JPanel();
    hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS));
    JPanel caption2Tools = new JPanel(new BorderLayout());
    if (myModel.getPromptText() != null) {
        JLabel label = new JLabel(myModel.getPromptText());
        label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));
        caption2Tools.add(label, BorderLayout.WEST);
    }
    caption2Tools.add(hBox, BorderLayout.EAST);
    // space between checkbox and filter/show all in view buttons
    myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
    final String checkBoxName = myModel.getCheckBoxName();
    myCheckBox.setText(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null && myCheckBoxShortcut.getShortcuts().length > 0 ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")" : "") : "");
    myCheckBox.setAlignmentX(SwingConstants.RIGHT);
    if (!SystemInfo.isMac) {
        myCheckBox.setBorder(null);
    }
    myCheckBox.setSelected(myModel.loadInitialCheckBoxState());
    if (checkBoxName == null) {
        myCheckBox.setVisible(false);
    }
    addCard(myCheckBox, CHECK_BOX_CARD);
    addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD);
    addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD);
    JPanel searching = new JPanel(new BorderLayout(5, 0));
    searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST);
    searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER);
    addCard(searching, SEARCHING_CARD);
    myCard.show(myCardContainer, CHECK_BOX_CARD);
    if (isCheckboxVisible()) {
        hBox.add(myCardContainer);
    }
    final DefaultActionGroup group = new DefaultActionGroup();
    group.add(new ShowFindUsagesAction() {

        @Override
        public PsiElement[][] getElements() {
            final List<Object> objects = myListModel.getItems();
            final List<PsiElement> prefixMatchElements = new ArrayList<>(objects.size());
            final List<PsiElement> nonPrefixMatchElements = new ArrayList<>(objects.size());
            List<PsiElement> curElements = prefixMatchElements;
            for (Object object : objects) {
                if (object instanceof PsiElement) {
                    curElements.add((PsiElement) object);
                } else if (object instanceof DataProvider) {
                    final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object);
                    if (psi != null) {
                        curElements.add(psi);
                    }
                } else if (object == NON_PREFIX_SEPARATOR) {
                    curElements = nonPrefixMatchElements;
                }
            }
            return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements), PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) };
        }
    });
    final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
    actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
    // we need valid ActionToolbar.getPreferredSize() to calc size of popup
    actionToolbar.updateActionsImmediately();
    final JComponent toolbarComponent = actionToolbar.getComponent();
    toolbarComponent.setBorder(null);
    if (myToolArea == null) {
        myToolArea = new JLabel(JBUI.scale(EmptyIcon.create(1, 24)));
    }
    hBox.add(myToolArea);
    hBox.add(toolbarComponent);
    myTextFieldPanel.add(caption2Tools);
    new MyCopyReferenceAction().registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.ACTION_COPY).getShortcutSet(), myTextField);
    myTextFieldPanel.add(myTextField);
    Font editorFont = EditorUtil.getEditorFont();
    myTextField.setFont(editorFont);
    if (checkBoxName != null) {
        if (myCheckBoxShortcut != null) {
            new DumbAwareAction("change goto check box", null, null) {

                @Override
                public void actionPerformed(@NotNull AnActionEvent e) {
                    myCheckBox.setSelected(!myCheckBox.isSelected());
                }
            }.registerCustomShortcutSet(myCheckBoxShortcut, myTextField);
        }
    }
    if (isCloseByFocusLost()) {
        myTextField.addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(@NotNull final FocusEvent e) {
                if (Registry.is("focus.follows.mouse.workarounds")) {
                    if (myFocusPoint != null) {
                        PointerInfo pointerInfo = MouseInfo.getPointerInfo();
                        if (pointerInfo != null && myFocusPoint.equals(pointerInfo.getLocation())) {
                            // Ignore the loss of focus if the mouse hasn't moved between the last dropdown resize
                            // and the loss of focus event. This happens in focus follows mouse mode if the mouse is
                            // over the dropdown and it resizes to leave the mouse outside the dropdown.
                            IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                            myFocusPoint = null;
                            return;
                        }
                    }
                    myFocusPoint = null;
                }
                // cancel thread as early as possible
                cancelListUpdater();
                myHideAlarm.addRequest(() -> {
                    JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent());
                    if (popup != null) {
                        popup.addListener(new JBPopupListener.Adapter() {

                            @Override
                            public void onClosed(@NotNull LightweightWindowEvent event) {
                                if (event.isOk()) {
                                    hideHint();
                                }
                            }
                        });
                    } else {
                        Component oppositeComponent = e.getOppositeComponent();
                        if (oppositeComponent == myCheckBox) {
                            IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                            return;
                        }
                        if (oppositeComponent != null && !(oppositeComponent instanceof JFrame) && myList.isShowing() && (oppositeComponent == myList || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) {
                            // Otherwise me may skip some KeyEvents
                            IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                            return;
                        }
                        if (isDescendingFromTemporarilyFocusableToolWindow(oppositeComponent)) {
                            // Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow)
                            return;
                        }
                        hideHint();
                    }
                }, 5);
            }
        });
    }
    myCheckBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(@NotNull ItemEvent e) {
            rebuildList(false);
        }
    });
    myCheckBox.setFocusable(false);
    myTextField.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            rebuildList(false);
        }
    });
    final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
    final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN);
    myTextField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(@NotNull KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
                myClosedByShiftEnter = true;
                close(true);
            }
            if (!myListScrollPane.isVisible()) {
                return;
            }
            final int keyCode;
            // Add support for user-defined 'caret up/down' shortcuts.
            KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
            if (upShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_UP;
            } else if (downShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_DOWN;
            } else {
                keyCode = e.getKeyCode();
            }
            switch(keyCode) {
                case KeyEvent.VK_DOWN:
                    ScrollingUtil.moveDown(myList, e.getModifiersEx());
                    break;
                case KeyEvent.VK_UP:
                    ScrollingUtil.moveUp(myList, e.getModifiersEx());
                    break;
                case KeyEvent.VK_PAGE_UP:
                    ScrollingUtil.movePageUp(myList);
                    break;
                case KeyEvent.VK_PAGE_DOWN:
                    ScrollingUtil.movePageDown(myList);
                    break;
                case KeyEvent.VK_TAB:
                    close(true);
                    break;
                case KeyEvent.VK_ENTER:
                    if (myList.getSelectedValue() == EXTRA_ELEM) {
                        myMaximumListSizeLimit += myListSizeIncreasing;
                        rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null);
                        e.consume();
                    }
                    break;
            }
            if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) {
                if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) {
                    ScrollingUtil.moveUp(myList, e.getModifiersEx());
                } else {
                    ScrollingUtil.moveDown(myList, e.getModifiersEx());
                }
            }
        }
    });
    myTextField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(@NotNull ActionEvent actionEvent) {
            doClose(true);
        }
    });
    myList.setFocusable(false);
    myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (!myTextField.hasFocus()) {
                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
            }
            if (clickCount == 2) {
                int selectedIndex = myList.getSelectedIndex();
                Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex);
                if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) {
                    // Otherwise it was reselected in the selection listener
                    if (myList.getSelectedValue() == EXTRA_ELEM) {
                        myMaximumListSizeLimit += myListSizeIncreasing;
                        rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null);
                    } else {
                        doClose(true);
                    }
                }
                return true;
            }
            return false;
        }
    }.installOn(myList);
    myList.setCellRenderer(myModel.getListCellRenderer());
    myList.setFont(editorFont);
    myList.addListSelectionListener(new ListSelectionListener() {

        private int myPreviousSelectionIndex = 0;

        @Override
        public void valueChanged(@NotNull ListSelectionEvent e) {
            if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) {
                myPreviousSelectionIndex = myList.getSelectedIndex();
                chosenElementMightChange();
                updateDocumentation();
            } else if (allowMultipleSelection) {
                myList.setSelectedIndex(myPreviousSelectionIndex);
            }
        }
    });
    myListScrollPane = ScrollPaneFactory.createScrollPane(myList);
    myListScrollPane.setViewportBorder(JBUI.Borders.empty());
    myTextFieldPanel.setBorder(JBUI.Borders.empty(5));
    showTextFieldPanel();
    myInitialized = true;
    if (modalityState != null) {
        rebuildList(myInitialIndex, 0, modalityState, null);
    }
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull) List(java.util.List) JBList(com.intellij.ui.components.JBList) PsiElement(com.intellij.psi.PsiElement) DocumentEvent(javax.swing.event.DocumentEvent) ListSelectionListener(javax.swing.event.ListSelectionListener)

Example 13 with DumbAwareAction

use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.

the class ProjectSdksModel method createAddActions.

public void createAddActions(@NotNull DefaultActionGroup group, @NotNull final JComponent parent, @Nullable final Sdk selectedSdk, @NotNull final Consumer<Sdk> updateTree, @Nullable Condition<SdkTypeId> filter) {
    final SdkType[] types = SdkType.getAllTypes();
    for (final SdkType type : types) {
        if (filter != null && !filter.value(type))
            continue;
        final AnAction addAction = new DumbAwareAction(type.getPresentableName(), null, type.getIconForAddAction()) {

            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                doAdd(parent, selectedSdk, type, updateTree);
            }
        };
        group.add(addAction);
    }
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with DumbAwareAction

use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.

the class PerFileConfigurableBase method createActionListGroup.

@NotNull
protected ActionGroup createActionListGroup(@Nullable Object target, @NotNull Consumer<T> onChosen) {
    DefaultActionGroup group = new DefaultActionGroup();
    String clearText = getClearValueText(target);
    Function<T, AnAction> choseAction = t -> {
        String nullValue = StringUtil.notNullize(clearText);
        AnAction a = new DumbAwareAction(renderValue(t, nullValue), "", getActionListIcon(target, t)) {

            @Override
            public void actionPerformed(AnActionEvent e) {
                onChosen.consume(t);
            }
        };
        a.getTemplatePresentation().setText(renderValue(t, nullValue));
        return a;
    };
    if (clearText != null) {
        group.add(choseAction.fun(null));
    }
    SimpleColoredText text = new SimpleColoredText();
    List<T> values = new ArrayList<>(getValueVariants(target));
    Function<T, String> toString = o -> {
        text.clear();
        renderValue(target, o, text);
        return text.toString();
    };
    Collections.sort(values, (o1, o2) -> StringUtil.naturalCompare(toString.fun(o1), toString.fun(o2)));
    for (T t : values) {
        group.add(choseAction.fun(t));
    }
    return group;
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) UIUtil(com.intellij.util.ui.UIUtil) AbstractTableCellEditor(com.intellij.util.ui.AbstractTableCellEditor) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) javax.swing.table(javax.swing.table) LanguagePerFileMappings(com.intellij.lang.LanguagePerFileMappings) PerFileMappingsBase(com.intellij.lang.PerFileMappingsBase) ContainerUtil(com.intellij.util.containers.ContainerUtil) JBLabel(com.intellij.ui.components.JBLabel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JBUI(com.intellij.util.ui.JBUI) Project(com.intellij.openapi.project.Project) SpeedSearchUtil(com.intellij.ui.speedSearch.SpeedSearchUtil) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) Messages(com.intellij.openapi.ui.Messages) PerFileMappings(com.intellij.lang.PerFileMappings) FileUtil(com.intellij.openapi.util.io.FileUtil) ComboBoxAction(com.intellij.openapi.actionSystem.ex.ComboBoxAction) TIntArrayList(gnu.trove.TIntArrayList) CustomComponentAction(com.intellij.openapi.actionSystem.ex.CustomComponentAction) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) Configurable(com.intellij.openapi.options.Configurable) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) com.intellij.ui(com.intellij.ui) Collectors(java.util.stream.Collectors) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) File(java.io.File) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) JBTable(com.intellij.ui.table.JBTable) Nullable(org.jetbrains.annotations.Nullable) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) IdeBorderFactory(com.intellij.ui.IdeBorderFactory) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ConfigurationException(com.intellij.openapi.options.ConfigurationException) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) FileChooser(com.intellij.openapi.fileChooser.FileChooser) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) TIntArrayList(gnu.trove.TIntArrayList) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with DumbAwareAction

use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.

the class ChooseFileEncodingAction method fillCharsetActions.

private void fillCharsetActions(@NotNull DefaultActionGroup group, @Nullable final VirtualFile virtualFile, @NotNull List<Charset> charsets, @NotNull final Function<Charset, String> charsetFilter) {
    for (final Charset charset : charsets) {
        AnAction action = new DumbAwareAction(charset.displayName(), null, EmptyIcon.ICON_16) {

            @Override
            public void actionPerformed(AnActionEvent e) {
                chosen(virtualFile, charset);
            }

            @Override
            public void update(AnActionEvent e) {
                super.update(e);
                String description = charsetFilter.fun(charset);
                e.getPresentation().setIcon(description == null ? AllIcons.General.Warning : null);
                e.getPresentation().setDescription(description);
            }
        };
        group.add(action);
    }
}
Also used : Charset(java.nio.charset.Charset) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction)

Aggregations

DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)41 NotNull (org.jetbrains.annotations.NotNull)19 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)18 AnAction (com.intellij.openapi.actionSystem.AnAction)8 Nullable (org.jetbrains.annotations.Nullable)8 List (java.util.List)6 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)4 FileChooser (com.intellij.openapi.fileChooser.FileChooser)4 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)4 SearchableConfigurable (com.intellij.openapi.options.SearchableConfigurable)4 Project (com.intellij.openapi.project.Project)4 Messages (com.intellij.openapi.ui.Messages)4 JBPopup (com.intellij.openapi.ui.popup.JBPopup)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4 JBTable (com.intellij.ui.table.JBTable)4 MouseEvent (java.awt.event.MouseEvent)4 StdFileTypes (com.intellij.openapi.fileTypes.StdFileTypes)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)3 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3