Search in sources :

Example 16 with KeyAdapter

use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.

the class MavenSelectProjectPopup method attachToButton.

public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            List<MavenProject> projectList = projectsManager.getProjects();
            if (projectList.isEmpty()) {
                JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
                return;
            }
            DefaultMutableTreeNode root = buildTree(projectList);
            final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
            final Tree projectTree = new Tree(root);
            projectTree.setRootVisible(false);
            projectTree.setCellRenderer(new NodeRenderer() {

                @Override
                public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    if (value instanceof DefaultMutableTreeNode) {
                        MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
                        value = projectsNameMap.get(mavenProject);
                        setIcon(MavenIcons.MavenProject);
                    }
                    super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
                }
            });
            new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {

                @Override
                public String convert(TreePath o) {
                    Object lastPathComponent = o.getLastPathComponent();
                    if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                        return null;
                    Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                    //noinspection SuspiciousMethodCalls
                    return projectsNameMap.get(userObject);
                }
            });
            final Ref<JBPopup> popupRef = new Ref<>();
            final Runnable clickCallBack = () -> {
                TreePath path = projectTree.getSelectionPath();
                if (path == null)
                    return;
                Object lastPathComponent = path.getLastPathComponent();
                if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                    return;
                Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                // may be it's the root
                if (object == null)
                    return;
                callback.consume((MavenProject) object);
                popupRef.get().closeOk(null);
            };
            projectTree.addKeyListener(new KeyAdapter() {

                @Override
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        clickCallBack.run();
                        e.consume();
                    }
                }
            });
            JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
            popupRef.set(popup);
            popup.showUnderneathOf(button);
        }

        private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
            MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
            Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
            Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
            for (MavenProject mavenProject : projects) {
                projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
            }
            DefaultMutableTreeNode root = new DefaultMutableTreeNode();
            for (MavenProject mavenProject : projects) {
                DefaultMutableTreeNode parent;
                MavenProject aggregator = projectsManager.findAggregator(mavenProject);
                if (aggregator != null) {
                    parent = projectsToNode.get(aggregator);
                } else {
                    parent = root;
                }
                parent.add(projectsToNode.get(mavenProject));
            }
            return root;
        }
    });
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) KeyAdapter(java.awt.event.KeyAdapter) Convertor(com.intellij.util.containers.Convertor) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) KeyEvent(java.awt.event.KeyEvent) Ref(com.intellij.openapi.util.Ref) ActionListener(java.awt.event.ActionListener) MavenProject(org.jetbrains.idea.maven.project.MavenProject) TreePath(javax.swing.tree.TreePath) Tree(com.intellij.ui.treeStructure.Tree) List(java.util.List) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with KeyAdapter

use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.

the class MavenSelectProjectPopup method attachToWorkingDirectoryField.

public static void attachToWorkingDirectoryField(@NotNull final MavenProjectsManager projectsManager, final JTextField workingDirectoryField, final JButton showModulesButton, @Nullable final JComponent focusAfterSelection) {
    attachToButton(projectsManager, showModulesButton, project -> {
        workingDirectoryField.setText(project.getDirectory());
        if (focusAfterSelection != null) {
            ApplicationManager.getApplication().invokeLater(() -> {
                if (workingDirectoryField.hasFocus()) {
                    IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                        IdeFocusManager.getGlobalInstance().requestFocus(focusAfterSelection, true);
                    });
                }
            });
        }
    });
    workingDirectoryField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (!e.isConsumed()) {
                    // May be consumed by path completion.
                    e.consume();
                    showModulesButton.doClick();
                }
            }
        }
    });
}
Also used : KeyEvent(java.awt.event.KeyEvent) KeyAdapter(java.awt.event.KeyAdapter)

Example 18 with KeyAdapter

use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.

the class PyDataViewerPanel method createEditorField.

@NotNull
private EditorTextField createEditorField() {
    return new EditorTextField(EditorFactory.getInstance().createDocument(""), myProject, PythonFileType.INSTANCE, false, true) {

        @Override
        protected EditorEx createEditor() {
            EditorEx editor = super.createEditor();
            editor.getContentComponent().addKeyListener(new KeyAdapter() {

                @Override
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        apply(mySliceTextField.getText());
                    }
                }
            });
            return editor;
        }
    };
}
Also used : KeyEvent(java.awt.event.KeyEvent) EditorEx(com.intellij.openapi.editor.ex.EditorEx) EditorTextField(com.intellij.ui.EditorTextField) KeyAdapter(java.awt.event.KeyAdapter) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with KeyAdapter

use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.

the class IpnbEditablePanel method createEditablePanel.

