Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.apache.cayenne.modeler.editor;
 
 
 
 import  org.apache.cayenne.map.event.QueryEvent;
 import  org.apache.cayenne.project.validator.EJBQLQueryValidator;
 import  org.apache.cayenne.project.validator.EJBQLQueryValidator.PositionException;
 import  org.apache.cayenne.query.EJBQLQuery;
 import  org.apache.cayenne.query.Query;
 import  org.apache.cayenne.util.Util;
 
 public class EjbqlQueryScriptsTab extends JPanel implements DocumentListener {
 
     protected ProjectController mediator;
     protected JCayenneTextPane scriptArea;
     private boolean updateDisabled;
     protected EJBQLQueryValidator ejbqlQueryValidator = new EJBQLQueryValidator();
 
     public EjbqlQueryScriptsTab(ProjectController mediator) {
         this. = mediator;
         initView();
     }
 
     void displayScript() {
         EJBQLQuery query = getQuery();
         .setText(query.getEjbqlStatement());
          = false;
     }
 
     private void initView() {
 
          = CayenneWidgetFactory.createJEJBQLTextPane();
          
         .getDocument().addDocumentListener(this);
 
 
             public void changedUpdate(DocumentEvent e) {
             }
 
             public void insertUpdate(DocumentEvent e) {
                 try {
                     String text = 
                             .getDocument()
                             .getText(e.getOffset(), 1)
                             .toString();
                     if (text.equals(" ") || text.equals("\n") || text.equals("\t")) {
                         getQuery().setEjbqlStatement(.getText());
                         validateEJBQL();
                     }
                 }
                 catch (BadLocationException e1) {
                     e1.printStackTrace();
                 }
             }
 
             public void removeUpdate(DocumentEvent e) {
                 getQuery().setEjbqlStatement(.getText());
                 validateEJBQL();
             }
         });
 
         .getPane().addFocusListener(new FocusListener() {
 
             EJBQLValidationThread thread;
 
             public void focusGained(FocusEvent e) {
                 = new EJBQLValidationThread();
                .start();
            }
            public void focusLost(FocusEvent e) {
                .terminate();
            }
        });
        .getPane().addKeyListener(new KeyListener() {
            boolean pasteOrCut;
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == .
                        || e.getKeyCode() == .
                        || e.getKeyCode() == .
                        || e.getKeyCode() == .
                        || e.getKeyCode() == .
                        || e.getKeyCode() == .) {
                    getQuery().setEjbqlStatement(.getText());
                    validateEJBQL();
                }
                if ((e.getKeyCode() == . || e.getKeyCode() == .)
                        && e.isControlDown()) {
                     = true;
                }
            }
            public void keyReleased(KeyEvent e) {
                if (( && e.getKeyCode() == .)
                        || e.getKeyCode() == .) {
                    .removeHighlightText();
                    getQuery().setEjbqlStatement(.getText());
                    validateEJBQL();
                     = false;
                }
            }
            public void keyTyped(KeyEvent e) {
            }
        });
        setLayout(new BorderLayout());
        add(.);
        setVisible(true);
    }
    public void initFromModel() {
        Query query = .getCurrentQuery();
        if (!(query instanceof EJBQLQuery)) {
            setVisible(false);
            return;
        }
        .setEnabled(true);
        displayScript();
        validateEJBQL();
        setVisible(true);
    }
    EJBQLQuery getQuery() {
        Query query = .getCurrentQuery();
        return (query instanceof EJBQLQuery) ? (EJBQLQuery) query : null;
    }
    void setEJBQL(DocumentEvent e) {
        Document doc = e.getDocument();
        try {
            setEJBQL(doc.getText(0, doc.getLength()));
        }
        catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }
    void setEJBQL(String text) {
        EJBQLQuery query = getQuery();
        if (query == null) {
            return;
        }
        String testTemp = null;
        if (text != null) {
            testTemp = text.trim();
            if (testTemp.length() == 0) {
                text = null;
            }
        }
        // Compare the value before modifying the query - text area
        // will call "verify" even if no changes have occured....
        if (!Util.nullSafeEquals(textquery.getEjbqlStatement())) {
            query.setEjbqlStatement(text);
            .fireQueryEvent(new QueryEvent(thisquery));
        }
    }
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    public void changedUpdate(DocumentEvent e) {
        if (!) {
            setEJBQL(e);
        }
    }
    void validateEJBQL() {
        PositionException positionException = .validateEJBQL(
                getQuery(),
                .getCurrentDataDomain());
        if (positionException != null) {
            if (positionException.getBeginLine() != null
                    || positionException.getBeginColumn() != null
                    || positionException.getLength() != null) {
                .setHighlightText(
                        positionException.getBeginLine(),
                        positionException.getBeginColumn(),
                        positionException.getLength(),
                        positionException.getMessage());
            }
            else {
                .removeHighlightText();
            }
        }
    }
    class EJBQLValidationThread extends Thread {
        boolean running;
        Object timer = new Object();
        int previousCaretPosition;
        int validateCaretPosition;
        static final int DELAY = 500;
        EJBQLValidationThread() {
            super("ejbql-validation-thread");
            setDaemon(true);
        }
        
        public void run() {
             = true;
            while () {
                int caretPosition = .getCaretPosition();
                if ( != 0
                        &&  == caretPosition
                        &&  != ) {
                    validateEJBQL();
                     = caretPosition;
                }
                 = caretPosition;
                synchronized () {
                    try {
                        .wait();
                    }
                    catch (InterruptedException e) {
                    }
                }
            }
        }
        public void terminate() {
            synchronized () {
                 = false;
                .notify();
            }
        }
    }
New to GrepCode? Check out our FAQ X