Search in sources :

Example 1 with EJBQLSyntaxConstant

use of org.apache.cayenne.swing.components.textpane.syntax.EJBQLSyntaxConstant 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)

Aggregations

BorderLayout (java.awt.BorderLayout)1 FocusEvent (java.awt.event.FocusEvent)1 FocusListener (java.awt.event.FocusListener)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1 BadLocationException (javax.swing.text.BadLocationException)1 JUndoableCayenneTextPane (org.apache.cayenne.modeler.util.JUndoableCayenneTextPane)1 EJBQLSyntaxConstant (org.apache.cayenne.swing.components.textpane.syntax.EJBQLSyntaxConstant)1