private JTextArea createEditablePanel() {
    final JTextArea textArea = new JTextArea(getRawCellText());
    textArea.setLineWrap(true);
    textArea.setEditable(true);
    textArea.setBorder(BorderFactory.createLineBorder(JBColor.lightGray));
    textArea.setBackground(IpnbEditorUtil.getEditablePanelBackground());
    textArea.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1) {
                setEditing(true);
                final Container parent = getParent();
                parent.repaint();
                if (parent instanceof IpnbFilePanel) {
                    ((IpnbFilePanel) parent).setSelectedCellPanel(IpnbEditablePanel.this);
                    IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                        IdeFocusManager.getGlobalInstance().requestFocus(textArea, true);
                    });
                }
            }
        }
    });
    textArea.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                setEditing(false);
                final Container parent = getParent();
                if (parent instanceof IpnbFilePanel) {
                    parent.repaint();
                    UIUtil.requestFocus((IpnbFilePanel) parent);
                }
            }
        }
    });
    return textArea;
}
Also used : KeyEvent(java.awt.event.KeyEvent) MouseEvent(java.awt.event.MouseEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseAdapter(java.awt.event.MouseAdapter)

Example 20 with KeyAdapter

use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.

the class EmmetAbbreviationBalloon method show.

public void show(@NotNull final CustomTemplateCallback customTemplateCallback) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (ourTestingAbbreviation == null) {
            throw new RuntimeException("Testing abbreviation is not set. See EmmetAbbreviationBalloon#setTestingAbbreviation");
        }
        myCallback.onEnter(ourTestingAbbreviation);
        return;
    }
    final TextFieldWithStoredHistory field = new TextFieldWithStoredHistory(myAbbreviationsHistoryKey);
    final Dimension fieldPreferredSize = field.getPreferredSize();
    field.setPreferredSize(new Dimension(Math.max(220, fieldPreferredSize.width), fieldPreferredSize.height));
    field.setHistorySize(10);
    final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
    final BalloonImpl balloon = (BalloonImpl) popupFactory.createDialogBalloonBuilder(field, myTitle).setCloseButtonEnabled(false).setBlockClicksThroughBalloon(true).setAnimationCycle(0).setHideOnKeyOutside(true).setHideOnClickOutside(true).createBalloon();
    final DocumentAdapter documentListener = new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            if (!isValid(customTemplateCallback)) {
                balloon.hide();
                return;
            }
            validateTemplateKey(field, balloon, field.getText(), customTemplateCallback);
        }
    };
    field.addDocumentListener(documentListener);
    final KeyAdapter keyListener = new KeyAdapter() {

        @Override
        public void keyPressed(@NotNull KeyEvent e) {
            if (!field.isPopupVisible()) {
                if (!isValid(customTemplateCallback)) {
                    balloon.hide();
                    return;
                }
                switch(e.getKeyCode()) {
                    case KeyEvent.VK_ENTER:
                        final String abbreviation = field.getText();
                        if (validateTemplateKey(field, balloon, abbreviation, customTemplateCallback)) {
                            myCallback.onEnter(abbreviation);
                            PropertiesComponent.getInstance().setValue(myLastAbbreviationKey, abbreviation);
                            field.addCurrentTextToHistory();
                            balloon.hide();
                        }
                        break;
                    case KeyEvent.VK_ESCAPE:
                        balloon.hide(false);
                        break;
                }
            }
        }
    };
    field.addKeyboardListener(keyListener);
    balloon.addListener(new JBPopupListener.Adapter() {

        @Override
        public void beforeShown(LightweightWindowEvent event) {
            field.setText(PropertiesComponent.getInstance().getValue(myLastAbbreviationKey, ""));
        }

        @Override
        public void onClosed(LightweightWindowEvent event) {
            field.removeKeyListener(keyListener);
            field.removeDocumentListener(documentListener);
            super.onClosed(event);
        }
    });
    balloon.show(popupFactory.guessBestPopupLocation(customTemplateCallback.getEditor()), Balloon.Position.below);
    final IdeFocusManager focusManager = IdeFocusManager.getInstance(customTemplateCallback.getProject());
    focusManager.doWhenFocusSettlesDown(() -> {
        focusManager.requestFocus(field, true);
        field.selectText();
    });
}
Also used : KeyAdapter(java.awt.event.KeyAdapter) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) DocumentEvent(javax.swing.event.DocumentEvent) NotNull(org.jetbrains.annotations.NotNull) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) KeyEvent(java.awt.event.KeyEvent) JBPopupListener(com.intellij.openapi.ui.popup.JBPopupListener) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory)

Aggregations

KeyAdapter (java.awt.event.KeyAdapter)70 KeyEvent (java.awt.event.KeyEvent)70 MouseEvent (java.awt.event.MouseEvent)26 MouseAdapter (java.awt.event.MouseAdapter)16 JPanel (javax.swing.JPanel)15 ActionEvent (java.awt.event.ActionEvent)14 JLabel (javax.swing.JLabel)14 ActionListener (java.awt.event.ActionListener)13 JButton (javax.swing.JButton)12 JScrollPane (javax.swing.JScrollPane)12 BorderLayout (java.awt.BorderLayout)11 JTextField (javax.swing.JTextField)11 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)11 TreePath (javax.swing.tree.TreePath)11 NotNull (org.jetbrains.annotations.NotNull)11 FlowLayout (java.awt.FlowLayout)10 Tree (com.intellij.ui.treeStructure.Tree)9 Dimension (java.awt.Dimension)8 ListSelectionEvent (javax.swing.event.ListSelectionEvent)7 ListSelectionListener (javax.swing.event.ListSelectionListener)7