Search in sources :

Example 21 with KeyEvent

use of java.awt.event.KeyEvent in project pcgen by PCGen.

the class NotesView method refreshTreeNodes.

//Gui methods
private void refreshTreeNodes() {
    Object obj = notesTree.getLastSelectedPathComponent();
    if (obj instanceof NotesTreeNode) {
        NotesTreeNode node = (NotesTreeNode) obj;
        editor = node.getTextPane();
        root.checkCache();
        // TODO: Uh-oh -- never call gc manually without strong reason
        //			Runtime.getRuntime().gc();
        JViewport vp = new JViewport();
        vp.setView(editor);
        jScrollPane2.setViewport(vp);
        editAreaDT = new DropTarget(editor, new DropEditorListener());
        editor.addCaretListener(this::editorCaretUpdate);
        editor.addKeyListener(new java.awt.event.KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                editorKeyTyped(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
            // TODO:  Method does nothing?
            }

            @Override
            public void keyReleased(KeyEvent e) {
            // TODO:  Method does nothing?
            }
        });
        editor.getStyledDocument().addUndoableEditListener(this::editorUndoableEditHappened);
        if (node.isLeaf()) {
            deleteButton.setEnabled(true);
        } else {
            deleteButton.setEnabled(false);
        }
        if (node == root) {
            exportButton.setEnabled(false);
        } else {
            exportButton.setEnabled(true);
        }
        if (node.isDirty()) {
            revertButton.setEnabled(true);
        } else {
            revertButton.setEnabled(false);
        }
        initFileBar(node.getFiles());
        saveButton.setEnabled(true);
        newButton.setEnabled(true);
    } else if (obj == null) {
        deleteButton.setEnabled(false);
        saveButton.setEnabled(false);
        revertButton.setEnabled(false);
        newButton.setEnabled(false);
    }
}
Also used : KeyEvent(java.awt.event.KeyEvent) JViewport(javax.swing.JViewport) DropTarget(java.awt.dnd.DropTarget)

Example 22 with KeyEvent

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

the class MavenArtifactSearchPanel method initComponents.

private void initComponents(String initialText) {
    myResultList = new Tree();
    myResultList.setExpandableItemsEnabled(false);
    myResultList.getEmptyText().setText("Loading...");
    myResultList.setRootVisible(false);
    myResultList.setShowsRootHandles(true);
    myResultList.setModel(null);
    MyArtifactCellRenderer renderer = myClassMode ? new MyClassCellRenderer(myResultList) : new MyArtifactCellRenderer(myResultList);
    myResultList.setCellRenderer(renderer);
    myResultList.setRowHeight(renderer.getPreferredSize().height);
    mySearchField = new JTextField(initialText);
    mySearchField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            int d;
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                d = 1;
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                d = -1;
            } else {
                return;
            }
            int row = myResultList.getSelectionModel().getLeadSelectionRow();
            row += d;
            if (row >= 0 && row < myResultList.getRowCount()) {
                myResultList.setSelectionRow(row);
            }
        }
    });
    setLayout(new BorderLayout());
    add(mySearchField, BorderLayout.NORTH);
    JScrollPane pane = ScrollPaneFactory.createScrollPane(myResultList);
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    // Don't remove this line.
    pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    // Without VERTICAL_SCROLLBAR_ALWAYS policy our custom layout
    // works incorrectly, see https://youtrack.jetbrains.com/issue/IDEA-72986
    add(pane, BorderLayout.CENTER);
    mySearchField.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            scheduleSearch();
        }
    });
    myResultList.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            if (!myAlarm.isEmpty())
                return;
            boolean hasSelection = !myResultList.isSelectionEmpty();
            myListener.canSelectStateChanged(MavenArtifactSearchPanel.this, hasSelection);
        }
    });
    myResultList.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && myResultList.getLastSelectedPathComponent() != null) {
                myListener.itemSelected();
                e.consume();
            }
        }
    });
    new DoubleClickListener() {

        @Override
        protected boolean onDoubleClick(MouseEvent e) {
            final TreePath path = myResultList.getPathForLocation(e.getX(), e.getY());
            if (path != null && myResultList.isPathSelected(path)) {
                Object sel = path.getLastPathComponent();
                if (sel != null && myResultList.getModel().isLeaf(sel)) {
                    myListener.itemSelected();
                    return true;
                }
            }
            return false;
        }
    }.installOn(myResultList);
}
Also used : MouseEvent(java.awt.event.MouseEvent) KeyAdapter(java.awt.event.KeyAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DocumentEvent(javax.swing.event.DocumentEvent) KeyEvent(java.awt.event.KeyEvent) TreePath(javax.swing.tree.TreePath) Tree(com.intellij.ui.treeStructure.Tree) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 23 with KeyEvent

use of java.awt.event.KeyEvent 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 24 with KeyEvent

use of java.awt.event.KeyEvent 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 25 with KeyEvent

use of java.awt.event.KeyEvent 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)

Aggregations

KeyEvent (java.awt.event.KeyEvent)135 KeyAdapter (java.awt.event.KeyAdapter)69 MouseEvent (java.awt.event.MouseEvent)31 ActionEvent (java.awt.event.ActionEvent)23 ActionListener (java.awt.event.ActionListener)19 KeyListener (java.awt.event.KeyListener)19 JPanel (javax.swing.JPanel)18 MouseAdapter (java.awt.event.MouseAdapter)17 JLabel (javax.swing.JLabel)17 JButton (javax.swing.JButton)14 JTextField (javax.swing.JTextField)14 JScrollPane (javax.swing.JScrollPane)13 BorderLayout (java.awt.BorderLayout)12 NotNull (org.jetbrains.annotations.NotNull)12 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)11 TreePath (javax.swing.tree.TreePath)11 FlowLayout (java.awt.FlowLayout)10 Tree (com.intellij.ui.treeStructure.Tree)9 Project (com.intellij.openapi.project.Project)8 Dimension (java.awt.Dimension)8