Search in sources :

Example 1 with KeyListener

use of java.awt.event.KeyListener in project enumerable by hraberg.

the class LambdaTest method lambdaAsKeyListenerWithRegex.

@Test
public void lambdaAsKeyListenerWithRegex() throws Exception {
    List<KeyEvent> events = new ArrayList<KeyEvent>();
    KeyListener keyListener = λ(ke, events.add(ke)).as(KeyListener.class, ".*Typed");
    KeyEvent event = new KeyEvent(new JButton(), 0, 0, 0, 0, (char) 0);
    keyListener.keyPressed(event);
    assertTrue(events.isEmpty());
    keyListener.keyReleased(event);
    assertTrue(events.isEmpty());
    keyListener.keyTyped(event);
    assertEquals(1, events.size());
}
Also used : KeyEvent(java.awt.event.KeyEvent) KeyListener(java.awt.event.KeyListener) Test(org.junit.Test)

Example 2 with KeyListener

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

the class AddRepositoryLocationDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    JLabel selectText = new JLabel(message("repository.browser.add.location.prompt"));
    selectText.setUI(new MultiLineLabelUI());
    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gb = new GridBagConstraints(0, 0, 1, 1, 1, 0, NORTHWEST, NONE, insets(5), 0, 0);
    mainPanel.add(selectText, gb);
    ++gb.gridy;
    myCombo = new ComboBox<>(new CollectionComboBoxModel<>(myPreviousLocations));
    myCombo.setEditable(true);
    myCombo.setMinimumSize(size(250, 20));
    gb.fill = HORIZONTAL;
    mainPanel.add(myCombo, gb);
    gb.fill = NONE;
    myComboField = (JTextField) myCombo.getEditor().getEditorComponent();
    myComboField.addInputMethodListener(new InputMethodListener() {

        public void inputMethodTextChanged(InputMethodEvent event) {
            validateMe();
        }

        public void caretPositionChanged(InputMethodEvent event) {
            validateMe();
        }
    });
    myComboField.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
            validateMe();
        }

        public void keyPressed(KeyEvent e) {
            validateMe();
        }

        public void keyReleased(KeyEvent e) {
            validateMe();
        }
    });
    myCombo.addActionListener(e -> validateMe());
    validateMe();
    JPanel wrapper = new JPanel(new GridBagLayout());
    wrapper.add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1, NORTHWEST, HORIZONTAL, emptyInsets(), 0, 0));
    wrapper.setPreferredSize(size(400, 70));
    return wrapper;
}
Also used : MultiLineLabelUI(com.intellij.openapi.ui.MultiLineLabelUI) KeyEvent(java.awt.event.KeyEvent) GridBagConstraints(java.awt.GridBagConstraints) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) InputMethodListener(java.awt.event.InputMethodListener) KeyListener(java.awt.event.KeyListener) InputMethodEvent(java.awt.event.InputMethodEvent)

Example 3 with KeyListener

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

the class CheckboxTreeHelper method initTree.

public void initTree(@NotNull final Tree tree, JComponent mainComponent, CheckboxTreeBase.CheckboxTreeCellRendererBase cellRenderer) {
    removeTreeListeners(mainComponent);
    tree.setCellRenderer(cellRenderer);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.setLineStyleAngled();
    TreeUtil.installActions(tree);
    KeyListener keyListener = setupKeyListener(tree, mainComponent);
    ClickListener clickListener = setupMouseListener(tree, mainComponent, cellRenderer);
    UIUtil.putClientProperty(mainComponent, TREE_LISTENERS_REMOVER, () -> {
        mainComponent.removeKeyListener(keyListener);
        clickListener.uninstall(mainComponent);
    });
}
Also used : KeyListener(java.awt.event.KeyListener)

Example 4 with KeyListener

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

the class ImageOrColorPreviewManager method registerListeners.

private void registerListeners(final Editor editor) {
    if (editor.isOneLineMode()) {
        return;
    }
    Project project = editor.getProject();
    if (project == null || project.isDisposed()) {
        return;
    }
    PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null || psiFile instanceof PsiCompiledElement || !isSupportedFile(psiFile)) {
        return;
    }
    editor.addEditorMouseMotionListener(this);
    KeyListener keyListener = new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT && !editor.isOneLineMode()) {
                PointerInfo pointerInfo = MouseInfo.getPointerInfo();
                if (pointerInfo != null) {
                    Point location = pointerInfo.getLocation();
                    SwingUtilities.convertPointFromScreen(location, editor.getContentComponent());
                    alarm.cancelAllRequests();
                    alarm.addRequest(new PreviewRequest(location, editor, true), 100);
                }
            }
        }
    };
    editor.getContentComponent().addKeyListener(keyListener);
    EDITOR_LISTENER_ADDED.set(editor, keyListener);
}
Also used : KeyEvent(java.awt.event.KeyEvent) Project(com.intellij.openapi.project.Project) KeyAdapter(java.awt.event.KeyAdapter) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) PsiFile(com.intellij.psi.PsiFile) KeyListener(java.awt.event.KeyListener)

Example 5 with KeyListener

use of java.awt.event.KeyListener in project processing by processing.

the class JavaTextArea method tweakRemoveListeners.

/* remove all standard interaction listeners */
public void tweakRemoveListeners() {
    if (baseCompListeners == null) {
        // First time in tweak mode, grab the default listeners. Moved from the
        // constructor since not all listeners may have been added at that point.
        baseCompListeners = painter.getComponentListeners();
        baseMouseListeners = painter.getMouseListeners();
        baseMotionListeners = painter.getMouseMotionListeners();
        baseKeyListeners = editor.getKeyListeners();
    }
    ComponentListener[] componentListeners = painter.getComponentListeners();
    MouseListener[] mouseListeners = painter.getMouseListeners();
    MouseMotionListener[] mouseMotionListeners = painter.getMouseMotionListeners();
    KeyListener[] keyListeners = editor.getKeyListeners();
    for (ComponentListener cl : componentListeners) {
        painter.removeComponentListener(cl);
    }
    for (MouseListener ml : mouseListeners) {
        painter.removeMouseListener(ml);
    }
    for (MouseMotionListener mml : mouseMotionListeners) {
        painter.removeMouseMotionListener(mml);
    }
    for (KeyListener kl : keyListeners) {
        editor.removeKeyListener(kl);
    }
}
Also used : ComponentListener(java.awt.event.ComponentListener) MouseListener(java.awt.event.MouseListener) KeyListener(java.awt.event.KeyListener) MouseMotionListener(java.awt.event.MouseMotionListener)

Aggregations

KeyListener (java.awt.event.KeyListener)64 KeyEvent (java.awt.event.KeyEvent)53 JPanel (javax.swing.JPanel)20 ActionEvent (java.awt.event.ActionEvent)18 ActionListener (java.awt.event.ActionListener)17 GridBagConstraints (java.awt.GridBagConstraints)15 JLabel (javax.swing.JLabel)14 JTextField (javax.swing.JTextField)13 GridBagLayout (java.awt.GridBagLayout)12 Insets (java.awt.Insets)11 BorderLayout (java.awt.BorderLayout)8 Dimension (java.awt.Dimension)8 FocusEvent (java.awt.event.FocusEvent)8 FocusListener (java.awt.event.FocusListener)8 JButton (javax.swing.JButton)8 Point (java.awt.Point)7 MouseAdapter (java.awt.event.MouseAdapter)6 MouseEvent (java.awt.event.MouseEvent)6 JComponent (javax.swing.JComponent)5 Color (java.awt.Color)4