Search in sources :

Example 31 with KeyListener

use of java.awt.event.KeyListener in project cayenne by apache.

the class AutoCompletion method enable.

/**
 * Enables auto-completion for specified combobox
 *
 * @param comboBox Combo to be featured
 * @param strict Whether strict matching (check 'startWith' or 'contains') should be used
 * @param allowsUserValues Whether non-present items are allowed
 */
public static void enable(JComboBox comboBox, boolean strict, boolean allowsUserValues) {
    comboBox.setEditable(true);
    KeyListener[] listeners = comboBox.getEditor().getEditorComponent().getListeners(KeyListener.class);
    comboBox.setEditor(new CustomTypeComboBoxEditor(comboBox, allowsUserValues));
    for (KeyListener listener : listeners) {
        comboBox.getEditor().getEditorComponent().addKeyListener(listener);
    }
    AutoCompletion ac = new AutoCompletion(comboBox, strict, allowsUserValues);
    comboBox.addFocusListener(ac);
    ac.textEditor.addKeyListener(ac);
    // original keys would not work properly
    SwingUtilities.replaceUIActionMap(comboBox, null);
}
Also used : KeyListener(java.awt.event.KeyListener)

Example 32 with KeyListener

use of java.awt.event.KeyListener in project cayenne by apache.

the class EjbqlQueryScriptsTab method initView.

private void initView() {
    scriptArea = new JUndoableCayenneTextPane(new EJBQLSyntaxConstant());
    scriptArea.getDocument().addDocumentListener(this);
    scriptArea.getDocument().addDocumentListener(new DocumentListener() {

        public void changedUpdate(DocumentEvent e) {
        }

        public void insertUpdate(DocumentEvent e) {
            try {
                String text = scriptArea.getDocument().getText(e.getOffset(), 1);
                if (text.equals(" ") || text.equals("\n") || text.equals("\t")) {
                    getQuery().setEjbql(scriptArea.getDocumentTextDirect());
                    validateEJBQL();
                }
            } catch (BadLocationException ex) {
                logger.warn("Error reading document", ex);
            }
        }

        public void removeUpdate(DocumentEvent e) {
            getQuery().setEjbql(scriptArea.getDocumentTextDirect());
            validateEJBQL();
        }
    });
    scriptArea.getPane().addFocusListener(new FocusListener() {

        EJBQLValidationThread thread;

        public void focusGained(FocusEvent e) {
            thread = new EJBQLValidationThread();
            thread.start();
        }

        public void focusLost(FocusEvent e) {
            thread.terminate();
        }
    });
    scriptArea.getPane().addKeyListener(new KeyListener() {

        boolean pasteOrCut;

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_END || e.getKeyCode() == KeyEvent.VK_HOME || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_UNDO) {
                getQuery().setEjbql(scriptArea.getText());
                validateEJBQL();
            }
            if ((e.getKeyCode() == KeyEvent.VK_V || e.getKeyCode() == KeyEvent.VK_X) && e.isControlDown()) {
                pasteOrCut = true;
            }
        }

        public void keyReleased(KeyEvent e) {
            if ((pasteOrCut && e.getKeyCode() == KeyEvent.VK_CONTROL) || e.getKeyCode() == KeyEvent.VK_DELETE) {
                scriptArea.removeHighlightText();
                getQuery().setEjbql(scriptArea.getText());
                validateEJBQL();
                pasteOrCut = false;
            }
        }

        public void keyTyped(KeyEvent e) {
        }
    });
    setLayout(new BorderLayout());
    add(scriptArea, BorderLayout.WEST);
    add(scriptArea.getScrollPane(), BorderLayout.CENTER);
    setVisible(true);
}
Also used : DocumentListener(javax.swing.event.DocumentListener) DocumentEvent(javax.swing.event.DocumentEvent) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) BorderLayout(java.awt.BorderLayout) JUndoableCayenneTextPane(org.apache.cayenne.modeler.util.JUndoableCayenneTextPane) KeyListener(java.awt.event.KeyListener) EJBQLSyntaxConstant(org.apache.cayenne.swing.components.textpane.syntax.EJBQLSyntaxConstant) FocusListener(java.awt.event.FocusListener) BadLocationException(javax.swing.text.BadLocationException)

Example 33 with KeyListener

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

the class LambdaTest method lambdaAsKeyListener.

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

Example 34 with KeyListener

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

the class LambdaTest method lambdaAsKeyListenerWithExactMatchAndMatchingParameterType.

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

Example 35 with KeyListener

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

the class LambdaTest method lambdaAsKeyListenerWithExactMatchAndNonMatchingParameterType.

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

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